10000 Use faster hash algorithm (xxh128) on PHP 8.1+ by GromNaN · Pull Request #3588 · twigphp/Twig · GitHub
[go: up one dir, main page]

Skip to content

Use faster hash algorithm (xxh128) on PHP 8.1+ #3588

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 16, 2021

Conversation

GromNaN
Copy link
Contributor
@GromNaN GromNaN commented Nov 6, 2021

Twig uses the hash function at runtime to convert template paths class names, each time a template is loaded. In a large project with a high granularity of templates (hundreds of include per page), this can become the most time consuming function as reported by Blackfire:

To optimise this use-case, PHP 8.1 supports the xxHash hash algorithms. xxh128 is 60x faster that sha256 according to the benchmarks, and guarantee a very low risk of collision.

I'm not able to test with PHP 8.1 for now. Before going further I would like to validate performance impact on a large list of file names.

@stof
Copy link
Member
stof commented Nov 8, 2021

This breaks a test assertion

@GromNaN GromNaN force-pushed the hash-xxh128 branch 3 times, most recently from 5770220 to dbdbc29 Compare November 8, 2021 14:30
@GromNaN GromNaN force-pushed the hash-xxh128 branch 2 times, most recently from 243c65c to 797331b Compare November 8, 2021 16:24
Copy link
Contributor Author
@GromNaN GromNaN left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Having a different cache in function of the PHP version might be an issue if the cache is pregenerated in a different environment and packaged with the application. Cache key change with PHP version.

We can remove calls to the hash function is some places to get better performance improvements without cache variation. See #3601

@@ -350,7 +350,7 @@ public function getTemplateClass($name, $index = null)
{
$key = $this->getLoader()->getCacheKey($name).$this->optionsHash;

return $this->templateClassPrefix.hash('sha256', $key).(null === $index ? '' : '___'.$index);
return $this->templateClassPrefix.hash(\PHP_VERSION_ID < 80100 ? 'sha256' : 'xxh128', $key).(null === $index ? '' : '___'.$index);
Copy link
Contributor Author
@GromNaN GromNaN Nov 23, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The class name is hashed twice at runtime, for every template load. Once in FilesystemCache::generateKey and the second here.

fabpot added a commit that referenced this pull request Dec 14, 2021
…s during compilation (GromNaN)

This PR was squashed before being merged into the 2.x branch.

Discussion
----------

Hashing is not necessary to generate unique variable names during compilation

Trying to optimize calls to `hash` function (#3588), I found that some calls are made during compilation that are not necessary at all.

According to the last commits on this functions (57ff882 & 6ab5fe9), the internal variable names must be unique and deterministic. Using a simple sequence is enough.

This avoid CPU cycles during compilation. Which should not have any impact on production; but still interesting for dev&test.

Commits
-------

ba2b4e6 Hashing is not necessary to generate unique variable names during compilation
@fabpot
Copy link
Contributor
fabpot commented Dec 15, 2021

@GromNaN Can you rebase this PR?

@GromNaN GromNaN changed the base branch from 3.x to 2.x December 15, 2021 05:27
@GromNaN
Copy link
Contributor Author
GromNaN commented Dec 15, 2021

@fabpot Rebased on 2.x after #3601.

@fabpot
Copy link
Contributor
fabpot commented Dec 16, 2021

Thank you @GromNaN.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

3 participants
0