Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .jules/bolt.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,7 @@
## 2026-03-20 - [Optimize directory listing with FilesystemIterator]
**Learning:** `scandir()` requires loading all file names into a large array at once, which consumes more memory. While PHP natively caches `stat()` calls, iterating with `scandir()` over large directories is still less efficient than lazy iteration.
**Action:** Use `FilesystemIterator` when iterating over directories. This reduces the memory footprint via lazy iteration. Note: Always wrap `SplFileInfo` methods like `getSize()` and `getMTime()` in a `try-catch` block, as they will throw a `RuntimeException` for unreadable files or broken symlinks.

## 2024-05-24 - [Optimize file extension checking in searchDirectory]
**Learning:** Inside a large loop (e.g., iterating through `RecursiveDirectoryIterator`), extracting file extensions with `pathinfo()` and checking them against a static array with `in_array()` adds significant overhead.
**Action:** Use `$file->getExtension()` directly from the `SplFileInfo` object, and use `isset()` with an `array_flip()` array for faster O(1) lookups instead of O(n) `in_array()` lookups.
11 changes: 7 additions & 4 deletions public/api/files.php
Original file line number Diff line number Diff line change
Expand Up @@ -327,26 +327,29 @@ function searchDirectory($path, $query) {

$results = [];
$ignoredDirs = ['node_modules', 'vendor', '__pycache__', '.git', '.idea', '.vscode', 'dist', 'build', 'generated'];
$ignoredDirsFlipped = array_flip($ignoredDirs);

// ⚡ Bolt: Replace userland FilterIterator with native RecursiveCallbackFilterIterator for faster traversal and lower memory usage
$dirIter = new RecursiveDirectoryIterator($path, RecursiveDirectoryIterator::SKIP_DOTS);
$filter = new RecursiveCallbackFilterIterator($dirIter, function ($current, $key, $iterator) use ($ignoredDirs) {
$filter = new RecursiveCallbackFilterIterator($dirIter, function ($current, $key, $iterator) use ($ignoredDirsFlipped) {
if ($iterator->hasChildren()) {
if (in_array($current->getFilename(), $ignoredDirs)) {
if (isset($ignoredDirsFlipped[$current->getFilename()])) {
return false;
}
}
return true;
});
$iterator = new RecursiveIteratorIterator($filter, RecursiveIteratorIterator::SELF_FIRST);
$ignoredExtsFlipped = array_flip(['png', 'jpg', 'jpeg', 'gif', 'webp', 'pdf', 'zip', 'tar', 'gz', 'exe', 'dll', 'so', 'sqlite', 'db']);

foreach ($iterator as $file) {
if ($file->isFile()) {
// simple binary check by size > 1MB
if ($file->getSize() > 1024 * 1024) continue;

$ext = strtolower(pathinfo($file->getFilename(), PATHINFO_EXTENSION));
if (in_array($ext, ['png', 'jpg', 'jpeg', 'gif', 'webp', 'pdf', 'zip', 'tar', 'gz', 'exe', 'dll', 'so', 'sqlite', 'db'])) continue;
// ⚡ Bolt: Use getExtension() and isset() with flipped array for faster file extension checking
$ext = strtolower($file->getExtension());
if (isset($ignoredExtsFlipped[$ext])) continue;

$filePath = $file->getRealPath();
$contents = @file_get_contents($filePath);
Expand Down