From 37923d91019840cd98cb7bada21cc4f821f1bf7a Mon Sep 17 00:00:00 2001 From: Claude Date: Tue, 21 Jul 2026 16:14:12 +0000 Subject: [PATCH] perf(relay): run the stored REQ replay undispatched SmallReqFloorBenchmark showed the per-REQ floor on small results is dominated by pipeline, not the store (raw query 0.125 ms vs 0.785 ms session REQ->EOSE in-process). Half of the dispatch slice was the scheduler hop between handleReq's launch and the query coroutine: starting the job with CoroutineStart.UNDISPATCHED runs the stored replay and EOSE inline on the receiving coroutine (the reader-pool acquire doesn't suspend when a connection is free), parking only at the live tail. Measured: dispatch+frames slice 0.397 -> 0.207 ms. Commands on a connection are processed sequentially, so nothing can target the subscription before the job lands in the registry at the first suspension point. Verified: quartz relay.server suite, SmallReqFloorBenchmark, geode full test suite. Co-Authored-By: Claude Fable 5 Claude-Session: https://claude.ai/code/session_01RzkoN3SJHCZWRAiadXXG4w --- .../quartz/nip01Core/relay/server/RelaySession.kt | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/quartz/src/commonMain/kotlin/com/vitorpamplona/quartz/nip01Core/relay/server/RelaySession.kt b/quartz/src/commonMain/kotlin/com/vitorpamplona/quartz/nip01Core/relay/server/RelaySession.kt index 6acacc1705..f73493f19c 100644 --- a/quartz/src/commonMain/kotlin/com/vitorpamplona/quartz/nip01Core/relay/server/RelaySession.kt +++ b/quartz/src/commonMain/kotlin/com/vitorpamplona/quartz/nip01Core/relay/server/RelaySession.kt @@ -49,6 +49,7 @@ import com.vitorpamplona.quartz.utils.Log import com.vitorpamplona.quartz.utils.cache.LargeCache import kotlinx.coroutines.CancellationException import kotlinx.coroutines.CoroutineScope +import kotlinx.coroutines.CoroutineStart import kotlinx.coroutines.Job import kotlinx.coroutines.channels.ClosedSendChannelException import kotlinx.coroutines.launch @@ -291,8 +292,16 @@ class RelaySession( // Policy may rewrite filters to match the user's access level. val filters = (result as PolicyResult.Accepted).cmd.filters + // UNDISPATCHED: the stored replay runs inline on this coroutine — + // the reader-pool acquire doesn't suspend when a connection is + // free, so EVENT frames and EOSE go out without a scheduler hop + // (SmallReqFloorBenchmark: the hop was most of the dispatch + // slice on small REQs). The coroutine first parks at the live + // tail (awaitCancellation), which is when launch returns and the + // job lands in [subscriptions]; commands on this connection are + // processed sequentially, so nothing can target the sub earlier. val job = - scope.launch { + scope.launch(start = CoroutineStart.UNDISPATCHED) { try { if (policy.filtersOutgoingEvents) { // Screened path: every event is materialized so the