Skip to content

YC (S26) | Open Computer History | Continuously record your company computer work, map your workflows, help you find work worth automating, and power your agents' context

perf(redact): flatten the PII worker CPU curve with a duty-cycle cooldown

Louis Beaumont7fe6c30d8291
diff --git a/crates/screenpipe-redact/src/adapters/onnx.rs b/crates/screenpipe-redact/src/adapters/onnx.rs
index b2b09644e..7b0138ade 100644
--- a/crates/screenpipe-redact/src/adapters/onnx.rs
+++ b/crates/screenpipe-redact/src/adapters/onnx.rs
@@ -786,9 +786,14 @@ mod runtime {
// pool burned ~4 cores in ThreadPoolTempl::WorkerLoop while the
// redaction backlog drained (340% CPU regression after 3b9a1a105).
.with_intra_op_spinning(false)?
- // Half the cores is enough for a background batch worker and
- // keeps the drain from monopolizing the machine.
- .with_intra_threads((num_cpus_physical() / 2).max(2))?;
+ // A quarter of the cores (min 1) is plenty for this
+ // background batch worker and caps the height of each CPU
+ // burst — combined with the worker's per-batch duty-cycle
+ // cooldown the redaction backlog drains as a low flat band
+ // instead of pinning half the machine. Latency is a
+ // non-goal here: nothing waits on this redactor
+ // synchronously.
+ .with_intra_threads((num_cpus_physical() / 4).max(1))?;
// NO CoreML EP here, deliberately: this text model is int8-quantized
// RoBERTa with dynamic sequence lengths, and the ANE compiler rejects
// every layer ("E5RT: unbounded dimension is not supported"), so
diff --git a/crates/screenpipe-redact/src/worker/mod.rs b/crates/screenpipe-redact/src/worker/mod.rs
index da814de3c..6c016916b 100644
--- a/crates/screenpipe-redact/src/worker/mod.rs
+++ b/crates/screenpipe-redact/src/worker/mod.rs
@@ -43,12 +43,27 @@ pub use tables::{TargetTable, ALL_TARGET_TABLES};
/// place, just shadowed by a redacted copy).
#[derive(Clone)]
pub struct WorkerConfig {
- /// How many rows to redact per database round-trip.
+ /// How many rows to redact per database round-trip. Also the width of
+ /// each CPU burst: the redactor runs this many inferences back-to-back
+ /// before the worker cools down, so a smaller batch gives a finer,
+ /// flatter CPU curve at the cost of more round-trips.
pub batch_size: u32,
- /// Sleep between batches when the queue isn't empty.
+ /// Lower bound on the post-batch cooldown (and the minimum yield for a
+ /// tiny batch). The actual cooldown is derived from how long the batch
+ /// took — see [`max_active_fraction`](Self::max_active_fraction).
pub idle_between_batches: Duration,
- /// Sleep when the queue IS empty (poll interval).
+ /// Sleep when the queue IS empty (poll interval). Doubles as the upper
+ /// bound on the post-batch cooldown.
pub poll_interval: Duration,
+ /// Ceiling on the worker's wall-clock duty cycle while draining a
+ /// backlog. After each batch the worker sleeps in proportion to the
+ /// time it just spent redacting, so it works at most ~this fraction of
+ /// the time and (since redaction is CPU-bound) holds ~this fraction of
+ /// the cores it touches on average. 0.4 = work 40%, rest 60%. This is
+ /// what turns the backlog drain from a sustained multi-core spike into
+ /// a low, flat band. Clamped to [0.05, 1.0]; 1.0 leaves only the
+ /// `idle_between_batches` floor between batches.
+ pub max_active_fraction: f64,
/// Tables to reconcile. Default: all of [`ALL_TARGET_TABLES`]
/// (frames:full_text, audio, accessibility, ui_events:keyboard,
/// ui_events:clipboard, elements).
@@ -58,14 +73,30 @@ pub struct WorkerConfig {
impl Default for WorkerConfig {
fn default() -> Self {
Self {
- batch_size: 32,
+ batch_size: 16,
idle_between_batches: Duration::from_millis(50),
poll_interval: Duration::from_secs(5),
+ max_active_fraction: 0.4,
tables: ALL_TARGET_TABLES.to_vec(),
}
}
}
+/// Cooldown to sleep after a batch that did work, so the worker holds a
+/// flat, bounded slice of CPU while draining a backlog instead of running
+/// batches back-to-back. Sleeps long enough that the time spent redacting
+/// is at most `max_active_fraction` of the work-plus-sleep cycle, clamped
+/// to [`idle_between_batches`, `poll_interval`].
+fn cooldown_after(worked: Duration, cfg: &WorkerConfig) -> Duration {
+ let frac = cfg.max_active_fraction.clamp(0.05, 1.0);
+ // sleep = worked * (1/frac - 1) => worked / (worked + sleep) == frac
+ let multiplier = (1.0 / frac - 1.0).max(0.0);
+ worked
+ .mul_f64(multiplier)
+ .max(cfg.idle_between_batches)
+ .min(cfg.poll_interval)
+}
+
/// Public status the worker exposes (used by Settings UI).
#[derive(Debug, Clone, Default, serde::Serialize)]
pub struct WorkerStatus {
@@ -190,6 +221,7 @@ impl Worker {
for table in &self.cfg.tables {
// Race the table work against shutdown so a long redact batch
// doesn't hold us through tokio teardown.
+ let batch_start = std::time::Instant::now();
let result = match shutdown.as_ref() {
Some(n) => tokio::select! {
_r = self.process_table(*table) => Some(_r),
@@ -205,6 +237,23 @@ impl Worker {
Some(Ok(n)) if n > 0 => {
any_work = true;
corruption_logged = false; // DB readable again
+
+ // Duty-cycle cooldown — the lever that keeps the CPU
+ // curve flat. The redactor just ran `n` inferences
+ // back-to-back on a few cores; with no pause,
+ // consecutive batches pin those cores for the whole
+ // backlog drain (the big screenpipe CPU spikes users
+ // report). Sleeping in proportion to the time this
+ // batch took holds the worker at `max_active_fraction`
+ // of wall-clock, so the drain reads as a low flat band
+ // rather than a sustained multi-core burst. Cooling
+ // down per-table (not once per full sweep) keeps each
+ // burst one batch wide.
+ let nap = cooldown_after(batch_start.elapsed(), &self.cfg);
+ if race(time::sleep(nap), shutdown.as_ref()).await.is_none() {
+ info!("redact worker: shutdown signal received, exiting");
+ return;
+ }
}
Some(Ok(_)) => {
corruption_logged = false; // DB readable again
@@ -248,12 +297,14 @@ impl Worker {
}
}
- let nap = if any_work {
- self.cfg.idle_between_batches
- } else {
- self.cfg.poll_interval
- };
- if race(time::sleep(nap), shutdown.as_ref()).await.is_none() {
+ // Nothing anywhere had work this sweep — idle at the poll
+ // interval. Batches that DID work already cooled down inline
+ // above, so there's no second nap to add here.
+ if !any_work
+ && race(time::sleep(self.cfg.poll_interval), shutdown.as_ref())
+ .await
+ .is_none()
+ {
info!("redact worker: shutdown signal received, exiting");
return;
}
diff --git a/crates/screenpipe-redact/tests/worker_integration.rs b/crates/screenpipe-redact/tests/worker_integration.rs
index 6894f7a1e..3b909d583 100644
--- a/crates/screenpipe-redact/tests/worker_integration.rs
+++ b/crates/screenpipe-redact/tests/worker_integration.rs
@@ -128,6 +128,7 @@ async fn worker_redacts_all_six_targets() {
idle_between_batches: Duration::from_millis(1),
poll_interval: Duration::from_millis(20),
tables: ALL_TARGET_TABLES.to_vec(),
+ ..Default::default()
};
let worker = Worker::new(pool.clone(), redactor, cfg);
let handle = worker.clone().spawn();