Skip to content

Commit 2d1e6da

Browse files
fix(core): stop rebuilding the directory index on every ripgrep entry
1 parent a38c279 commit 2d1e6da

1 file changed

Lines changed: 8 additions & 6 deletions

File tree

‎packages/core/src/filesystem/search.ts‎

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,8 @@ export const ripgrepLayer = Layer.effect(
2929
const scope = yield* Scope.Scope
3030
const state = {
3131
files: [] as string[],
32-
directories: [] as string[],
32+
directories: new Set<string>(),
3333
}
34-
const directories = new Set<string>()
3534
yield* ripgrep
3635
.find({
3736
cwd: location.directory,
@@ -40,9 +39,12 @@ export const ripgrepLayer = Layer.effect(
4039
onEntry: (entry) =>
4140
Effect.sync(() => {
4241
state.files.push(entry.path)
43-
const parts = entry.path.split("/")
44-
parts.slice(0, -1).forEach((_, index) => directories.add(parts.slice(0, index + 1).join("/") + path.sep))
45-
state.directories = Array.from(directories)
42+
// Directories accumulate in place; rebuilding the array per entry made this O(files * directories).
43+
let prefix = ""
44+
for (const part of entry.path.split("/").slice(0, -1)) {
45+
prefix = prefix ? `${prefix}/${part}` : part
46+
state.directories.add(prefix + path.sep)
47+
}
4648
}),
4749
})
4850
.pipe(Effect.orDie, Effect.asVoid, Effect.forkIn(scope))
@@ -104,7 +106,7 @@ export const ripgrepLayer = Layer.effect(
104106
input.type === "file"
105107
? state.files
106108
: input.type === "directory"
107-
? state.directories
109+
? [...state.directories]
108110
: [...state.files, ...state.directories]
109111
return fuzzysort.go(input.query, items, { limit: input.limit ?? 50 }).map((item) => {
110112
const relative = item.target

0 commit comments

Comments
 (0)