Skip to content

Commit

Permalink
[TASK] Use xxh3 instead of sha1
Browse files Browse the repository at this point in the history
64bit xxh3 hash is significantly quicker. This is a
micro optimization especially when creating hashes
from template contents for caching.
  • Loading branch information
lolli42 committed May 22, 2024
1 parent 389ac38 commit d94cccb
Show file tree
Hide file tree
Showing 6 changed files with 9 additions and 13 deletions.
2 changes: 1 addition & 1 deletion examples/src/CustomVariableProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class CustomVariableProvider extends StandardVariableProvider implements Variabl
public function getByPath($path)
{
if ($path === 'random') {
return 'random' . sha1(rand(0, 999999999));
return 'random' . hash('xxh3', (string)rand(0, 999999999));
}
if ($path === 'incrementer') {
return ++$this->incrementer;
Expand Down
8 changes: 2 additions & 6 deletions src/Core/Compiler/TemplateCompiler.php
Original file line number Diff line number Diff line change
Expand Up @@ -234,11 +234,7 @@ protected function generateCodeForLayoutName($storedLayoutNameArgument): string
return 'return (string)\'' . $storedLayoutNameArgument . '\'';
}

/**
* @param ParsingState $parsingState
* @return string
*/
protected function generateSectionCodeFromParsingState(ParsingState $parsingState)
protected function generateSectionCodeFromParsingState(ParsingState $parsingState): string
{
$generatedRenderFunctions = '';
if ($parsingState->getVariableContainer()->exists('1457379500_sections')) {
Expand All @@ -249,7 +245,7 @@ protected function generateSectionCodeFromParsingState(ParsingState $parsingStat
// @todo: Verify this is *always* an instance of RootNode
// and call $node->convert($this) directly.
$this->convertSubNodes($sectionRootNode->getChildNodes()),
'section_' . sha1($sectionName),
'section_' . hash('xxh3', $sectionName),
'section ' . $sectionName,
);
}
Expand Down
2 changes: 1 addition & 1 deletion src/View/AbstractTemplateView.php
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ public function renderSection($sectionName, array $variables = [], $ignoreUnknow
}

if ($parsedTemplate->isCompiled()) {
$methodNameOfSection = 'section_' . sha1($sectionName);
$methodNameOfSection = 'section_' . hash('xxh3', (string)$sectionName);
if (!method_exists($parsedTemplate, $methodNameOfSection)) {
if ($ignoreUnknown) {
return '';
Expand Down
6 changes: 3 additions & 3 deletions src/View/TemplatePaths.php
Original file line number Diff line number Diff line change
Expand Up @@ -556,7 +556,7 @@ public function getLayoutSource($layoutName = 'Default')
public function getTemplateIdentifier($controller = 'Default', $action = 'Default')
{
if ($this->templateSource !== null) {
return 'source_' . sha1($this->templateSource) . '_' . $controller . '_' . $action . '_' . $this->getFormat();
return 'source_' . hash('xxh3', (string)$this->templateSource) . '_' . $controller . '_' . $action . '_' . $this->getFormat();
}
$templatePathAndFilename = $this->resolveTemplateFileForControllerAndActionAndFormat($controller, $action);
$prefix = ltrim($controller . '_action_' . $action, '_');
Expand Down Expand Up @@ -610,7 +610,7 @@ public function getTemplateSource($controller = 'Default', $action = 'Default')

/**
* Returns a unique identifier for the given file in the format
* <PackageKey>_<SubPackageKey>_<ControllerName>_<prefix>_<SHA1>
* <PackageKey>_<SubPackageKey>_<ControllerName>_<prefix>_<hash>
* The SH1 hash is a checksum that is based on the file path and last modification date
*
* @param string|null $pathAndFilename
Expand All @@ -621,7 +621,7 @@ protected function createIdentifierForFile($pathAndFilename, $prefix)
{
$pathAndFilename = (string)$pathAndFilename;
$templateModifiedTimestamp = $pathAndFilename !== 'php://stdin' && file_exists($pathAndFilename) ? filemtime($pathAndFilename) : 0;
return sprintf('%s_%s', $prefix, sha1($pathAndFilename . '|' . $templateModifiedTimestamp));
return sprintf('%s_%s', $prefix, hash('xxh3', $pathAndFilename . '|' . $templateModifiedTimestamp));
}

/**
Expand Down
2 changes: 1 addition & 1 deletion tests/Functional/AbstractFunctionalTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ abstract class AbstractFunctionalTestCase extends BaseTestCase

public static function setUpBeforeClass(): void
{
self::$cachePath = sys_get_temp_dir() . '/' . 'fluid-functional-tests-' . sha1(__CLASS__);
self::$cachePath = sys_get_temp_dir() . '/' . 'fluid-functional-tests-' . hash('xxh3', __CLASS__);
mkdir(self::$cachePath);
self::$cache = (new SimpleFileCache(self::$cachePath));
}
Expand Down
2 changes: 1 addition & 1 deletion tests/Unit/View/TemplatePathsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ public function testGetTemplateIdentifierReturnsSourceChecksumWithControllerAndA
{
$instance = new TemplatePaths();
$instance->setTemplateSource('foobar');
self::assertSame('source_8843d7f92416211de9ebb963ff4ce28125932878_DummyController_dummyAction_html', $instance->getTemplateIdentifier('DummyController', 'dummyAction'));
self::assertSame('source_d78fda63144c5c84_DummyController_dummyAction_html', $instance->getTemplateIdentifier('DummyController', 'dummyAction'));
}

/**
Expand Down

0 comments on commit d94cccb

Please sign in to comment.