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

Skip to content

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

Merged
fabpot merged 1 commit into
twigphp:2.xfrom
GromNaN:hash-xxh128
Dec 16, 2021
Merged

Use faster hash algorithm (xxh128) on PHP 8.1+#3588
fabpot merged 1 commit into
twigphp:2.xfrom
GromNaN:hash-xxh128

Conversation

@GromNaN

@GromNaN GromNaN commented Nov 6, 2021

Copy link
Copy Markdown
Contributor

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

stof commented Nov 8, 2021

Copy link
Copy Markdown
Member

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
Comment thread tests/Fixtures/functions/template_from_string_error.test Outdated
@GromNaN
GromNaN force-pushed the hash-xxh128 branch 2 times, most recently from 243c65c to 797331b Compare November 8, 2021 16:24

@GromNaN GromNaN left a comment

Copy link
Copy Markdown
Contributor Author

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

Comment thread src/Compiler.php Outdated
Comment thread src/Parser.php Outdated
Comment thread src/Environment.php
$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);

@GromNaN GromNaN Nov 23, 2021

Copy link
Copy Markdown
Contributor Author

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

fabpot commented Dec 15, 2021

Copy link
Copy Markdown
Contributor

@GromNaN Can you rebase this PR?

@GromNaN
GromNaN changed the base branch from 3.x to 2.x December 15, 2021 05:27
@GromNaN

GromNaN commented Dec 15, 2021

Copy link
Copy Markdown
Contributor Author

@fabpot Rebased on 2.x after #3601.

@fabpot

fabpot commented Dec 16, 2021

Copy link
Copy Markdown
Contributor

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