From 443dc646fb88e09dd9a908b072cff3a9771d4ed7 Mon Sep 17 00:00:00 2001 From: KevIsDev Date: Sat, 19 Apr 2025 12:57:14 +0100 Subject: [PATCH] refactor(files): optimize file deletion logic for better performance Refactor the file deletion logic in FilesStore to precompute prefixes and iterate through files only once. This reduces the complexity of nested loops and improves performance by applying all deletions in a single update to the store. Additionally, remove a redundant console.log statement in Chat.client.tsx and update the prompts documentation for clarity. --- app/components/chat/Chat.client.tsx | 2 -- app/lib/common/prompts/prompts.ts | 4 ++- app/lib/stores/files.ts | 55 ++++++++++++++++++++--------- 3 files changed, 42 insertions(+), 19 deletions(-) diff --git a/app/components/chat/Chat.client.tsx b/app/components/chat/Chat.client.tsx index 2ddd358b..4f976837 100644 --- a/app/components/chat/Chat.client.tsx +++ b/app/components/chat/Chat.client.tsx @@ -249,8 +249,6 @@ export const ChatImpl = memo( }); }, [messages, isLoading, parseMessages]); - console.log('messages', messages); - const scrollTextArea = () => { const textarea = textareaRef.current; diff --git a/app/lib/common/prompts/prompts.ts b/app/lib/common/prompts/prompts.ts index 7fe4b599..c6263a7a 100644 --- a/app/lib/common/prompts/prompts.ts +++ b/app/lib/common/prompts/prompts.ts @@ -38,7 +38,9 @@ You are Bolt, an expert AI assistant and exceptional senior software developer w IMPORTANT: When choosing databases or npm packages, prefer options that don't rely on native binaries. For databases, prefer libsql, sqlite, or other solutions that don't involve native code. WebContainer CANNOT execute arbitrary native binaries. - IMPORTANT You must never use the "bundled" type for artifacts. + IMPORTANT You must never use the "bundled" type for artifacts, This is non-negotiable and used internally only. + + CRITICAL: You MUST always follow the format. Available shell commands: File Operations: diff --git a/app/lib/stores/files.ts b/app/lib/stores/files.ts index 2ec73a03..8355bb9b 100644 --- a/app/lib/stores/files.ts +++ b/app/lib/stores/files.ts @@ -181,29 +181,52 @@ export class FilesStore { } const currentFiles = this.files.get(); + const pathsToDelete = new Set(); - for (const deletedPath of this.#deletedPaths) { - if (currentFiles[deletedPath]) { - this.files.setKey(deletedPath, undefined); + // Precompute prefixes for efficient checking + const deletedPrefixes = [...this.#deletedPaths].map((p) => p + '/'); - if (currentFiles[deletedPath]?.type === 'file') { + // Iterate through all current files/folders once + for (const [path, dirent] of Object.entries(currentFiles)) { + // Skip if dirent is already undefined (shouldn't happen often but good practice) + if (!dirent) { + continue; + } + + // Check for exact match in deleted paths + if (this.#deletedPaths.has(path)) { + pathsToDelete.add(path); + continue; // No need to check prefixes if it's an exact match + } + + // Check if the path starts with any of the deleted folder prefixes + for (const prefix of deletedPrefixes) { + if (path.startsWith(prefix)) { + pathsToDelete.add(path); + break; // Found a match, no need to check other prefixes for this path + } + } + } + + // Perform the deletions and updates based on the collected paths + if (pathsToDelete.size > 0) { + const updates: FileMap = {}; + + for (const pathToDelete of pathsToDelete) { + const dirent = currentFiles[pathToDelete]; + updates[pathToDelete] = undefined; // Mark for deletion in the map update + + if (dirent?.type === 'file') { this.#size--; - } - } - for (const [path, dirent] of Object.entries(currentFiles)) { - if (path.startsWith(deletedPath + '/')) { - this.files.setKey(path, undefined); - - if (dirent?.type === 'file') { - this.#size--; - } - - if (dirent?.type === 'file' && this.#modifiedFiles.has(path)) { - this.#modifiedFiles.delete(path); + if (this.#modifiedFiles.has(pathToDelete)) { + this.#modifiedFiles.delete(pathToDelete); } } } + + // Apply all deletions to the store at once for potential efficiency + this.files.set({ ...currentFiles, ...updates }); } }