mirror of
https://github.com/stackblitz-labs/bolt.diy.git
synced 2025-06-18 01:49:07 +01:00
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.
This commit is contained in:
parent
9b4736921f
commit
443dc646fb
@ -249,8 +249,6 @@ export const ChatImpl = memo(
|
||||
});
|
||||
}, [messages, isLoading, parseMessages]);
|
||||
|
||||
console.log('messages', messages);
|
||||
|
||||
const scrollTextArea = () => {
|
||||
const textarea = textareaRef.current;
|
||||
|
||||
|
@ -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 <boltArtifact> format.
|
||||
|
||||
Available shell commands:
|
||||
File Operations:
|
||||
|
@ -181,29 +181,52 @@ export class FilesStore {
|
||||
}
|
||||
|
||||
const currentFiles = this.files.get();
|
||||
const pathsToDelete = new Set<string>();
|
||||
|
||||
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 });
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user