Skip to content

[REFACTORING] Project Scanning Performance #530

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
stephanj opened this issue Feb 26, 2025 · 0 comments
Open

[REFACTORING] Project Scanning Performance #530

stephanj opened this issue Feb 26, 2025 · 0 comments
Labels
Nice to have Further information is requested
Milestone

Comments

@stephanj
Copy link
Collaborator

The ProjectScannerService recursively scans project files but doesn't optimize for large projects.

Example Problem:

private void walkThroughDirectory(@NotNull VirtualFile directory,
                               @NotNull ProjectFileIndex fileIndex,
                               @NotNull StringBuilder fullContent,
                               @NotNull ScanContentResult scanContentResult) {
    VfsUtilCore.visitChildrenRecursively(directory, new VirtualFileVisitor<Void>() {
        @Override
        public boolean visitFile(@NotNull VirtualFile file) {
            // Process each file
            if (!file.isDirectory()) {
                readFileContent(file, fullContent, scanContentResult);
            }
            return true;
        }
    });
}

Recommendation:
Implement early termination based on token limits and more efficient traversal:

private void walkThroughDirectory(@NotNull VirtualFile directory,
                                @NotNull ProjectFileIndex fileIndex,
                                @NotNull StringBuilder fullContent,
                                @NotNull ScanContentResult scanContentResult,
                                int maxTokenLimit) {
    
    AtomicInteger tokenCount = new AtomicInteger(0);
    Queue<VirtualFile> directoriesToProcess = new LinkedList<>();
    directoriesToProcess.add(directory);
    
    while (!directoriesToProcess.isEmpty() && tokenCount.get() < maxTokenLimit) {
        VirtualFile currentDir = directoriesToProcess.poll();
        VirtualFile[] children = currentDir.getChildren();
        
        // Process directories first (breadth-first)
        for (VirtualFile child : children) {
            if (child.isDirectory() && !shouldExcludeDirectory(child)) {
                directoriesToProcess.add(child);
            }
        }
        
        // Process files
        for (VirtualFile child : children) {
            if (!child.isDirectory() && tokenCount.get() < maxTokenLimit) {
                if (shouldIncludeFile(child)) {
                    int fileTokens = readFileContent(child, fullContent, scanContentResult);
                    tokenCount.addAndGet(fileTokens);
                    
                    if (tokenCount.get() >= maxTokenLimit) {
                        break; // Stop if we've reached the token limit
                    }
                }
            }
        }
    }
}
@stephanj stephanj added the enhancement New feature or request label Feb 26, 2025
@stephanj stephanj added this to the v0.4.19 milestone Feb 26, 2025
@stephanj stephanj added Nice to have Further information is requested and removed enhancement New feature or request labels Feb 27, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Nice to have Further information is requested
Projects
None yet
Development

No branches or pull requests

1 participant