-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
Conversation
This breaks a test assertion |
5770220
to
dbdbc29
Compare
243c65c
to
797331b
Compare
There was a problem hiding this 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); |
There was a problem hiding this comment.
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.
…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
@GromNaN Can you rebase this PR? |
Thank you @GromNaN. |
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 ofinclude
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 thatsha256
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.