Skip to content

perf(core): skip encoding durable event payloads that aren't persisted - #58923

Merged
kitlangton merged 2 commits into
devfrom
perf/bus-skip-encode
Oct 25, 2026
Merged

kitlangton merged 2 commits into
devfrom
perf/bus-skip-encode

Conversation

@kitlangton

Copy link
Copy Markdown
Contributor

What it does. Every durable event was schema-encoded on publish, even when persist is off (the default) and the encoded payload was thrown away. This skips that work. Clients receive exactly what they did before.

How it works.

  1. In commitDurableEvent and publishAll, the payload is encoded only where it's used: the stale-replay comparison and the EventTable insert, both of which already run only when persist is on.
  2. The event_sequence read/upsert and event.durable.seq are unchanged. They still run with persist off, because the session projector (session_message.seq, fork copy, current-assistant lookup), SessionTitle's optimistic check and SessionTransfer depend on them. Only the encode and payload are skipped.

Risk. Server only, no data or wire change. With persist off, a payload that fails schema encoding no longer throws at publish. Nothing catches or relies on that throw, ephemeral events never had it, and projections that store data run their own encode.

Proof.

  • New packages/core/test/bus-unpersisted.test.ts, with persist off: message seqs still increase and Bus.latestSequence advances; the title path's sequence check still accepts a fresh seq and rejects a stale one; an encode-counting schema counts 3 encodes on dev and 0 with this change.
  • bun test on the bus, projector and title test files: 103 pass. bun run typecheck in packages/core: clean.
  • Micro-benchmark, 20,000 non-persisted 2 KB events: 25.8 µs → 24.0 µs per event (about 7%). Larger step and tool payloads save more.

@opencode-agent opencode-agent Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The change looks sound. With persist off, the encoded payload was only used by the stale-replay comparison and the EventTable insert, which already run only with persist on, so skipping the encode doesn't change behaviour. With persist on, the encode still runs inside the transaction, so an encode failure still rolls it back. I checked that the new encode-count test fails on the base (3 encodes) and passes here. The bus, projector and title tests pass (103), and tsc -b in packages/core is clean.

Public API: unchanged (server internals only; no wire, event or schema change).

One small ask: the new test file is 137 lines for a 16-line change. Its first two cases (message seqs and the title path's sequence check) pass on the base too, because they cover code this PR doesn't touch. The test could be cut down to the encode-count case, which is the one that checks this change.

.run()
return db
})

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These first two cases pass unchanged on the base, because they cover sequence behaviour this PR doesn't touch. They and seedSession could be dropped so the file is just the Counted encode-count case, which fails on the base and passes here.

@kitlangton
kitlangton merged commit 2d91ed5 into dev Oct 25, 2026
12 checks passed
@kitlangton
kitlangton deleted the perf/bus-skip-encode branch October 25, 2026 13:16
@amrkmn amrkmn mentioned this pull request Oct 27, 2026
1 task
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant