Skip to content

refactor(core): cache combined find index until rescan - #63454

Open
delaysystem wants to merge 1 commit into
anomalyco:devfrom
delaysystem:opt/find-index-cache
Open

delaysystem wants to merge 1 commit into
anomalyco:devfrom
delaysystem:opt/find-index-cache

Conversation

@delaysystem

Copy link
Copy Markdown

Issue for this PR

N/A — small performance refactor, no behavior change.

Type of change

  • Bug fix
  • New feature
  • Refactor / code improvement
  • Documentation

What does this PR do?

Every find call rebuilt the candidate arrays from the index maps (Array.from over all files/directories). Since each scan swaps in a fresh index object atomically, this caches the files/directories/combined arrays in a WeakMap keyed by the index object, so repeated finds reuse them and the cache is naturally dropped on the next rescan. Adds cache and refresh tests.

Note: this touches packages/core/src/filesystem/search.ts in a hunk disjoint from the sibling PR from opt/scan-prefix-incremental. They are sequenced — if that one lands first, this branch will rebase.

How did you verify your code works?

Ran the focused suite in packages/core:

bun test test/filesystem/search.test.ts
10 pass, 0 fail, 40 expect() calls

Checklist

  • I have tested my changes locally
  • I have not included unrelated changes in this PR

@opencodejr opencodejr Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

The caching itself looks correct: each rescan fills a fresh index object and only then swaps it in, and find waits for the first scan before searching. So arrays cached per index object can't go stale, and fuzzysort only reads them. I read the scan and refresh flow, ran search.test.ts and filetype.test.ts, and type-checked packages/core (typecheck:tsc, since the typecheck script failed with Script not found "check").

Public API: unchanged.

Two things to fix:

  • This branch also carries b8e5960e5 (pinning the Swift highlights query, plus its test in packages/tui). That is unrelated to the find cache and is already its own PR, #63165. Please drop it from this branch.
  • The new test doesn't exercise the cache (see the inline comment).

}),
)

it.effect("serves repeated finds from a cached index and drops the cache on refresh", () =>

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This test passes without the cache too: I put the base search.ts back and the suite still passes 10/10. Equal results across repeated finds, and new results after a rescan, are already how the code behaves today. The test also needs a fake Ripgrep, TestClock and a polling Effect.repeat loop, which is a lot of setup for that. The existing refresh tests already cover the rescan behaviour, so I'd drop this test. If you want to keep one, it should fail when the cache is removed, for example by checking that a repeated find doesn't rebuild the candidate arrays.

Comment thread packages/tui/src/parsers-config.ts Outdated
// NOTE: Using parser repo queries instead of nvim-treesitter due to incompatible #lua-match? predicates
// "https://raw.githubusercontent.com/nvim-treesitter/nvim-treesitter/refs/heads/master/queries/highlights.scm
"https://raw.githubusercontent.com/alex-pinkus/tree-sitter-swift/main/queries/highlights.scm",
"https://raw.githubusercontent.com/alex-pinkus/tree-sitter-swift/0.7.1/queries/highlights.scm",

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This Swift highlights change (and its filetype.test.ts test) is unrelated to the find cache. It is already open as #63165, so please remove it from this PR.

Cache the files+directories array in a WeakMap keyed by index object, invalidated atomically on index swap. Adds cache+refresh tests.

@opencodejr opencodejr Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Thanks for dropping the Swift highlights change. The PR now only touches the find cache in packages/core. Your changes to search.ts and search.test.ts are the same as before the rebase, and search.test.ts still passes.

The public API is unchanged.

The open point about the new test is still unaddressed: it passes without the cache too, so it doesn't actually check the cache.

This branch has not been deployed

No deployments
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