-
Notifications
You must be signed in to change notification settings - Fork 1
Optimize DataStore versioning and reduce allocations #68
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -159,6 +159,9 @@ class DataStore { | |
| private recoveredFromSnapshot = false; | ||
| private isRestoringSnapshot = false; | ||
|
|
||
| /** P0-2: monotonic write counter — subscribers can use this to skip redundant Map rebuilds */ | ||
| private _version = 0; | ||
|
|
||
| // ── Cold store integration ────────────────────────────────────────────── | ||
|
|
||
| /** Decoded warm cache: cold frames re-decoded for the current scrub window. */ | ||
|
|
@@ -556,17 +559,23 @@ class DataStore { | |
|
|
||
| const msgID = message.msgID; | ||
|
|
||
| // Round sensor readings to 3 decimal places for cleaner display | ||
| const roundedData = { ...message.data }; | ||
| Object.keys(roundedData).forEach((key) => { | ||
| const signal = roundedData[key]; | ||
| // P0-3: only allocate new signal objects when rounding actually changes the value. | ||
| // This avoids a per-frame { ...message.data } spread and per-signal spread for | ||
| // signals that are already at 3 decimal places. | ||
| const roundedData: typeof message.data = {}; | ||
| for (const key of Object.keys(message.data)) { | ||
| const signal = message.data[key]; | ||
| if (signal && typeof signal.sensorReading === 'number') { | ||
| roundedData[key] = { | ||
| ...signal, | ||
| sensorReading: Math.round(signal.sensorReading * 1000) / 1000, | ||
| }; | ||
| const rounded = Math.round(signal.sensorReading * 1000) / 1000; | ||
| // Only allocate a new signal object if rounding changed the value. | ||
| // This preserves object identity for the common case (already 3 dp). | ||
| roundedData[key] = rounded === signal.sensorReading | ||
| ? signal | ||
| : { ...signal, sensorReading: rounded }; | ||
| } else { | ||
| roundedData[key] = signal; | ||
| } | ||
| }); | ||
| } | ||
|
|
||
| // Create the sample | ||
| const sample: TelemetrySample = { | ||
|
|
@@ -606,6 +615,7 @@ class DataStore { | |
|
|
||
| // Notify all subscribers | ||
| this.notifyAll(msgID); | ||
| this._version++; | ||
|
Comment on lines
617
to
+618
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
When a live frame is ingested after Useful? React with 👍 / 👎. |
||
| } | ||
|
|
||
| /** | ||
|
|
@@ -645,16 +655,19 @@ class DataStore { | |
|
|
||
| const buffers = this.getSourceBuffers(source); | ||
|
|
||
| const roundedData = { ...message.data }; | ||
| Object.keys(roundedData).forEach((key) => { | ||
| const signal = roundedData[key]; | ||
| if (signal && typeof signal.sensorReading === "number") { | ||
| roundedData[key] = { | ||
| ...signal, | ||
| sensorReading: Math.round(signal.sensorReading * 1000) / 1000, | ||
| }; | ||
| // P0-3: same optimisation as ingestMessage — skip allocations when rounding is a no-op. | ||
| const roundedData: typeof message.data = {}; | ||
| for (const key of Object.keys(message.data)) { | ||
| const signal = message.data[key]; | ||
| if (signal && typeof signal.sensorReading === 'number') { | ||
| const rounded = Math.round(signal.sensorReading * 1000) / 1000; | ||
| roundedData[key] = rounded === signal.sensorReading | ||
| ? signal | ||
| : { ...signal, sensorReading: rounded }; | ||
| } else { | ||
| roundedData[key] = signal; | ||
| } | ||
| }); | ||
| } | ||
|
|
||
| const sample: TelemetrySample = { | ||
| timestamp, | ||
|
|
@@ -689,6 +702,7 @@ class DataStore { | |
| this.scheduleSnapshotSave(); | ||
| this.notifyTrace(); | ||
| this.notifyAll(); | ||
| this._version++; | ||
| } | ||
|
|
||
| /** | ||
|
|
@@ -981,6 +995,7 @@ class DataStore { | |
| this.scheduleSnapshotSave(); | ||
| this.notifyAll(); | ||
| this.notifyTrace(); | ||
| this._version++; | ||
| } | ||
|
|
||
| // ── Cold state / warm cache API ─────────────────────────────────────────── | ||
|
|
@@ -1143,6 +1158,7 @@ class DataStore { | |
| this.getSourceBuffers(source).trace = []; | ||
| this.scheduleSnapshotSave(); | ||
| this.notifyTrace(); | ||
| this._version++; | ||
| } | ||
|
|
||
| private pruneTraceBuffer(referenceTimeMs: number, source: TelemetrySource = this.activeSource): void { | ||
|
|
@@ -1224,6 +1240,7 @@ class DataStore { | |
| this.getSourceBuffers(source).byMsgId.delete(msgID); | ||
| this.scheduleSnapshotSave(); | ||
| this.notifyAll(msgID); | ||
| this._version++; | ||
| } | ||
|
|
||
| /** | ||
|
|
@@ -1236,6 +1253,7 @@ class DataStore { | |
| } | ||
|
|
||
| this.retentionWindowMs = windowMs; | ||
| this._version++; | ||
|
|
||
| // Prune all messages with new window | ||
| for (const source of ["live", "replay"] as TelemetrySource[]) { | ||
|
|
@@ -1257,6 +1275,16 @@ class DataStore { | |
| this.notifyTrace(); | ||
| } | ||
|
|
||
| /** | ||
| * Returns the current monotonic write version. | ||
| * Incremented on every mutating operation (ingest, clear, etc.). | ||
| * Subscribers can snapshot this value and skip expensive recomputations | ||
| * when the version hasn't changed since their last render. | ||
| */ | ||
| public getVersion(): number { | ||
| return this._version; | ||
| } | ||
|
|
||
| /** | ||
| * Get current retention window | ||
| * @returns Retention window in milliseconds | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For any signal whose reading is already rounded, the stored sample now keeps the caller's signal object by reference. If the decoded message object is reused or mutated after
ingestMessage, historical data in DataStore changes as well; the previous implementation always spread each signal and the new immutability test only covers the changed-rounding case, so no-op values still alias the caller.Useful? React with 👍 / 👎.