perf: serialize events once in the append path (#361) #408
No reviewers
Labels
No labels
adr
automated
bug
chore
dependencies
documentation
enhancement
epic
github-actions
P1-high
P2-medium
P3-low
release
research
rust
bug
duplicate
enhancement
help wanted
invalid
question
wontfix
No milestone
No project
No assignees
2 participants
Notifications
Due date
No due date set.
Dependencies
No dependencies set.
Reference
Slipstream/eventcore!408
Loading…
Add table
Add a link
Reference in a new issue
No description provided.
Delete branch "perf/361-single-serialization"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Summary
Events were serialized twice on the way to storage: once to a
serde_json::ValueinStreamWrites::append, then again by each backend when binding thatValuefor SQL (or embedding it on disk).This change makes
StreamWriteEntry::event_dataa pre-serializedBox<serde_json::value::RawValue>, produced once viato_raw_valueinappend. Backends now consume the already-serialized JSON directly:to_string)from_strserde_json::Valuefor its on-disk envelopeThis eliminates the intermediate
Valueallocation and one serialize pass per event across all backends.fs format safety
The fs backend keeps writing a
serde_json::Valueinto its on-disk envelope, so the envelope and its integrity anchor (ADR-0046) stay byte-identical to the historical representation. This is exact, not approximate:from_str::<Value>(to_raw_value(&e).get())yields the same sorted-keyValue(serde_json::Mapis aBTreeMap) as the previousto_value(&e), so the serialized envelope bytes — and thus the anchor — are unchanged. The fs read-time fsck contract tests confirm anchors still validate.Tests
Behavior is unchanged and verified by the existing backend contract suites, which exercise append + read for all four backends (memory, sqlite, postgres, fs). Full workspace suite passes (242 tests).
Workflow note
Phases 1-3 are not applicable: behavior-preserving internal optimization, no public behavioral change. The cross-backend contract suites are the regression safety net (Phase 4 TDD).
Closes #361
This PR optimizes event serialization by pre-serializing events to JSON once in the append path, reducing redundant serialization across different backends. The change appears safe to merge as it maintains existing behavior and passes all tests.
Walkthrough
serde_json::Valueto maintain historical on-disk format.Box<RawValue>for event data, binding it directly to SQL.StreamWriteEntry::event_datatoBox<RawValue>, serialized once at append time.LLM usage and cost
Estimated total USD: $0.365532 via https://api.openai.com and https://api.openai.com