Skip to content
Source
Sign inSign up
pingdotgg/t3code

perf(server): replay stored events about twice as fast #26719

Mergedhieu-lee(GitHub profile, opens in a new tab)merged 5 commits intomain fromrefs/pull/26719/headmerged by juliusmarminge View on GitHub

Showing 1 changed file with 37 additions and 19 deletions.

x
0 of 1 viewed, 1 left
37 additions and 19 deletionsapps/server/src/persistence/OrchestrationEventStore.tsModified
View file
@@ -19,6 +19,7 @@
import * as SqlClient from "effect/sql/SqlClient";
import * as SqlSchema from "effect/sql/SqlSchema";
import * as Effect from "effect/Effect";
+import { identity } from "effect/Function";
import * as Layer from "effect/Layer";
import * as PubSub from "effect/PubSub";
import * as Option from "effect/Option";
@@ -181,8 +182,21 @@
const decodeV2EventJson = Schema.decodeUnknownEffect(OrchestrationV2DomainEventJson);
const encodeV2EventJson = Schema.encodeEffect(OrchestrationV2DomainEventJson);
const decodeV2StoredEvent = Schema.decodeUnknownEffect(OrchestrationV2StoredEvent);
+// decodeV2EventJson already validated the event; decoding it again here is the hot cost in replay.
+const decodeV2StoredEventHeader = Schema.decodeUnknownEffect(
+ OrchestrationV2StoredEvent.mapFields(({ event: _event, ...fields }) => fields),
+);
const decodeJson = Schema.decodeUnknownEffect(Schema.fromJsonString(Schema.Unknown));
const encodeJson = Schema.encodeEffect(Schema.fromJsonString(Schema.Unknown));
+// Same result as decodeJson; only malformed input pays for the schema's error.
+const parseJson = (json: string) => {
+ if (typeof json !== "string") return decodeJson(json);
+ try {
+ return Effect.succeed<unknown>(JSON.parse(json));
+ } catch {
+ return decodeJson(json);
+ }
+};
​
function metadataForV2Event(event: OrchestrationV2DomainEvent): Record<string, unknown> {