Eliminate statSync calls and filter prefixes first in path completion - #1214
Open
nordicnode wants to merge 1 commit into
Open
Eliminate statSync calls and filter prefixes first in path completion#1214nordicnode wants to merge 1 commit into
nordicnode wants to merge 1 commit into
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Eliminate statSync calls and filter prefixes first in path completion
Summary
• In$O(N)$ synchronous disk
cli/src/utils/path-completion.ts, optimizegetPathCompletionto eliminatestatSynccalls during CLI tab completion.• Previously,
getPathCompletioncalledreaddirSync(parentDir)and immediately ranstatSync(fullPath).isDirectory()on every single item in the directory before checking if it matched the typed prefix. In directories with thousands of files (e.g. user home, desktop, monorepo roots), this executed thousands of synchronous filesystem syscalls on every Tab press, freezing the TUI.• Switched to
readdirSync(parentDir, { withFileTypes: true })and placed the prefix match check (!name.toLowerCase().startsWith(partial)) first. For matching items,entry.isDirectory()checks inode type directly with zero extra syscalls, only falling back tostatSyncwhen a symbolic link needs resolution.• Benchmark: In a directory of 2,500 files, reduced
statSyncsyscalls from 2,505 per Tab completion down to 0, improving completion latency by 6.2x (102.56ms -> 16.55ms across 20 iterations).• Adds regression unit test in
cli/src/__tests__/path-completion.test.tsverifying that symlinked directories continue to resolve and complete correctly.Test plan
[✓]
bun test --config=/dev/null src/__tests__/path-completion.test.ts— 25 pass, 0 fail[✓]
bun test --config=/dev/null src/hooks/__tests__/use-path-tab-completion.test.ts— 33 pass, 0 fail[✓]
bun run --cwd cli typecheck— 0 errors[✓] PR hygiene check passed