Use faster hash algorithm (xxh128) on PHP 8.1+ - #3588
Merged
Conversation
Member
|
This breaks a test assertion |
GromNaN
force-pushed
the
hash-xxh128
branch
3 times, most recently
from
November 8, 2021 14:30
5770220 to
dbdbc29
Compare
stof
reviewed
Nov 8, 2021
GromNaN
force-pushed
the
hash-xxh128
branch
2 times, most recently
from
November 8, 2021 16:24
243c65c to
797331b
Compare
GromNaN
commented
Nov 23, 2021
Contributor
Author
There was a problem hiding this comment.
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
| $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); |
Contributor
Author
There was a problem hiding this comment.
The class name is hashed twice at runtime, for every template load. Once in FilesystemCache::generateKey and the second here.
This was referenced Dec 13, 2021
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
Contributor
|
@GromNaN Can you rebase this PR? |
Contributor
Author
stof
approved these changes
Dec 15, 2021
Contributor
|
Thank you @GromNaN. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Twig uses the

hashfunction 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 ofincludeper 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.
xxh128is 60x faster thatsha256according 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.