Skip to content

Commit

Permalink
[12.x] Replace md5 with much faster xxhash (#52301)
Browse files Browse the repository at this point in the history
* Replace md5 with much faster xxhash

* Update CacheTest.php

* Update RateLimited.php

* Update ThrottleRequests.php
  • Loading branch information
GrahamCampbell authored Aug 5, 2024
1 parent c1a21ab commit 87f9dd5
Show file tree
Hide file tree
Showing 7 changed files with 13 additions and 13 deletions.
2 changes: 1 addition & 1 deletion src/Illuminate/Foundation/Exceptions/Handler.php
Original file line number Diff line number Diff line change
Expand Up @@ -417,7 +417,7 @@ protected function shouldntReport(Throwable $e)
}

return ! $this->container->make(RateLimiter::class)->attempt(
with($throttle->key ?: 'illuminate:foundation:exceptions:'.$e::class, fn ($key) => $this->hashThrottleKeys ? md5($key) : $key),
with($throttle->key ?: 'illuminate:foundation:exceptions:'.$e::class, fn ($key) => $this->hashThrottleKeys ? hash('xxh128', $key) : $key),
$throttle->maxAttempts,
fn () => true,
$throttle->decaySeconds
Expand Down
2 changes: 1 addition & 1 deletion src/Illuminate/Http/Middleware/SetCacheHeaders.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public function handle($request, Closure $next, $options = [])
}

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

if (isset($options['last_modified'])) {
Expand Down
2 changes: 1 addition & 1 deletion src/Illuminate/Queue/Middleware/ThrottlesExceptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ protected function getKey($job)
return $this->prefix.$job->job->uuid();
}

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

/**
Expand Down
2 changes: 1 addition & 1 deletion src/Illuminate/Support/Onceable.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ protected static function hashFromTrace(array $trace, callable $callable)
$callable instanceof Closure ? (new ReflectionClosure($callable))->getClosureUsedVariables() : [],
);

return md5(sprintf(
return hash('xxh128', sprintf(
'%s@%s%s:%s (%s)',
$trace[0]['file'],
isset($trace[1]['class']) ? ($trace[1]['class'].'@') : '',
Expand Down
2 changes: 1 addition & 1 deletion src/Illuminate/View/Compilers/BladeCompiler.php
Original file line number Diff line number Diff line change
Expand Up @@ -810,7 +810,7 @@ public function getClassComponentAliases()
*/
public function anonymousComponentPath(string $path, ?string $prefix = null)
{
$prefixHash = md5($prefix ?: $path);
$prefixHash = hash('xxh128', $prefix ?: $path);

$this->anonymousComponentPaths[] = [
'path' => $path,
Expand Down
4 changes: 2 additions & 2 deletions tests/Http/Middleware/CacheTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ public function testGenerateEtag()
return new Response('some content');
}, 'etag;max_age=100;s_maxage=200');

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

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

$response = (new Cache)->handle($request, function () {
return new Response('some content');
Expand Down
12 changes: 6 additions & 6 deletions tests/View/Blade/BladeComponentTagCompilerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -598,22 +598,22 @@ public function testClasslessComponentsWithAnonymousComponentPath()
$app->shouldReceive('getNamespace')->once()->andReturn('App\\');

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

Container::setInstance($container);

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

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

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

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

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

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

Container::setInstance($container);

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

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

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

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

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

0 comments on commit 87f9dd5

Please sign in to comment.