Skip to content

perf: 1-file incremental rebuild regresses +79-81% (117ms -> ~209ms), likely from PR #1840 structure-metrics recompute #1855

Closed

Description

@carlos-alm

The "Pre-publish benchmark gate" CI job (tests/benchmarks/regression-guard.test.ts) is failing on both PR #1841 and PR #1843 with an identical regression signature:

[native] 1-file rebuild: 117 -> 209-212ms (+79-81%, threshold 15-50%)
[wasm]   1-file rebuild: 112-113 -> 201-203ms (+79-81%, threshold 15-75%)

Both PRs are stacked on top of PR #1840 ("fix: recompute directory structure metrics for affected directories on incremental rebuild", branch fix/issue-1738-structure-metrics-incremental-stale). Neither #1841 (maxBuffer DEFAULTS refactor) nor #1843 (blast-radius check gating) touch incremental-build or structure-metrics code in their own diffs — the regression is present identically on both, which points to it being inherited from an earlier commit in the stack.

PR #1840's change adds a recompute of directory structure metrics for affected directories on every incremental rebuild. That is a very plausible root cause for a ~80% slowdown specifically isolated to the "1-file rebuild" incremental benchmark tier (full builds and other tiers are unaffected per the regression-guard output).

Ask: Investigate whether PR #1840's incremental structure-metrics recompute is the source of this regression (e.g. recomputing metrics for more directories than necessary, or recomputing unconditionally instead of only when the affected directory's contents changed), and fix at the root cause or add a KNOWN_REGRESSIONS entry with justification if the added work is genuinely necessary and the new baseline is acceptable.

Discovered while sweeping PR #1843 for merge-readiness — deferred because the regression is not part of #1843's own diff scope (git diff origin/fix/issue-1739-maxbuffer-defaults...HEAD --stat for #1843 only touches src/features/check.ts, src/cli/commands/check.ts, tests/integration/check.test.ts, and two SKILL.md docs).

Activity

  1. added
    follow-upDeferred work from PR reviews that needs tracking
    on Jul 6, 2026
  2. carlos-alm commented on Jul 7, 2026

    @carlos-alm
    ContributorAuthor

    Confirmed and fixed in #1950. #1840 is indeed the root cause of the first part of this — but there's a second, independent, compounding cause: #1849 (landed after this issue was filed) adds a second regression on top via a different mechanism. Full writeup below.

    #1840's contribution (structureMs)

    refresh_affected_directory_metrics (Rust: crates/codegraph-core/src/features/structure.rs; TS mirror: src/domain/graph/builder/stages/build-structure.ts) discovers directories whose fan-in/fan-out might have shifted because of the changed file, by finding cross-directory neighbour files. That neighbour search was scoped to the changed file's entire containing directory (file >= dir/ AND file < dir0), not to the changed file itself.

    For the benchmark's fixed probe file, src/domain/queries.ts (leaf directory src/domain, itself a widely-imported hub), this pulled in 251 neighbour files spanning 29 affected directories, including the repo-root src directory — and recomputing src's aggregate metrics (file/symbol counts, fan-in/fan-out, cohesion) requires a near-full-table range scan, measured at ~43ms alone. structureMs in the incremental-benchmark's phase breakdown went from a 3.15.0 baseline of 4ms to 51-58ms on main.

    #1849's contribution (rolesMs) — the second, compounding cause

    #1849 added symbol-level reexports edges (previously only file-level reexports edges existed) so individually re-exported symbols get fan-in credit and aren't misclassified as dead code (mirroring the earlier imports-type fix in #1724). do_classify_incremental's find_neighbour_files (Rust: crates/codegraph-core/src/graph/classifiers/roles.rs; TS mirror: src/features/structure.ts) already filtered on e.kind IN ('calls', 'imports-type', 'reexports') — before #1849 that 'reexports' clause matched nothing (file-level targets don't pass the query's n2.kind NOT IN ('file','directory') filter), so it was a no-op. After #1849, it's not: queries.ts re-exports ~36 symbols from 15 other files, so this widened do_classify_incremental's scope from 1 file to 16 files for every incremental change to queries.ts.

    That widened scope hit two separate pre-existing inefficiencies that were never exercised at this scale on a 1-file change before:

    • find_neighbour_files's SQL used an OR-conditioned self-join SQLite can't serve with an index on either branch of the OR.
    • do_classify_incremental's "is this reexported symbol production-reachable" check ran the exact same repo-wide recursive CTE do_classify_full uses (seeded from every non-test file's imports — 442 rows on this repo, closing over 703 total) — correct for a full rebuild, wasteful when only 16 files are actually in scope. Measured ~18-21ms on this repo.

    rolesMs went from an 18ms baseline to 42-58ms on main.

    Fix — #1950

    • Scope neighbor discovery (both the structure-metrics one and the roles one) to the exact touched file instead of a directory-range/whole-graph scan.
    • Rewrite the OR self-join as a UNION of two directional equi-joins (same result, ~2.4x faster).
    • Replace the global reachability closure with a backward-scoped check (find the small set of barrels reexporting into the affected files, then check reachability per-barrel by walking backward from each one, bounded by that barrel's own chain depth).

    All correctness guarantees preserved — verified against the dedicated test suites for both this issue's fix (issue-1738-structure-metrics-incremental.test.ts) and #1849's fix (issue-1742-reexport-symbol-scope.test.ts) on both engines, the full test suite, and the engine-parity suite.

    Also see #1864, which was independently investigating the same symptom under a "stale baseline" hypothesis — I've reconciled that there too.

  3. added 2 commits that reference this issue on Jul 7, 2026
    a7ca6e9
    61698ae
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    follow-upDeferred work from PR reviews that needs tracking

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions