Fix 3x scan performance regression caused by WatchesFile recreating watchers on every open()#262
Merged
Merged
Conversation
…recreation and emit calls Co-authored-by: albe <4259532+albe@users.noreply.github.com> Agent-Logs-Url: https://github.com/albe/node-event-storage/sessions/30186b59-6016-4170-a527-bc2a9e7c36bb
Copilot
AI
changed the title
[WIP] Investigate performance regression in scan scenarios
Fix 3x scan performance regression caused by WatchesFile recreating watchers on every open()
Mar 21, 2026
albe
marked this pull request as ready for review
March 21, 2026 21:14
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.
Scans (forward, backward, join, range) were ~3x slower than v0.7.2 because
ReadOnlyPartition(via theWatchesFilemixin) was creating and destroying an OS-level file watcher on everypartition.open()call — which fires once per document read during a scan viagetPartition().Root cause
WatchesFile.open()unconditionally calledwatchFile()after delegating tosuper.open(), butReadablePartition.open()returnstrueboth on first open and when already open (if (this.fd) return true). So each of the 10,000getPartition()calls per scan triggered a full watcher teardown + recreation (~3.5 µs vs ~0.02 µs for a null check).Changes
src/WatchesFile.js: GuardwatchFile()withif (!this.watcher)so the file watcher is created only on the firstopen(), not on subsequent no-op calls.src/Storage/ReadableStorage.js: Guardthis.emit('preRead', …)withlistenerCount('preRead') > 0to skip EventEmitter overhead when no hooks are registered.src/Storage/WritableStorage.js: Same guard forthis.emit('preCommit', …).⌨️ Start Copilot coding agent tasks without leaving your editor — available in VS Code, Visual Studio, JetBrains IDEs and Eclipse.