Skip to content

Commit c61b4c1

Browse files
npub1qyvc0c5kl4gqv2fd97fsk46tu378sqgy35vc83rvgfwne90sel7s0ed67dTyler Longwell
andcommitted
perf(relay): share event with sig-verify task via Arc instead of deep clone
verify_event only needs a borrow; spawn_blocking only needs 'static. Wrapping the event in an Arc avoids deep-cloning tags + up to 256 KB of content on every ingest. After verification the Arc is uniquely held (the verify task's clone is dropped on completion), so try_unwrap returns the original event; the clone fallback is unreachable in practice but keeps the code total. W8 in PLANS/RELAY_PERF_OPTIMIZATION_PLAN.md; ruled GREEN (no invariant touched) in RESEARCH/RELAY_PERF_CORRECTNESS.md. cargo test -p buzz-relay: 436 passed, 0 failed. Co-authored-by: Tyler Longwell <[email protected]> Signed-off-by: Tyler Longwell <[email protected]>
1 parent 605f712 commit c61b4c1

1 file changed

Lines changed: 8 additions & 2 deletions

File tree

‎crates/buzz-relay/src/handlers/ingest.rs‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1244,8 +1244,13 @@ async fn ingest_event_inner(
12441244
return Err(IngestError::Rejected("restricted: relay-only kind".into()));
12451245
}
12461246

1247-
let event_clone = event.clone();
1248-
let verify_result = tokio::task::spawn_blocking(move || verify_event(&event_clone)).await;
1247+
// Share the event with the verify task via Arc instead of deep-cloning it
1248+
// (tags + up to 256 KB of content). spawn_blocking only needs 'static, not
1249+
// ownership; once it completes its Arc is dropped, so try_unwrap returns
1250+
// the original event without ever having copied it.
1251+
let event = std::sync::Arc::new(event);
1252+
let event_for_verify = std::sync::Arc::clone(&event);
1253+
let verify_result = tokio::task::spawn_blocking(move || verify_event(&event_for_verify)).await;
12491254
match verify_result {
12501255
Ok(Ok(())) => {}
12511256
Ok(Err(e)) => {
@@ -1258,6 +1263,7 @@ async fn ingest_event_inner(
12581263
));
12591264
}
12601265
}
1266+
let event = std::sync::Arc::try_unwrap(event).unwrap_or_else(|arc| (*arc).clone());
12611267

12621268
const MAX_TIMESTAMP_DRIFT_SECS: i64 = 900; // ±15 minutes
12631269
let now = chrono::Utc::now().timestamp();

0 commit comments

Comments
 (0)