Skip to content

perf: skip unnecessary import detection work#546

Open
TheAlexLichter wants to merge 1 commit into
mainfrom
perf/optimize-import-detection
Open

perf: skip unnecessary import detection work#546
TheAlexLichter wants to merge 1 commit into
mainfrom
perf/optimize-import-detection

Conversation

@TheAlexLichter

@TheAlexLichter TheAlexLichter commented Jun 21, 2026

Copy link
Copy Markdown
Member

This PR improves unimport performance:

  • It skips expensive import detection when code contains no registered auto-import candidates.
  • It also defers static-import parsing until needed while preserving addons, virtual imports, custom parsers, and debug logging.

Benchmark

According to my local benchmarks via Nuxt, I saw a 48% reduction for the "transform-heavy workload". As comparison: Just the old "fail-fast precheck" took 929.1ms.

Median
origin/main 1,162.4 ms
This branch 603.4 ms

A manually constructed "worst case" scenario got roughly 6% worse (late hits using injectAtEnd).

Summary by CodeRabbit

  • New Features

    • Import detection can now skip processing when unnecessary, improving performance.
  • Tests

    • Added test coverage for addon transformations and injections in edge cases.

@coderabbitai

coderabbitai Bot commented Jun 21, 2026

Copy link
Copy Markdown

Review Change Stack

📝 Walkthrough

Walkthrough

src/context.ts gains a canSkipImportDetection helper and a RE_IDENTIFIER regex. Inside injectImports, an early-return branch calls this helper to bypass detectImports/resolveImports when no relevant identifiers are found. src/utils.ts moves strippedCode inside its lazy-init block. Tests cover the new skip and addon-hook paths.

Changes

Import Detection Skip Optimization

Layer / File(s) Summary
canSkipImportDetection logic and early-return in injectImports
src/context.ts
Adds RE_IDENTIFIER regex and canSkipImportDetection async function. Checks parser mode, addon hook presence, virtual-import handling, autoImport === false, and identifier matches against ctx.getImportMap(). Inserts early-return branch in injectImports that calls this helper, emitting an optional debug log and returning an empty imports list when skipping is safe.
Lazy strippedCode computation fix
src/utils.ts
Moves strippedCode = stripCommentsAndStrings(s.original) inside the if (!_staticImports) guard in findStaticImportsLazy, so it is computed only on first access.
Tests for skip paths and addon hooks with no candidates
test/addon-match-imports.test.ts, test/inject.test.ts
Adds a test asserting addon match imports resolve ElInput even without registered candidates. Adds three tests confirming that with zero auto-detectable candidates, addon transform and injectImportsStringified hooks still run, and debugLog emits the expected "0 imports detected" message.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Poem

🐇 When no imports are found in the code so bare,
I skip all the scanning with thoughtful flair!
A regex, a flag, and a hook-check or two —
The bunny leaps fast when there's nothing to do.
Addon hooks still fire, and the debug log sings,
"Zero imports detected!" — efficiency rings! 🎉

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title 'perf: skip unnecessary import detection work' directly and clearly describes the main change: a performance optimization that skips import detection when it's not needed.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch perf/optimize-import-detection

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@TheAlexLichter TheAlexLichter marked this pull request as draft June 21, 2026 12:14
@TheAlexLichter TheAlexLichter marked this pull request as ready for review June 21, 2026 12:22
@TheAlexLichter TheAlexLichter requested a review from antfu June 21, 2026 12:23
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.

1 participant