Skip to content

Commit cb9b3e6

Browse files
committed
Keep normalized paths that did not change once in the cache
FileHelper's cache of normalized paths kept an array per original path, holding a second copy of the path - about 500 bytes per entry. Every file FileFinder finds goes through it, 6,653 paths for Sylius before analysis starts, which made it 3.2 MB. Keyed by the directory separator first, an entry is one hash bucket, and a path that normalizes to itself - nearly all of them - is the same string as its key: 0.3 MB for those 6,653 paths, and lookups are slightly faster. Amp-Thread-ID: https://ampcode.com/threads/T-01a1b250-2318-7374-8dd7-2834f59aa720
1 parent 6bba813 commit cb9b3e6

1 file changed

Lines changed: 14 additions & 3 deletions

File tree

‎src/File/FileHelper.php‎

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,13 @@
2424
final class FileHelper
2525
{
2626

27-
/** @var array<string, array<string, string>> */
27+
/**
28+
* Directory separator => original path => normalized path. A path that normalizes to itself,
29+
* which most do, is kept once - as the key and the value - so an entry costs little more than
30+
* its hash bucket.
31+
*
32+
* @var array<string, array<string, string>>
33+
*/
2834
private static array $normalizedPathsCache = [];
2935

3036
private string $workingDirectory;
@@ -63,7 +69,7 @@ public function absolutizePath(string $path): string
6369
/** @api */
6470
public function normalizePath(string $originalPath, string $directorySeparator = DIRECTORY_SEPARATOR): string
6571
{
66-
$cached = self::$normalizedPathsCache[$originalPath][$directorySeparator] ?? null;
72+
$cached = self::$normalizedPathsCache[$directorySeparator][$originalPath] ?? null;
6773
if ($cached !== null) {
6874
return $cached;
6975
}
@@ -109,7 +115,12 @@ public function normalizePath(string $originalPath, string $directorySeparator =
109115
}
110116
}
111117

112-
return self::$normalizedPathsCache[$originalPath][$directorySeparator] = ($scheme !== null ? $scheme . '://' : '') . $pathRoot . implode($directorySeparator, $normalizedPathParts);
118+
$normalizedPath = ($scheme !== null ? $scheme . '://' : '') . $pathRoot . implode($directorySeparator, $normalizedPathParts);
119+
if ($normalizedPath === $originalPath) {
120+
$normalizedPath = $originalPath;
121+
}
122+
123+
return self::$normalizedPathsCache[$directorySeparator][$originalPath] = $normalizedPath;
113124
}
114125

115126
public function normalizeSeparator(string $path): string

0 commit comments

Comments
 (0)