Skip to content

LLM Wiki is a cross-platform desktop application that turns your documents into an organized, interlinked knowledge base — automatically. Instead of traditional RAG (retrieve-and-answer from scratch every time), the LLM incrementally builds and maintains a persistent wiki from your sources。

TypeScript

perf: dedupe concurrent directory listings

nash_suf32d5bf3f99a
diff --git a/src/commands/fs.test.ts b/src/commands/fs.test.ts
index f16a4131..d11c3d79 100644
--- a/src/commands/fs.test.ts
+++ b/src/commands/fs.test.ts
@@ -8,7 +8,7 @@ vi.mock("@tauri-apps/api/core", () => ({
invoke: mocks.invoke,
}))
-import { createDirectory, writeFile, writeFileAtomic } from "./fs"
+import { createDirectory, listDirectory, writeFile, writeFileAtomic } from "./fs"
describe("fs command path guards", () => {
beforeEach(() => {
@@ -47,4 +47,140 @@ describe("fs command path guards", () => {
contents: "content",
})
})
+
+ it("deduplicates matching in-flight listDirectory requests only while pending", async () => {
+ const tree = [{
+ name: "wiki",
+ path: "/tmp/project/wiki",
+ is_dir: true,
+ children: [{ name: "page.md", path: "/tmp/project/wiki/page.md", is_dir: false }],
+ }]
+ let resolveTree: (value: typeof tree) => void = () => {}
+ mocks.invoke.mockImplementationOnce(
+ () => new Promise<typeof tree>((resolve) => {
+ resolveTree = resolve
+ }),
+ )
+
+ const first = listDirectory("/tmp/project", { maxDepth: 2 })
+ const second = listDirectory("/tmp/project", { maxDepth: 2 })
+
+ expect(mocks.invoke).toHaveBeenCalledTimes(1)
+ expect(mocks.invoke).toHaveBeenCalledWith("list_directory", {
+ path: "/tmp/project",
+ includeHidden: false,
+ maxDepth: 2,
+ })
+
+ resolveTree(tree)
+ const [firstTree, secondTree] = await Promise.all([first, second])
+ expect(firstTree).toEqual(tree)
+ expect(secondTree).toEqual(tree)
+ expect(firstTree).not.toBe(secondTree)
+ expect(firstTree[0]).not.toBe(secondTree[0])
+ expect(firstTree[0].children?.[0]).not.toBe(secondTree[0].children?.[0])
+ secondTree[0].name = "mutated"
+ secondTree[0].children![0].name = "mutated-child.md"
+ expect(firstTree[0].name).toBe("wiki")
+ expect(firstTree[0].children?.[0]?.name).toBe("page.md")
+
+ mocks.invoke.mockResolvedValueOnce([])
+ await listDirectory("/tmp/project", { maxDepth: 2 })
+
+ expect(mocks.invoke).toHaveBeenCalledTimes(2)
+ })
+
+ it("does not clone single-caller listDirectory results", async () => {
+ const tree = [{ name: "wiki", path: "/tmp/project/wiki", is_dir: true }]
+ mocks.invoke.mockResolvedValueOnce(tree)
+
+ await expect(listDirectory("/tmp/project")).resolves.toBe(tree)
+ })
+
+ it("does not deduplicate listDirectory requests with different options", async () => {
+ mocks.invoke.mockResolvedValue([])
+
+ await Promise.all([
+ listDirectory("/tmp/project", { maxDepth: 2 }),
+ listDirectory("/tmp/project", { maxDepth: 3 }),
+ ])
+
+ expect(mocks.invoke).toHaveBeenCalledTimes(2)
+ })
+
+ it("does not deduplicate listDirectory requests with different paths", async () => {
+ mocks.invoke
+ .mockResolvedValueOnce([{ name: "a", path: "/tmp/a", is_dir: true }])
+ .mockResolvedValueOnce([{ name: "b", path: "/tmp/b", is_dir: true }])
+
+ const [first, second] = await Promise.all([
+ listDirectory("/tmp/a"),
+ listDirectory("/tmp/b"),
+ ])
+
+ expect(mocks.invoke).toHaveBeenCalledTimes(2)
+ expect(first).not.toBe(second)
+ expect(first).toEqual([{ name: "a", path: "/tmp/a", is_dir: true }])
+ expect(second).toEqual([{ name: "b", path: "/tmp/b", is_dir: true }])
+ })
+
+ it("does not deduplicate listDirectory requests with different hidden-entry options", async () => {
+ mocks.invoke.mockResolvedValue([])
+
+ await Promise.all([
+ listDirectory("/tmp/project", false),
+ listDirectory("/tmp/project", true),
+ ])
+
+ expect(mocks.invoke).toHaveBeenCalledTimes(2)
+ })
+
+ it("deduplicates boolean and object includeHidden overloads with the same value", async () => {
+ mocks.invoke.mockResolvedValue([])
+
+ await Promise.all([
+ listDirectory("/tmp/project", true),
+ listDirectory("/tmp/project", { includeHidden: true }),
+ ])
+
+ expect(mocks.invoke).toHaveBeenCalledTimes(1)
+ })
+
+ it("deduplicates default and explicit false includeHidden options", async () => {
+ mocks.invoke.mockResolvedValue([])
+
+ await Promise.all([
+ listDirectory("/tmp/project"),
+ listDirectory("/tmp/project", { includeHidden: false }),
+ ])
+
+ expect(mocks.invoke).toHaveBeenCalledTimes(1)
+ })
+
+ it("clears rejected in-flight listDirectory requests so later calls can retry", async () => {
+ let rejectTree: (reason: Error) => void = () => {}
+ mocks.invoke.mockImplementationOnce(
+ () => new Promise((_, reject) => {
+ rejectTree = reject
+ }),
+ )
+
+ const first = listDirectory("/tmp/project")
+ const second = listDirectory("/tmp/project")
+
+ expect(mocks.invoke).toHaveBeenCalledTimes(1)
+
+ rejectTree(new Error("scan failed"))
+
+ const settled = await Promise.allSettled([first, second])
+ expect(settled).toEqual([
+ expect.objectContaining({ status: "rejected" }),
+ expect.objectContaining({ status: "rejected" }),
+ ])
+
+ mocks.invoke.mockResolvedValueOnce([])
+ await expect(listDirectory("/tmp/project")).resolves.toEqual([])
+
+ expect(mocks.invoke).toHaveBeenCalledTimes(2)
+ })
})
diff --git a/src/commands/fs.ts b/src/commands/fs.ts
index a5031f14..33de21eb 100644
--- a/src/commands/fs.ts
+++ b/src/commands/fs.ts
@@ -45,6 +45,23 @@ export interface ListDirectoryOptions {
maxDepth?: number
}
+// In-flight dedupe only: entries are removed when the request settles. Each
+// caller receives its own tree copy when a request is actually shared, so
+// accidental in-place mutations do not leak across concurrent waiters.
+interface PendingListDirectory {
+ request: Promise<FileNode[]>
+ shared: boolean
+}
+
+const pendingListDirectory = new Map<string, PendingListDirectory>()
+
+function cloneFileNodes(nodes: FileNode[]): FileNode[] {
+ return nodes.map((node) => ({
+ ...node,
+ children: node.children ? cloneFileNodes(node.children) : node.children,
+ }))
+}
+
export async function listDirectory(
path: string,
includeHiddenOrOptions: boolean | ListDirectoryOptions = false,
@@ -53,11 +70,25 @@ export async function listDirectory(
typeof includeHiddenOrOptions === "boolean"
? { includeHidden: includeHiddenOrOptions }
: includeHiddenOrOptions
- return invoke<FileNode[]>("list_directory", {
+ const includeHidden = options.includeHidden ?? false
+ const maxDepth = options.maxDepth
+ const requestKey = JSON.stringify([path, includeHidden, maxDepth ?? null])
+ const pending = pendingListDirectory.get(requestKey)
+ if (pending) {
+ pending.shared = true
+ return pending.request.then(cloneFileNodes)
+ }
+
+ const request = invoke<FileNode[]>("list_directory", {
path,
- includeHidden: options.includeHidden,
- maxDepth: options.maxDepth,
+ includeHidden,
+ maxDepth,
+ }).finally(() => {
+ pendingListDirectory.delete(requestKey)
})
+ const entry: PendingListDirectory = { request, shared: false }
+ pendingListDirectory.set(requestKey, entry)
+ return request.then((nodes) => (entry.shared ? cloneFileNodes(nodes) : nodes))
}
export async function copyFile(