Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 6322c22

Browse files
committed
feature #52948 Use faster hashing algorithms when possible (javiereguiluz)
This PR was squashed before being merged into the 7.1 branch. Discussion ---------- Use faster hashing algorithms when possible | Q | A | ------------- | --- | Branch? | 7.1 | Bug fix? | no | New feature? | yes | Deprecations? | no | Issues | - | License | MIT `xxh128` is 60 times faster than `sha256` (source: https://php.watch/versions/8.1/xxHash) but it's not cryptographically secure. In previous versions we already used `xxh128` in some places. In this PR I propose to use it in all places where we don't need a cryptographically secure hash. Comments: * ~I also remove some `base64` encoding of binary hashes, etc. It looks convoluted and to me it looks unnecessary. But I can revert these changes if needed.~ **REVERTED** * There are some places where we can probably use `xxh128` but I'm not sure, so I didn't. These: * https://github.com/symfony/symfony/blob/7.1/src/Symfony/Component/HttpKernel/EventListener/CacheAttributeListener.php#L71 * https://github.com/symfony/symfony/blob/7.1/src/Symfony/Component/HttpKernel/HttpCache/Store.php#L421-L434 * https://github.com/symfony/symfony/blob/7.1/src/Symfony/Component/HttpKernel/HttpCache/Store.php#L421-L434 * And there's this use case, where a function explicitly tells that we're generating a sha256 hash, so I'm not sure if changing it is a BC break: https://github.com/symfony/symfony/blob/7.1/src/Symfony/Component/Form/ChoiceList/Factory/CachingFactoryDecorator.php#L42-L65 #SymfonyHackday Commits ------- 3d4d355 Use faster hashing algorithms when possible
2 parents e595c52 + 3d4d355 commit 6322c22

38 files changed

+114
-114
lines changed

src/Symfony/Bridge/Twig/NodeVisitor/TranslationDefaultDomainNodeVisitor.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,6 @@ private function isNamedArguments(Node $arguments): bool
114114

115115
private function getVarName(): string
116116
{
117-
return sprintf('__internal_%s', hash('sha256', uniqid(mt_rand(), true), false));
117+
return sprintf('__internal_%s', hash('xxh128', uniqid(mt_rand(), true)));
118118
}
119119
}

src/Symfony/Bundle/FrameworkBundle/EventListener/ConsoleProfilerListener.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ public function initialize(ConsoleCommandEvent $event): void
7777
return;
7878
}
7979

80-
$request->attributes->set('_stopwatch_token', substr(hash('sha256', uniqid(mt_rand(), true)), 0, 6));
80+
$request->attributes->set('_stopwatch_token', substr(hash('xxh128', uniqid(mt_rand(), true)), 0, 6));
8181
$this->stopwatch->openSection();
8282
}
8383

src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/FrameworkExtensionTestCase.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1693,7 +1693,7 @@ public function testCachePoolServices()
16931693
->replaceArgument(0, $expectedSeed)
16941694
->replaceArgument(1, 12),
16951695
(new ChildDefinition('cache.adapter.redis'))
1696-
->replaceArgument(0, new Reference('.cache_connection.kYdiLgf'))
1696+
->replaceArgument(0, new Reference('.cache_connection.U5HliuY'))
16971697
->replaceArgument(1, $expectedSeed)
16981698
->replaceArgument(2, 12),
16991699
],

