Skip to content

refactor: lazily cache resolved analyzeFile reference in createAnalyzer#275

Merged
askpt merged 2 commits intomainfrom
repo-assist/improve-createanalyzer-lazy-resolution-20260428-822fe14877b7f5c0
Apr 30, 2026
Merged

refactor: lazily cache resolved analyzeFile reference in createAnalyzer#275
askpt merged 2 commits intomainfrom
repo-assist/improve-createanalyzer-lazy-resolution-20260428-822fe14877b7f5c0

Conversation

@github-actions
Copy link
Copy Markdown
Contributor

🤖 This PR was created by Repo Assist, an automated AI assistant.

Problem

The closure returned by createAnalyzer() previously called require(modulePath) and re-validated the analyzer class on every invocation:

return function (sourceText: string): UnifiedFunctionMetrics[] {
  const mod = require(modulePath) as Record<string, AnalyzerClass | undefined>;
  const analyzerClass = mod[className];
  if (!analyzerClass || typeof analyzerClass.analyzeFile !== "function") {
    throw new Error(...); // checked every call
  }
  const functions = analyzerClass.analyzeFile(sourceText);
  ...
};

While Node.js caches module loads, require() still incurs a lookup in the module registry on each call, and the class-validity guard runs on every analysis — even though neither the module nor the class can change after the first successful load.

Fix

A cachedAnalyze variable is introduced in the createAnalyzer closure scope. It is null until the first call, at which point the module is loaded, the class is validated, and the bound analyzeFile reference is stored. All subsequent calls skip directly to cachedAnalyze(sourceText):

let cachedAnalyze: ((sourceText: string) => RawFunctionMetrics[]) | null = null;

return function (sourceText: string): UnifiedFunctionMetrics[] {
  if (!cachedAnalyze) {
    // one-time resolution and validation
    cachedAnalyze = analyzerClass.analyzeFile.bind(analyzerClass);
  }
  const functions = cachedAnalyze(sourceText);
  ...
};

Behaviour

  • Error message on misconfiguration is identical.
  • Lazy loading is preserved (module is still not loaded until first analysis request for that language).
  • The resolution lifecycle is now explicit in the code: lazy init on first use, stable reference thereafter.

Test Status

  • npm run compile — no errors
  • npm run lint — no warnings
  • ⚠️ npm test (vscode-test) — requires VS Code download; not available in this sandboxed environment (known infrastructure limitation).

Generated by 🌈 Repo Assist, see workflow run.

Warning

⚠️ Firewall blocked 1 domain

The following domain was blocked by the firewall during workflow execution:

  • releaseassets.githubusercontent.com

To allow these domains, add them to the network.allowed list in your workflow frontmatter:

network:
  allowed:
    - defaults
    - "releaseassets.githubusercontent.com"

See Network Configuration for more information.

Generated by 🌈 Repo Assist, see workflow run. Learn more.

To install this agentic workflow, run

gh aw add githubnext/agentics/workflows/repo-assist.md@3de4e604a36b5190a1c7dc4719c7341500ba8a95

Previously, the closure returned by createAnalyzer() called require()
and validated the analyzer class on every invocation — even though
the module is loaded once and the class never changes after that.

This commit introduces a per-language cachedAnalyze variable that is
populated on the first call and reused for all subsequent calls.
After the first successful invocation:
- require() is never called again for that language
- the class-validity check is skipped
- analyzeFile is invoked directly via the cached bound reference

The change is purely mechanical (same observable behaviour, same error
message on misconfiguration) and makes the resolution lifecycle
explicit: lazy init on first use, stable reference thereafter.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@askpt askpt changed the title [Repo Assist] refactor: lazily cache resolved analyzeFile reference in createAnalyzer refactor: lazily cache resolved analyzeFile reference in createAnalyzer Apr 30, 2026
@askpt askpt marked this pull request as ready for review April 30, 2026 20:30
@askpt askpt self-requested a review as a code owner April 30, 2026 20:30
Copilot AI review requested due to automatic review settings April 30, 2026 20:30
@codecov
Copy link
Copy Markdown

codecov Bot commented Apr 30, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 66.96%. Comparing base (ce22c0d) to head (ff1f042).
⚠️ Report is 1 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #275      +/-   ##
==========================================
+ Coverage   66.82%   66.96%   +0.14%     
==========================================
  Files           9        9              
  Lines        2565     2576      +11     
  Branches      222      224       +2     
==========================================
+ Hits         1714     1725      +11     
  Misses        850      850              
  Partials        1        1              

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR refactors createAnalyzer() in the VS Code extension’s metrics analyzer factory to lazily resolve and then cache the resolved analyzeFile function per language, avoiding repeated module registry lookups and repeated runtime validation on subsequent analyses.

Changes:

  • Cache a bound analyzeFile reference within the createAnalyzer() closure after the first successful invocation.
  • Preserve lazy-loading behavior while ensuring the module/class guard runs only once per language.
  • Update function documentation to describe the one-time resolution/caching behavior.

@askpt askpt merged commit 5fc7d1f into main Apr 30, 2026
13 checks passed
@askpt askpt deleted the repo-assist/improve-createanalyzer-lazy-resolution-20260428-822fe14877b7f5c0 branch April 30, 2026 20:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants