Skip to content

Commit 240b67c

Browse files
Copilotswissspidy
andcommitted
Add caching to avoid duplicate isPathIgnored() calls
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 <[email protected]>
1 parent e1f9c5c commit 240b67c

File tree

2 files changed

+24
-3
lines changed

2 files changed

+24
-3
lines changed

src/Dist_Archive_Command.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -508,7 +508,7 @@ private function get_file_list( $source_dir_path, $excluded = false ) {
508508
foreach ( $iterator as $item ) {
509509
$relative_filepath = str_replace( $source_dir_path, '', $item->getPathname() );
510510
try {
511-
if ( $this->checker->isPathIgnored( $relative_filepath ) ) {
511+
if ( $filter_iterator->isPathIgnoredCached( $relative_filepath ) ) {
512512
$excluded_files[] = $relative_filepath;
513513
} else {
514514
$included_files[] = $relative_filepath;

src/Distignore_Filter_Iterator.php

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,13 @@ class Distignore_Filter_Iterator extends RecursiveFilterIterator {
2222
*/
2323
private $source_dir_path;
2424

25+
/**
26+
* Cache for ignored status to avoid duplicate checks.
27+
*
28+
* @var array<string, bool>
29+
*/
30+
private $ignored_cache = [];
31+
2532
/**
2633
* Constructor.
2734
*
@@ -47,6 +54,20 @@ public function accept() {
4754
return true;
4855
}
4956

57+
/**
58+
* Check if a path is ignored, with caching to avoid duplicate checks.
59+
*
60+
* @param string $relative_filepath Relative file path to check.
61+
* @return bool True if the path is ignored, false otherwise.
62+
* @throws \Inmarelibero\GitIgnoreChecker\Exception\InvalidArgumentException
63+
*/
64+
public function isPathIgnoredCached( $relative_filepath ) {
65+
if ( ! isset( $this->ignored_cache[ $relative_filepath ] ) ) {
66+
$this->ignored_cache[ $relative_filepath ] = $this->checker->isPathIgnored( $relative_filepath );
67+
}
68+
return $this->ignored_cache[ $relative_filepath ];
69+
}
70+
5071
/**
5172
* Check whether the current element has children that should be recursed into.
5273
* We return false for certain ignored directories to prevent descending into them.
@@ -81,7 +102,7 @@ public function hasChildren() {
81102
}
82103

83104
try {
84-
$is_ignored = $this->checker->isPathIgnored( $relative_filepath );
105+
$is_ignored = $this->isPathIgnoredCached( $relative_filepath );
85106

86107
if ( ! $is_ignored ) {
87108
// Not ignored, so descend.
@@ -100,7 +121,7 @@ public function hasChildren() {
100121
// The actual name doesn't matter; we just need to verify the pattern applies to children.
101122
$test_child = $relative_filepath . '/test';
102123
try {
103-
$child_ignored = $this->checker->isPathIgnored( $test_child );
124+
$child_ignored = $this->isPathIgnoredCached( $test_child );
104125
if ( $child_ignored ) {
105126
// Child is also ignored, safe to skip descent.
106127
return false;

0 commit comments

Comments
 (0)