src/Symfony/Bundle/SecurityBundle/Tests/DependencyInjection/CompleteConfigurationTestCase.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ public function testFirewalls()
137137
[
138138
'simple',
139139
'security.user_checker',
140-
'.security.request_matcher.h5ibf38',
140+
'.security.request_matcher.rud_2nr',
141141
false,
142142
false,
143143
'',
@@ -187,7 +187,7 @@ public function testFirewalls()
187187
[
188188
'host',
189189
'security.user_checker',
190-
'.security.request_matcher.bcmu4fb',
190+
'.security.request_matcher.ap9sh8g',
191191
true,
192192
false,
193193
'security.user.provider.concrete.default',

src/Symfony/Component/Cache/DependencyInjection/CachePoolPass.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,7 @@ public function process(ContainerBuilder $container): void
217217

218218
private function getNamespace(string $seed, string $id): string
219219
{
220-
return substr(str_replace('/', '-', base64_encode(hash('sha256', $id.$seed, true))), 0, 10);
220+
return substr(str_replace('/', '-', base64_encode(hash('xxh128', $id.$seed, true))), 0, 10);
221221
}
222222

223223
/**

src/Symfony/Component/Cache/Tests/DependencyInjection/CachePoolPassTest.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ public function testNamespaceArgumentIsReplaced()
4949

5050
$this->cachePoolPass->process($container);
5151

52-
$this->assertSame('z3X945Jbf5', $cachePool->getArgument(0));
52+
$this->assertSame('cKLcR15Llk', $cachePool->getArgument(0));
5353
}
5454

5555
public function testNamespaceArgumentIsSeededWithAdapterClassName()
@@ -70,7 +70,7 @@ public function testNamespaceArgumentIsSeededWithAdapterClassName()
7070

7171
$this->cachePoolPass->process($container);
7272

73-
$this->assertSame('xmOJ8gqF-Y', $cachePool->getArgument(0));
73+
$this->assertSame('mVXLns1cYU', $cachePool->getArgument(0));
7474
}
7575

7676
public function testNamespaceArgumentIsSeededWithAdapterClassNameWithoutAffectingOtherCachePools()
@@ -97,7 +97,7 @@ public function testNamespaceArgumentIsSeededWithAdapterClassNameWithoutAffectin
9797

9898
$this->cachePoolPass->process($container);
9999

100-
$this->assertSame('xmOJ8gqF-Y', $cachePool->getArgument(0));
100+
$this->assertSame('mVXLns1cYU', $cachePool->getArgument(0));
101101
}
102102

103103
public function testNamespaceArgumentIsNotReplacedIfArrayAdapterIsUsed()
@@ -153,7 +153,7 @@ public function testArgsAreReplaced()
153153

154154
$this->assertInstanceOf(Reference::class, $cachePool->getArgument(0));
155155
$this->assertSame('foobar', (string) $cachePool->getArgument(0));
156-
$this->assertSame('6Ridbw4aMn', $cachePool->getArgument(1));
156+
$this->assertSame('ZmalVIjCbI', $cachePool->getArgument(1));
157157
$this->assertSame(3, $cachePool->getArgument(2));
158158
}
159159

@@ -174,7 +174,7 @@ public function testWithNameAttribute()
174174

175175
$this->cachePoolPass->process($container);
176176

177-
$this->assertSame('PeXBWSl6ca', $cachePool->getArgument(1));
177+
$this->assertSame('5SvqAqqNBH', $cachePool->getArgument(1));
178178
}
179179

180180
public function testThrowsExceptionWhenCachePoolTagHasUnknownAttributes()

src/Symfony/Component/DependencyInjection/ContainerBuilder.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1592,7 +1592,7 @@ public static function getInitializedConditionals(mixed $value): array
15921592
*/
15931593
public static function hash(mixed $value): string
15941594
{
1595-
$hash = substr(base64_encode(hash('sha256', serialize($value), true)), 0, 7);
1595+
$hash = substr(base64_encode(hash('xxh128', serialize($value), true)), 0, 7);
15961596

15971597
return str_replace(['/', '+'], ['.', '_'], $hash);
15981598
}

src/Symfony/Component/DependencyInjection/LazyProxy/PhpDumper/LazyServiceDumper.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,6 @@ public function getProxyClass(Definition $definition, bool $asGhostObject, \Refl
146146

147147
return preg_replace('/^.*\\\\/', '', $definition->getClass())
148148
.($asGhostObject ? 'Ghost' : 'Proxy')
149-
.ucfirst(substr(hash('sha256', $this->salt.'+'.$class->name.'+'.serialize($definition->getTag('proxy'))), -7));
149+
.ucfirst(substr(hash('xxh128', $this->salt.'+'.$class->name.'+'.serialize($definition->getTag('proxy'))), -7));
150150
}
151151
}

src/Symfony/Component/DependencyInjection/Tests/Compiler/RegisterServiceSubscribersPassTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -462,7 +462,7 @@ public static function getSubscribedServices(): array
462462
'autowired' => new ServiceClosureArgument(new TypedReference('service.id', 'stdClass', ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE, 'autowired', [new Autowire(service: 'service.id')])),
463463
'autowired.nullable' => new ServiceClosureArgument(new TypedReference('service.id', 'stdClass', ContainerInterface::IGNORE_ON_INVALID_REFERENCE, 'autowired.nullable', [new Autowire(service: 'service.id')])),
464464
'autowired.parameter' => new ServiceClosureArgument('foobar'),
465-
'autowire.decorated' => new ServiceClosureArgument(new Reference('.service_locator.oO4rxCy.inner', ContainerInterface::NULL_ON_INVALID_REFERENCE)),
465+
'autowire.decorated' => new ServiceClosureArgument(new Reference('.service_locator.420ES7z.inner', ContainerInterface::NULL_ON_INVALID_REFERENCE)),
466466
'target' => new ServiceClosureArgument(new TypedReference('stdClass', 'stdClass', ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE, 'target', [new Target('someTarget')])),
467467
];
468468
$this->assertEquals($expected, $container->getDefinition((string) $locator->getFactory()[0])->getArgument(0));

src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/lazy_autowire_attribute.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,14 +78,14 @@ protected static function getFooService($container)
7878
protected static function getFoo2Service($container, $lazyLoad = true)
7979
{
8080
if (true === $lazyLoad) {
81-
return $container->privates['.lazy.Symfony\\Component\\DependencyInjection\\Tests\\Compiler\\Foo'] = $container->createProxy('FooProxy4048957', static fn () => \FooProxy4048957::createLazyProxy(static fn () => self::getFoo2Service($container, false)));
81+
return $container->privates['.lazy.Symfony\\Component\\DependencyInjection\\Tests\\Compiler\\Foo'] = $container->createProxy('FooProxyCd8d23a', static fn () => \FooProxyCd8d23a::createLazyProxy(static fn () => self::getFoo2Service($container, false)));
8282
}
8383

8484
return ($container->services['foo'] ??= new \Symfony\Component\DependencyInjection\Tests\Compiler\Foo());
8585
}
8686
}
8787

88-
class FooProxy4048957 extends \Symfony\Component\DependencyInjection\Tests\Compiler\Foo implements \Symfony\Component\VarExporter\LazyObjectInterface
88+
class FooProxyCd8d23a extends \Symfony\Component\DependencyInjection\Tests\Compiler\Foo implements \Symfony\Component\VarExporter\LazyObjectInterface
8989
{
9090
use \Symfony\Component\VarExporter\LazyProxyTrait;
9191

0 commit comments

Comments
 (0)