Optimize dist-archive by preventing descent into ignored directories#116
Merged
swissspidy merged 18 commits intomainfrom Feb 15, 2026
Merged
Conversation
Co-authored-by: swissspidy <841956+swissspidy@users.noreply.github.com>
Updated the RecursiveFilterIterator to use hasChildren() instead of accept() to prevent descending into ignored directories. This ensures: 1. Ignored directories are still yielded (needed for exclude lists in targz format) 2. We don't descend into them (performance optimization) 3. All existing tests pass Co-authored-by: swissspidy <841956+swissspidy@users.noreply.github.com>
Updated hasChildren() to be more conservative when skipping descent: - Only skip for top-level ignored directories where children would also be ignored - This handles complex patterns like "frontend/*" with "!/frontend/build/" - Still provides performance benefit for common cases like node_modules and .git - All 27 distignore test scenarios now pass Co-authored-by: swissspidy <841956+swissspidy@users.noreply.github.com>
- Use substr() instead of str_replace() for path manipulation to avoid edge cases - Add comment explaining the use of 'test' as a probe filename - Replace Bash-specific syntax with portable POSIX shell commands in tests - Fix code style (trailing whitespace) Co-authored-by: swissspidy <841956+swissspidy@users.noreply.github.com>
Copilot
AI
changed the title
[WIP] Fix performance issue with RecursiveIteratorIterator in file scanning
Optimize dist-archive by preventing descent into ignored directories
Dec 23, 2025
Codecov Report❌ Patch coverage is
📢 Thoughts on this report? Let us know! |
This comment was marked as resolved.
This comment was marked as resolved.
Introduced an ignored_cache array in Distignore_Filter_Iterator to cache the results of isPathIgnored() checks. This eliminates duplicate checks: - Once in hasChildren() when deciding whether to descend into directories - Once in get_file_list() when categorizing files/directories The cache is shared between both methods via the new isPathIgnoredCached() method, reducing redundant gitignore pattern matching for directories. Co-authored-by: swissspidy <841956+swissspidy@users.noreply.github.com>
This comment was marked as resolved.
This comment was marked as resolved.
This comment was marked as resolved.
This comment was marked as resolved.
Moved the filtering logic from get_file_list() into the iterator's accept() method as suggested. This provides a more profound optimization: - accept() now filters out ignored FILES immediately (not yielded) - Ignored DIRECTORIES are still yielded (for exclude tracking) but won't be descended into per hasChildren() - get_file_list() loop now only processes items that pass the filter, significantly reducing iterations - Excluded files are tracked in the iterator and available via getExcludedFiles() Performance: Iterator now yields only ~5 items instead of 200+ for a directory with 200 ignored files, while maintaining correct behavior for complex gitignore patterns with negations. Co-authored-by: swissspidy <841956+swissspidy@users.noreply.github.com>
This comment was marked as resolved.
This comment was marked as resolved.
This comment was marked as resolved.
This comment was marked as resolved.
Created 9 test methods covering all major functionality: - test_filters_ignored_files: Verifies ignored files are not yielded - test_tracks_ignored_directories: Ensures directories are tracked but files inside are filtered - test_get_excluded_files: Tests getExcludedFiles() method - test_caching_avoids_duplicate_checks: Verifies caching behavior - test_has_children_prevents_descent: Tests that hasChildren() prevents descent into ignored dirs - test_negation_patterns: Validates complex negation patterns work correctly - test_get_error_for_item_returns_null: Tests error handling - test_nested_directory_filtering: Ensures multi-level directories are handled - test_children_share_state: Verifies child iterators share cache and excluded files All 9 tests pass with 24 assertions. Tests include setup/teardown with temporary directories. Co-authored-by: swissspidy <841956+swissspidy@users.noreply.github.com>
This comment was marked as resolved.
This comment was marked as resolved.
This comment was marked as resolved.
This comment was marked as resolved.
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
swissspidy
reviewed
Jan 20, 2026
swissspidy
reviewed
Jan 20, 2026
Extracted the duplicated relative file path calculation logic from accept() and hasChildren() methods into a private helper method getRelativeFilePath(). This improves maintainability and reduces the risk of inconsistencies between the two implementations. Co-authored-by: swissspidy <841956+swissspidy@users.noreply.github.com>
Replaced str_replace() with strpos() and substr() when calculating relative file paths in get_file_list(). This prevents incorrect paths when the source directory name appears multiple times in the path (e.g., /tmp/proj/src/tmp/proj/file.php). Now uses the same robust logic as Distignore_Filter_Iterator. Co-authored-by: swissspidy <841956+swissspidy@users.noreply.github.com>
This comment was marked as resolved.
This comment was marked as resolved.
Added visited_paths tracking to prevent infinite recursion when symlinks create cycles. The iterator now: - Tracks real paths that have been visited - Prevents descending into symlinks that point to already-visited directories - Prevents descending into symlinks that point to parent directories - Shares visited_paths across child iterators to maintain global state This fixes the test failure with recursive symlinks where a directory links back to itself or a parent, which would cause infinite recursion and create extremely long paths. Co-authored-by: swissspidy <841956+swissspidy@users.noreply.github.com>
This comment was marked as resolved.
This comment was marked as resolved.
swissspidy
approved these changes
Jan 22, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Performance Optimization Complete ✅
RecursiveFilterIteratorclass to filter out ignored directories before iterationget_file_list()to use the filter iterator to prevent descending into ignored directoriesSummary
This PR fixes a critical performance issue where
RecursiveIteratorIteratorwas scanning ALL files in ignored directories before filtering them out. For projects with largenode_modulesdirectories (30,000+ files), this made the command nearly unusable.Solution: Introduced a
RecursiveFilterIteratorthat filters ignored files directly in theaccept()method, preventing them from being yielded to the foreach loop. The iterator:Performance Impact:
Changes:
Distignore_Filter_Iteratorwith smart filtering in accept() methodget_file_list()to leverage iterator's filtering (reduced logic)str_replacewith robuststrpos/substrfor path calculationsTest Coverage:
Compatibility: All 27 existing Behat test scenarios pass, plus new performance test added. All 9 PHPUnit tests pass. Symlink protection prevents infinite loops when directories link back to themselves or parent directories.
Original prompt
✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.