fix: trace store incremental dirty tracking to eliminate GVL stall#43
Merged
Conversation
snapshot_dirty_state was serializing ALL traces to JSON on every flush just to diff and find which ones changed. With thousands of traces this monopolized the Ruby GVL for seconds, starving every other thread and pegging a single core at 100%. Replace the boolean @traces_dirty flag with a per-trace @dirty_trace_ids Set. Now flush only serializes traces that were actually modified — O(changed) instead of O(total). Also eliminates the redundant full-collection serialize on boot (load_from_local).
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
@traces_dirtywith per-trace@dirty_trace_idsSet +@deleted_trace_idsSetsnapshot_dirty_statenow only serializes traces in the dirty set — O(changed) not O(total)load_from_localstores raw DB rows directly instead of re-serializing all traces on bootProblem
The trace store's
snapshot_dirty_statewas calling@traces.transform_values { |t| serialize_trace_for_db(t) }on every flush — serializing every trace to JSON just to diff against the last-persisted state. With thousands of traces containing nested payloads with floats, this:The
decay_cycleactor (fires every 60s) callsstore.store(trace)on every trace with non-zero decay, setting@traces_dirty = true, which guaranteed the full-serialize path fired every cycle.Fix
Track dirty state per-trace-ID instead of as a boolean. Only serialize the traces that actually changed during flush. The decay cycle touching 500 of 10,000 traces now serializes only those 500.
Test plan