Skip to content

Commit 87f9dd5

Browse files
[12.x] Replace md5 with much faster xxhash (#52301)
* Replace md5 with much faster xxhash * Update CacheTest.php * Update RateLimited.php * Update ThrottleRequests.php
1 parent c1a21ab commit 87f9dd5

File tree

7 files changed

+13
-13
lines changed

7 files changed

+13
-13
lines changed

src/Illuminate/Foundation/Exceptions/Handler.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -417,7 +417,7 @@ protected function shouldntReport(Throwable $e)
417417
}
418418

419419
return ! $this->container->make(RateLimiter::class)->attempt(
420-
with($throttle->key ?: 'illuminate:foundation:exceptions:'.$e::class, fn ($key) => $this->hashThrottleKeys ? md5($key) : $key),
420+
with($throttle->key ?: 'illuminate:foundation:exceptions:'.$e::class, fn ($key) => $this->hashThrottleKeys ? hash('xxh128', $key) : $key),
421421
$throttle->maxAttempts,
422422
fn () => true,
423423
$throttle->decaySeconds

src/Illuminate/Http/Middleware/SetCacheHeaders.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ public function handle($request, Closure $next, $options = [])
6262
}
6363

6464
if (isset($options['etag']) && $options['etag'] === true) {
65-
$options['etag'] = $response->getEtag() ?? md5($response->getContent());
65+
$options['etag'] = $response->getEtag() ?? hash('xxh128', $response->getContent());
6666
}
6767

6868
if (isset($options['last_modified'])) {

src/Illuminate/Queue/Middleware/ThrottlesExceptions.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ protected function getKey($job)
171171
return $this->prefix.$job->job->uuid();
172172
}
173173

174-
return $this->prefix.md5(get_class($job));
174+
return $this->prefix.hash('xxh128', get_class($job));
175175
}
176176

177177
/**

src/Illuminate/Support/Onceable.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ protected static function hashFromTrace(array $trace, callable $callable)
6666
$callable instanceof Closure ? (new ReflectionClosure($callable))->getClosureUsedVariables() : [],
6767
);
6868

69-
return md5(sprintf(
69+
return hash('xxh128', sprintf(
7070
'%s@%s%s:%s (%s)',
7171
$trace[0]['file'],
7272
isset($trace[1]['class']) ? ($trace[1]['class'].'@') : '',

src/Illuminate/View/Compilers/BladeCompiler.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -810,7 +810,7 @@ public function getClassComponentAliases()
810810
*/
811811
public function anonymousComponentPath(string $path, ?string $prefix = null)
812812
{
813-
$prefixHash = md5($prefix ?: $path);
813+
$prefixHash = hash('xxh128', $prefix ?: $path);
814814

815815
$this->anonymousComponentPaths[] = [
816816
'path' => $path,

tests/Http/Middleware/CacheTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ public function testGenerateEtag()
108108
return new Response('some content');
109109
}, 'etag;max_age=100;s_maxage=200');
110110

111-
$this->assertSame('"9893532233caff98cd083a116b013c0b"', $response->getEtag());
111+
$this->assertSame('"4f1b32bff4356281946800d355007128"', $response->getEtag());
112112
$this->assertSame('max-age=100, public, s-maxage=200', $response->headers->get('Cache-Control'));
113113
}
114114

@@ -124,7 +124,7 @@ public function testDoesNotOverrideEtag()
124124
public function testIsNotModified()
125125
{
126126
$request = new Request;
127-
$request->headers->set('If-None-Match', '"9893532233caff98cd083a116b013c0b"');
127+
$request->headers->set('If-None-Match', '"4f1b32bff4356281946800d355007128"');
128128

129129
$response = (new Cache)->handle($request, function () {
130130
return new Response('some content');

tests/View/Blade/BladeComponentTagCompilerTest.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -598,22 +598,22 @@ public function testClasslessComponentsWithAnonymousComponentPath()
598598
$app->shouldReceive('getNamespace')->once()->andReturn('App\\');
599599

600600
$factory->shouldReceive('exists')->andReturnUsing(function ($arg) {
601-
return $arg === md5('test-directory').'::panel.index';
601+
return $arg === hash('xxh128', 'test-directory').'::panel.index';
602602
});
603603

604604
Container::setInstance($container);
605605

606606
$blade = m::mock(BladeCompiler::class)->makePartial();
607607

608608
$blade->shouldReceive('getAnonymousComponentPaths')->once()->andReturn([
609-
['path' => 'test-directory', 'prefix' => null, 'prefixHash' => md5('test-directory')],
609+
['path' => 'test-directory', 'prefix' => null, 'prefixHash' => hash('xxh128', 'test-directory')],
610610
]);
611611

612612
$compiler = $this->compiler([], [], $blade);
613613

614614
$result = $compiler->compileTags('<x-panel />');
615615

616-
$this->assertSame("##BEGIN-COMPONENT-CLASS##@component('Illuminate\View\AnonymousComponent', 'panel', ['view' => '".md5('test-directory')."::panel.index','data' => []])
616+
$this->assertSame("##BEGIN-COMPONENT-CLASS##@component('Illuminate\View\AnonymousComponent', 'panel', ['view' => '".hash('xxh128', 'test-directory')."::panel.index','data' => []])
617617
<?php if (isset(\$attributes) && \$attributes instanceof Illuminate\View\ComponentAttributeBag): ?>
618618
<?php \$attributes = \$attributes->except(\Illuminate\View\AnonymousComponent::ignoredParameterNames()); ?>
619619
<?php endif; ?>
@@ -631,22 +631,22 @@ public function testClasslessIndexComponentsWithAnonymousComponentPath()
631631
$app->shouldReceive('getNamespace')->once()->andReturn('App\\');
632632

633633
$factory->shouldReceive('exists')->andReturnUsing(function ($arg) {
634-
return $arg === md5('test-directory').'::panel';
634+
return $arg === hash('xxh128', 'test-directory').'::panel';
635635
});
636636

637637
Container::setInstance($container);
638638

639639
$blade = m::mock(BladeCompiler::class)->makePartial();
640640

641641
$blade->shouldReceive('getAnonymousComponentPaths')->once()->andReturn([
642-
['path' => 'test-directory', 'prefix' => null, 'prefixHash' => md5('test-directory')],
642+
['path' => 'test-directory', 'prefix' => null, 'prefixHash' => hash('xxh128', 'test-directory')],
643643
]);
644644

645645
$compiler = $this->compiler([], [], $blade);
646646

647647
$result = $compiler->compileTags('<x-panel />');
648648

649-
$this->assertSame("##BEGIN-COMPONENT-CLASS##@component('Illuminate\View\AnonymousComponent', 'panel', ['view' => '".md5('test-directory')."::panel','data' => []])
649+
$this->assertSame("##BEGIN-COMPONENT-CLASS##@component('Illuminate\View\AnonymousComponent', 'panel', ['view' => '".hash('xxh128', 'test-directory')."::panel','data' => []])
650650
<?php if (isset(\$attributes) && \$attributes instanceof Illuminate\View\ComponentAttributeBag): ?>
651651
<?php \$attributes = \$attributes->except(\Illuminate\View\AnonymousComponent::ignoredParameterNames()); ?>
652652
<?php endif; ?>

0 commit comments

Comments
 (0)