Skip to content

Commit 1997729

Browse files
Use faster hash algorithm (xxh128) (#45371)
1 parent b66d98a commit 1997729

File tree

7 files changed

+25
-25
lines changed

7 files changed

+25
-25
lines changed

src/Illuminate/View/Compilers/Compiler.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ public function __construct(
8181
*/
8282
public function getCompiledPath($path)
8383
{
84-
return $this->cachePath.'/'.sha1('v2'.Str::after($path, $this->basePath)).'.'.$this->compiledExtension;
84+
return $this->cachePath.'/'.hash('xxh128', 'v2'.Str::after($path, $this->basePath)).'.'.$this->compiledExtension;
8585
}
8686

8787
/**

src/Illuminate/View/Compilers/Concerns/CompilesComponents.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ protected function compileComponent($expression)
4646
*/
4747
public static function newComponentHash(string $component)
4848
{
49-
static::$componentHashStack[] = $hash = sha1($component);
49+
static::$componentHashStack[] = $hash = hash('xxh128', $component);
5050

5151
return $hash;
5252
}

src/Illuminate/View/Component.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ protected function createBladeViewFromString($factory, $contents)
187187
$directory = Container::getInstance()['config']->get('view.compiled')
188188
);
189189

190-
if (! is_file($viewFile = $directory.'/'.sha1($contents).'.blade.php')) {
190+
if (! is_file($viewFile = $directory.'/'.hash('xxh128', $contents).'.blade.php')) {
191191
if (! is_dir($directory)) {
192192
mkdir($directory, 0755, true);
193193
}

src/Illuminate/View/Concerns/ManagesLayouts.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ public static function parentPlaceholder($section = '')
178178
if (! isset(static::$parentPlaceholder[$section])) {
179179
$salt = static::parentPlaceholderSalt();
180180

181-
static::$parentPlaceholder[$section] = '##parent-placeholder-'.sha1($salt.$section).'##';
181+
static::$parentPlaceholder[$section] = '##parent-placeholder-'.hash('xxh128', $salt.$section).'##';
182182
}
183183

184184
return static::$parentPlaceholder[$section];

tests/View/Blade/BladeComponentsTest.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public function testComponentsAreCompiled()
1616

1717
public function testClassComponentsAreCompiled()
1818
{
19-
$this->assertSame('<?php if (isset($component)) { $__componentOriginal32877a641c21ac6579f6376333c8770674a6058f = $component; } ?>
19+
$this->assertSame('<?php if (isset($component)) { $__componentOriginal2dda3d2f2f9b76bd400bf03f0b84e87f = $component; } ?>
2020
<?php $component = Illuminate\Tests\View\Blade\ComponentStub::class::resolve(["foo" => "bar"] + (isset($attributes) && $attributes instanceof Illuminate\View\ComponentAttributeBag ? (array) $attributes->getIterator() : [])); ?>
2121
<?php $component->withName(\'test\'); ?>
2222
<?php if ($component->shouldRender()): ?>
@@ -36,9 +36,9 @@ public function testEndComponentClassesAreCompiled()
3636

3737
$this->assertSame('<?php echo $__env->renderComponent(); ?>
3838
<?php endif; ?>
39-
<?php if (isset($__componentOriginal0beec7b5ea3f0fdbc95d0dd47f3c5bc275da8a33)): ?>
40-
<?php $component = $__componentOriginal0beec7b5ea3f0fdbc95d0dd47f3c5bc275da8a33; ?>
41-
<?php unset($__componentOriginal0beec7b5ea3f0fdbc95d0dd47f3c5bc275da8a33); ?>
39+
<?php if (isset($__componentOriginal79aef92e83454121ab6e5f64077e7d8a)): ?>
40+
<?php $component = $__componentOriginal79aef92e83454121ab6e5f64077e7d8a; ?>
41+
<?php unset($__componentOriginal79aef92e83454121ab6e5f64077e7d8a); ?>
4242
<?php endif; ?>', $this->compiler->compileString('@endcomponentClass'));
4343
}
4444

tests/View/ComponentTest.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ public function testInlineViewsGetCreated()
5959
$this->viewFactory->shouldReceive('addNamespace')->once()->with('__components', '/tmp');
6060

6161
$component = new TestInlineViewComponent;
62-
$this->assertSame('__components::c6327913fef3fca4518bcd7df1d0ff630758e241', $component->resolveView());
62+
$this->assertSame('__components::57b7a54afa0eb51fd9b88eec031c9e9e', $component->resolveView());
6363
}
6464

6565
public function testRegularViewsGetReturnedUsingViewHelper()
@@ -191,7 +191,7 @@ public function testBladeViewCacheWithInlineViewComponent()
191191
->with('__components', '/tmp')
192192
->twice();
193193

194-
$compiledViewName = '__components::c6327913fef3fca4518bcd7df1d0ff630758e241';
194+
$compiledViewName = '__components::57b7a54afa0eb51fd9b88eec031c9e9e';
195195
$contents = '::Hello {{ $title }}';
196196
$cacheKey = $component::class.$contents;
197197

@@ -227,8 +227,8 @@ public function testBladeViewCacheWithInlineViewComponentWhereRenderDependsOnPro
227227
->with('__components', '/tmp')
228228
->twice();
229229

230-
$compiledViewNameA = '__components::6dcd4ce23d88e2ee9568ba546c007c63d9131c1b';
231-
$compiledViewNameB = '__components::ae4f281df5a5d0ff3cad6371f76d5c29b6d953ec';
230+
$compiledViewNameA = '__components::9b0498cbe3839becd0d496e05c553485';
231+
$compiledViewNameB = '__components::9d1b9bc4078a3e7274d3766ca02423f3';
232232
$cacheAKey = $componentA::class.'::A';
233233
$cacheBKey = $componentB::class.'::B';
234234

tests/View/ViewBladeCompilerTest.php

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ protected function tearDown(): void
1818
public function testIsExpiredReturnsTrueIfCompiledFileDoesntExist()
1919
{
2020
$compiler = new BladeCompiler($files = $this->getFiles(), __DIR__);
21-
$files->shouldReceive('exists')->once()->with(__DIR__.'/'.sha1('v2foo').'.php')->andReturn(false);
21+
$files->shouldReceive('exists')->once()->with(__DIR__.'/'.hash('xxh128', 'v2foo').'.php')->andReturn(false);
2222
$this->assertTrue($compiler->isExpired('foo'));
2323
}
2424

@@ -33,18 +33,18 @@ public function testCannotConstructWithBadCachePath()
3333
public function testIsExpiredReturnsTrueWhenModificationTimesWarrant()
3434
{
3535
$compiler = new BladeCompiler($files = $this->getFiles(), __DIR__);
36-
$files->shouldReceive('exists')->once()->with(__DIR__.'/'.sha1('v2foo').'.php')->andReturn(true);
36+
$files->shouldReceive('exists')->once()->with(__DIR__.'/'.hash('xxh128', 'v2foo').'.php')->andReturn(true);
3737
$files->shouldReceive('lastModified')->once()->with('foo')->andReturn(100);
38-
$files->shouldReceive('lastModified')->once()->with(__DIR__.'/'.sha1('v2foo').'.php')->andReturn(0);
38+
$files->shouldReceive('lastModified')->once()->with(__DIR__.'/'.hash('xxh128', 'v2foo').'.php')->andReturn(0);
3939
$this->assertTrue($compiler->isExpired('foo'));
4040
}
4141

4242
public function testIsExpiredReturnsFalseWhenUseCacheIsTrueAndNoFileModification()
4343
{
4444
$compiler = new BladeCompiler($files = $this->getFiles(), __DIR__);
45-
$files->shouldReceive('exists')->once()->with(__DIR__.'/'.sha1('v2foo').'.php')->andReturn(true);
45+
$files->shouldReceive('exists')->once()->with(__DIR__.'/'.hash('xxh128', 'v2foo').'.php')->andReturn(true);
4646
$files->shouldReceive('lastModified')->once()->with('foo')->andReturn(0);
47-
$files->shouldReceive('lastModified')->once()->with(__DIR__.'/'.sha1('v2foo').'.php')->andReturn(100);
47+
$files->shouldReceive('lastModified')->once()->with(__DIR__.'/'.hash('xxh128', 'v2foo').'.php')->andReturn(100);
4848
$this->assertFalse($compiler->isExpired('foo'));
4949
}
5050

@@ -57,15 +57,15 @@ public function testIsExpiredReturnsTrueWhenUseCacheIsFalse()
5757
public function testCompilePathIsProperlyCreated()
5858
{
5959
$compiler = new BladeCompiler($this->getFiles(), __DIR__);
60-
$this->assertEquals(__DIR__.'/'.sha1('v2foo').'.php', $compiler->getCompiledPath('foo'));
60+
$this->assertEquals(__DIR__.'/'.hash('xxh128', 'v2foo').'.php', $compiler->getCompiledPath('foo'));
6161
}
6262

6363
public function testCompileCompilesFileAndReturnsContents()
6464
{
6565
$compiler = new BladeCompiler($files = $this->getFiles(), __DIR__);
6666
$files->shouldReceive('get')->once()->with('foo')->andReturn('Hello World');
6767
$files->shouldReceive('exists')->once()->with(__DIR__)->andReturn(true);
68-
$files->shouldReceive('put')->once()->with(__DIR__.'/'.sha1('v2foo').'.php', 'Hello World<?php /**PATH foo ENDPATH**/ ?>');
68+
$files->shouldReceive('put')->once()->with(__DIR__.'/'.hash('xxh128', 'v2foo').'.php', 'Hello World<?php /**PATH foo ENDPATH**/ ?>');
6969
$compiler->compile('foo');
7070
}
7171

@@ -75,7 +75,7 @@ public function testCompileCompilesFileAndReturnsContentsCreatingDirectory()
7575
$files->shouldReceive('get')->once()->with('foo')->andReturn('Hello World');
7676
$files->shouldReceive('exists')->once()->with(__DIR__)->andReturn(false);
7777
$files->shouldReceive('makeDirectory')->once()->with(__DIR__, 0777, true, true);
78-
$files->shouldReceive('put')->once()->with(__DIR__.'/'.sha1('v2foo').'.php', 'Hello World<?php /**PATH foo ENDPATH**/ ?>');
78+
$files->shouldReceive('put')->once()->with(__DIR__.'/'.hash('xxh128', 'v2foo').'.php', 'Hello World<?php /**PATH foo ENDPATH**/ ?>');
7979
$compiler->compile('foo');
8080
}
8181

@@ -84,7 +84,7 @@ public function testCompileCompilesAndGetThePath()
8484
$compiler = new BladeCompiler($files = $this->getFiles(), __DIR__);
8585
$files->shouldReceive('get')->once()->with('foo')->andReturn('Hello World');
8686
$files->shouldReceive('exists')->once()->with(__DIR__)->andReturn(true);
87-
$files->shouldReceive('put')->once()->with(__DIR__.'/'.sha1('v2foo').'.php', 'Hello World<?php /**PATH foo ENDPATH**/ ?>');
87+
$files->shouldReceive('put')->once()->with(__DIR__.'/'.hash('xxh128', 'v2foo').'.php', 'Hello World<?php /**PATH foo ENDPATH**/ ?>');
8888
$compiler->compile('foo');
8989
$this->assertSame('foo', $compiler->getPath());
9090
}
@@ -101,7 +101,7 @@ public function testCompileWithPathSetBefore()
101101
$compiler = new BladeCompiler($files = $this->getFiles(), __DIR__);
102102
$files->shouldReceive('get')->once()->with('foo')->andReturn('Hello World');
103103
$files->shouldReceive('exists')->once()->with(__DIR__)->andReturn(true);
104-
$files->shouldReceive('put')->once()->with(__DIR__.'/'.sha1('v2foo').'.php', 'Hello World<?php /**PATH foo ENDPATH**/ ?>');
104+
$files->shouldReceive('put')->once()->with(__DIR__.'/'.hash('xxh128', 'v2foo').'.php', 'Hello World<?php /**PATH foo ENDPATH**/ ?>');
105105
// set path before compilation
106106
$compiler->setPath('foo');
107107
// trigger compilation with $path
@@ -132,7 +132,7 @@ public function testIncludePathToTemplate($content, $compiled)
132132
$compiler = new BladeCompiler($files = $this->getFiles(), __DIR__);
133133
$files->shouldReceive('get')->once()->with('foo')->andReturn($content);
134134
$files->shouldReceive('exists')->once()->with(__DIR__)->andReturn(true);
135-
$files->shouldReceive('put')->once()->with(__DIR__.'/'.sha1('v2foo').'.php', $compiled);
135+
$files->shouldReceive('put')->once()->with(__DIR__.'/'.hash('xxh128', 'v2foo').'.php', $compiled);
136136

137137
$compiler->compile('foo');
138138
}
@@ -187,7 +187,7 @@ public function testDontIncludeEmptyPath()
187187
$compiler = new BladeCompiler($files = $this->getFiles(), __DIR__);
188188
$files->shouldReceive('get')->once()->with('')->andReturn('Hello World');
189189
$files->shouldReceive('exists')->once()->with(__DIR__)->andReturn(true);
190-
$files->shouldReceive('put')->once()->with(__DIR__.'/'.sha1('v2').'.php', 'Hello World');
190+
$files->shouldReceive('put')->once()->with(__DIR__.'/'.hash('xxh128', 'v2').'.php', 'Hello World');
191191
$compiler->setPath('');
192192
$compiler->compile();
193193
}
@@ -197,7 +197,7 @@ public function testDontIncludeNullPath()
197197
$compiler = new BladeCompiler($files = $this->getFiles(), __DIR__);
198198
$files->shouldReceive('get')->once()->with(null)->andReturn('Hello World');
199199
$files->shouldReceive('exists')->once()->with(__DIR__)->andReturn(true);
200-
$files->shouldReceive('put')->once()->with(__DIR__.'/'.sha1('v2').'.php', 'Hello World');
200+
$files->shouldReceive('put')->once()->with(__DIR__.'/'.hash('xxh128', 'v2').'.php', 'Hello World');
201201
$compiler->setPath(null);
202202
$compiler->compile();
203203
}

0 commit comments

Comments
 (0)