Skip to content

Fix 3x scan performance regression caused by WatchesFile recreating watchers on every open()#262

Merged
albe merged 2 commits into
mainfrom
copilot/bisect-performance-regression
Mar 21, 2026
Merged

Fix 3x scan performance regression caused by WatchesFile recreating watchers on every open()#262
albe merged 2 commits into
mainfrom
copilot/bisect-performance-regression

Conversation

Copilot AI commented Mar 21, 2026

Copy link
Copy Markdown
Contributor

Scans (forward, backward, join, range) were ~3x slower than v0.7.2 because ReadOnlyPartition (via the WatchesFile mixin) was creating and destroying an OS-level file watcher on every partition.open() call — which fires once per document read during a scan via getPartition().

Root cause

WatchesFile.open() unconditionally called watchFile() after delegating to super.open(), but ReadablePartition.open() returns true both on first open and when already open (if (this.fd) return true). So each of the 10,000 getPartition() calls per scan triggered a full watcher teardown + recreation (~3.5 µs vs ~0.02 µs for a null check).

// Before — watchFile() fires on every call, even when already open
open() {
    if (super.open()) {
        this.watchFile();   // ← recreates Watcher every time
        return true;
    }
    return false;
}

// After — only create the watcher once
open() {
    if (super.open()) {
        if (!this.watcher) {
            this.watchFile();
        }
        return true;
    }
    return false;
}

Changes

  • src/WatchesFile.js: Guard watchFile() with if (!this.watcher) so the file watcher is created only on the first open(), not on subsequent no-op calls.
  • src/Storage/ReadableStorage.js: Guard this.emit('preRead', …) with listenerCount('preRead') > 0 to skip EventEmitter overhead when no hooks are registered.
  • src/Storage/WritableStorage.js: Same guard for this.emit('preCommit', …).

⌨️ Start Copilot coding agent tasks without leaving your editor — available in VS Code, Visual Studio, JetBrains IDEs and Eclipse.

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
Copilot AI requested a review from albe March 21, 2026 16:10
Copilot finished work on behalf of albe March 21, 2026 16:10
@albe
albe marked this pull request as ready for review March 21, 2026 21:14
@coveralls

Copy link
Copy Markdown

Coverage Status

coverage: 97.892% (+0.009%) from 97.883%
when pulling 7e68ccd on copilot/bisect-performance-regression
into eaa8ee1 on main.

@albe
albe merged commit 5c60d83 into main Mar 21, 2026
10 checks passed
@albe
albe deleted the copilot/bisect-performance-regression branch March 21, 2026 21:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants