Skip to content

Commit 533e342

Browse files
SanderMullerclaude
andcommitted
Use xxh128 instead of sha256 for file content hashing
All hash_file() calls here are cache-invalidation keys, not security boundaries. xxh128 hashes a file in ~31 µs vs ~266 µs for sha256, which adds up across result-cache validation, source-locator scans, per-identifier reflection cache keys, and PHPDoc cache validation. Existing caches invalidate once (different hash values), then rebuild. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
1 parent ff2647a commit 533e342

8 files changed

Lines changed: 11 additions & 11 deletions

File tree

src/Analyser/ResultCache/ResultCacheManager.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1206,7 +1206,7 @@ private function getFileHash(string $path): string
12061206
return $this->fileHashes[$path];
12071207
}
12081208

1209-
$hash = hash_file('sha256', $path);
1209+
$hash = hash_file('xxh128', $path);
12101210
if ($hash === false) {
12111211
throw new CouldNotReadFileException($path);
12121212
}

src/Command/AnalyseApplication.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ public function analyse(
160160
continue;
161161
}
162162

163-
$newHash = hash_file('sha256', $file);
163+
$newHash = hash_file('xxh128', $file);
164164
if ($newHash === $hash) {
165165
continue;
166166
}

src/DependencyInjection/Configurator.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ public function loadContainer(): string
106106
array_keys($this->dynamicParameters),
107107
$this->configs,
108108
PHP_VERSION_ID - PHP_RELEASE_VERSION,
109-
is_file($attributesPhp) ? hash_file('sha256', $attributesPhp) : 'attributes-missing',
109+
is_file($attributesPhp) ? hash_file('xxh128', $attributesPhp) : 'attributes-missing',
110110
NeonAdapter::CACHE_KEY,
111111
$this->getAllConfigFilesHashes(),
112112
var_export(TurboExtensionEnabler::isLoaded(), true),
@@ -242,7 +242,7 @@ private function getAllConfigFilesHashes(): array
242242
{
243243
$hashes = [];
244244
foreach ($this->allConfigFiles as $file) {
245-
$hash = hash_file('sha256', $file);
245+
$hash = hash_file('xxh128', $file);
246246

247247
if ($hash === false) {
248248
throw new CouldNotReadFileException($file);

src/File/FileMonitor.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ public function getChanges(): FileMonitorResult
107107

108108
private function getFileHash(string $filePath): string
109109
{
110-
$hash = hash_file('sha256', $filePath);
110+
$hash = hash_file('xxh128', $filePath);
111111

112112
if ($hash === false) {
113113
throw new CouldNotReadFileException($filePath);

src/Reflection/BetterReflection/SourceLocator/OptimizedDirectorySourceLocator.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ public function __construct(
5151
*/
5252
private function getCacheKeys(string $file, Identifier $identifier): array
5353
{
54-
$fileHash = hash_file('sha256', $file);
54+
$fileHash = hash_file('xxh128', $file);
5555
if ($fileHash === false) {
5656
throw new CouldNotReadFileException($file);
5757
}

src/Reflection/BetterReflection/SourceLocator/OptimizedDirectorySourceLocatorFactory.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public function createByDirectory(string $directory): OptimizedDirectorySourceLo
3232
$files = $this->fileFinder->findFiles([$directory])->getFiles();
3333
$fileHashes = [];
3434
foreach ($files as $file) {
35-
$hash = hash_file('sha256', $file);
35+
$hash = hash_file('xxh128', $file);
3636
if ($hash === false) {
3737
continue;
3838
}
@@ -105,7 +105,7 @@ public function createByFiles(array $files, string $uniqueCacheIdentifier): Opti
105105
{
106106
$fileHashes = [];
107107
foreach ($files as $file) {
108-
$hash = hash_file('sha256', $file);
108+
$hash = hash_file('xxh128', $file);
109109
if ($hash === false) {
110110
continue;
111111
}

src/Reflection/BetterReflection/SourceLocator/OptimizedSingleFileSourceLocator.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ public function __construct(
4545

4646
private function getVariableCacheKey(string $file): string
4747
{
48-
$fileHash = hash_file('sha256', $file);
48+
$fileHash = hash_file('xxh128', $file);
4949
if ($fileHash === false) {
5050
throw new CouldNotReadFileException($file);
5151
}

src/Type/FileTypeMapper.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -344,7 +344,7 @@ private function getNameScopeMap(string $fileName): array
344344
[$nameScopeMap, $files] = $this->createPhpDocNodeMap($fileName, null, null, [], $fileName);
345345
$filesWithHashes = [];
346346
foreach ($files as $file) {
347-
$newHash = hash_file('sha256', $file);
347+
$newHash = hash_file('xxh128', $file);
348348
$filesWithHashes[$file] = $newHash;
349349
}
350350
$this->cache->save($cacheKey, $variableCacheKey, [$nameScopeMap, $filesWithHashes]);
@@ -381,7 +381,7 @@ private function loadCachedPhpDocNodeMap(string $cacheKey, string $variableCache
381381
[$nameScopeMap, $filesWithHashes] = $cached;
382382
$useCache = true;
383383
foreach ($filesWithHashes as $file => $hash) {
384-
$newHash = @hash_file('sha256', $file);
384+
$newHash = @hash_file('xxh128', $file);
385385
if ($newHash === false) {
386386
$useCache = false;
387387
break;

0 commit comments

Comments
 (0)