← Back to Pull requests

perf(linter/import/no-cycle): reduce allocations #24961

Closed
unknown closed this pull request without merging on Jul 28, 2026 00:11 UTC
View on GitHub
unknown unknown commented on Jul 26, 2026 22:53 UTC
Author

[!WARNING]

This is a PROOF OF CONCEPT, not a merge-ready change.

> It is opened as a draft to get people smarter than me to confirm whether this change makes sense and whether there are downsides to this improvement. The traversed change in particular relies on an invariant that needs confirmation (see Open questions). Please review the approach and whether this PR makes sense.

AI Disclosure: This was generated using Claude Code w/ Opus 5. I have reviewed it to the best of my ability and confirmed to the best of my ability that it does not break anything (at least not obviously) in the vscode codebase. But it probably needs further testing. If we want to close this PR and have someone else push this change forward, that'd be fine by me.

What this shows

import/no-cycle walks the module graph once per direct import of every linted file, and nearly
every node and edge of that walk allocates. On the vscode codebase (src, 7,368 files) the rule
accounts for 330M of oxlint's 383M heap allocations and ~86% of total rule time when it is
enabled.

Removing four hot-path allocations cuts that substantially.

Changes

  • should_traverse_module collected two Vecs per graph edge, only to test is_empty() and

  • all(is_type). Now iterates with an early break.
  • Visitor child list allocated a fresh Vec per visited node. Now staged on one scratch stack

  • shared by the whole walk; each frame owns scratch[start..end] and truncates on the way out.
    Entries are moved out with mem::replace rather than cloned.
  • traversed set cloned a PathBuf per visited node. Now keyed on Arc pointer identity.

  • Cycle stack cloned a PathBuf per node entered. Now holds an Arc<ModuleRecord>.

  • std::env::current_dir() is resolved once per file rather than once per emitted diagnostic.
  • Measurements

    vscode src, 7,368 files, all 785 non-type-aware rules enabled, release build with mimalloc:

    | metric | before | after | delta |
    | ----------- | ------ | ------ | ---------- |
    | allocations | 383.4M | 134.1M | -65.0% |
    | bytes | 29.0GB | 12.5GB | -57.0% |
    | wall clock | 3635ms | 2719ms | -25.2% |
    | CPU time | 56.8s | 42.5s | -25.2% |

    With only import/no-cycle enabled, allocations go from 332.6M to 83.4M (-74.9%).

    Wall clock and CPU are paired interleaved measurements (alternating binary order to cancel thermal
    drift), n=20, t=30.9 and t=71.3, patched build faster in 20/20 runs. These are measured from the
    exact commit on this branch, not from a prototype. Allocation counts come from a
    counting global allocator, 2-3 reps, run-to-run spread <0.001%.

    Correctness so far

  • The 1,566 no-cycle diagnostics on vscode are byte-identical before and after when sorted.

  • A full 785-rule run reports the same 1,252,020 warnings and 16 errors.

  • All 41 import:: and oxc/no_barrel_file unit tests pass. Clippy is clean.
  • Open questions

    1. Is the Arc pointer key acceptable? It is equivalent to keying on the path only because
    linking resolves every loaded_modules edge to modules_by_path[path].last(). That is true
    today, but it is implicit, and it sits next to a TODO: revise how to store multiple sections in
    loaded_modules
    . I added a debug_assert that re-checks pointer identity against path identity,
    but interning paths to a u32 id would remove the coupling entirely. Preference?

    2. Arc<PathBuf> was tried and is worse. Changing resolved_absolute_path to Arc<PathBuf>
    reaches the same allocation count with exact path semantics, but benchmarks
    slower than the
    unpatched baseline* (3027ms vs 2893ms) — cloning an Arc shared across 18 rayon threads turns a
    malloc into a contended atomic increment. Recording this so nobody repeats it.

    3. The algorithmic cost is untouched. no-cycle is still O(files x graph size), with a fresh
    traversed set per direct import. The remaining 83M allocations are mostly CompactStr
    specifier clones in the scratch extend. Making loaded_modules a pre-sorted immutable list
    after graph construction would let the walk borrow instead of clone, and is probably the larger
    remaining win. Worth pursuing?

    Not done

  • Only benchmarked on vscode. A repo with a shallower import graph will show less.

  • No new tests locking in the behaviour; existing coverage is what passed.
  • Diff: 560 lines (+420 / -140) across 7 files Reviewer: connorshea Review duration: 1518m
    All Vouch review confidence checks passed
    Residual risk 0.22 = Change Risk (0.55) × (1 − Review Confidence (0.60)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.55 · 560 lines diff across 7 files
    0.55
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.17 · 1 substantive comment(s)
    0.17
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 35%
    60%
    Bedrock Claude 3 · Risk Narration
    (disconnected)
    Groq
    Groq Risk Narration
    AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
    Model 1 · Change Risk Attribution
    SHAP Weights
    Diff Size diff_size
    0.379
    Shallow Review Depth shallow_review_depth
    0.289
    Ai Authored ai_authored
    0.050
    Real PR Conversation Timeline
    1 comments
    "Review active on #24961"
    general weight: 0.35
    Reviewers
    Assignees
    Unassigned
    Labels
    risk: low confidence: 60.5% reviewed
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 54.7% (MEDIUM) · Higher = larger blast radius
    54.7%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 60.5% · Lower = rushed or shallow review
    60.5%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 21.6% · Safe to merge
    21.6%