Visitar URL original
Accelerate DJBX33A hash algorithm by "unoptimizing" it · Issue #115704 · python/cpython · GitHub
The Wayback Machine - https://web.archive.org/web/20241108145700/https://github.com/python/cpython/issues/115704
Skip to content
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

Accelerate DJBX33A hash algorithm by "unoptimizing" it #115704

Open
PeterYang12 opened this issue Feb 20, 2024 · 16 comments
Open

Accelerate DJBX33A hash algorithm by "unoptimizing" it #115704

PeterYang12 opened this issue Feb 20, 2024 · 16 comments
Labels
performance Performance or resource usage type-feature A feature request or enhancement

Comments

@PeterYang12
Copy link

PeterYang12 commented Feb 20, 2024 •

Feature or enhancement

Proposal:

Hello CPython team! 👋
I saw Daniel Lemire's blog post: https://lemire.me/blog/2016/07/21/accelerating-php-hashing-by-unoptimizing-it/
By "unoptimizing" DJBX33A hash algorithm, it could get some performance gain.
The idea has been implemented in the php interpreter. Is that possible also porting this optimization in Python?

Has this already been discussed elsewhere?

No response given

Links to previous discussion of this feature:

No response

Linked PRs

@PeterYang12 PeterYang12 added the type-feature A feature request or enhancement label Feb 20, 2024
PeterYang12 added a commit to PeterYang12/cpython that referenced this issue Feb 20, 2024
Accelerating python hash algorithm by "unoptimizing" it when using
DJBX33A as hash algorithm. See Daniel Lemire's blog post:
https://lemire.me/blog/2016/07/21/accelerating-php-hashing-by-unoptimizing-it/
It shows 1-2% performance gain in benchmark tomli_loads.

Signed-off-by: PeterYang12 <yuhan.yang@intel.com>
@Eclips4 Eclips4 added the performance Performance or resource usage label Feb 20, 2024
@serhiy-storchaka
Copy link
Member

Do you have any benchmarks that show the effect of this optimization in Python?

@PeterYang12
Copy link
Author

Do you have any benchmarks that show the effect of this optimization in Python?

Hi, I used benchmark tomli_loads, one of the benchmarks in pyperformance. It shows ~1.2% perf gain.
My SUT is Intel 4th Xeon and OS is Ubuntu22.04.

@gpshead gpshead self-assigned this Feb 22, 2024
@PeterYang12
Copy link
Author

Do you have any benchmarks that show the effect of this optimization in Python?

Hi, maintainer, you can follow the following steps:

# install pyperformance
python3 -m pip install pyperformance

# run and test
pyperformance run -p "$PYTHON_BINARY" -r -b tomli_loads

@serhiy-storchaka
Copy link
Member

If it shows stable difference in macrobenchmarks, it should have much more difference (perhaps tens of percents) in microbenchmarks. Could you please create such microbenchmarks to demonstrate this? Something that hashes strings in tight loop.

@PeterYang12
Copy link
Author

If it shows stable difference in macrobenchmarks, it should have much more difference (perhaps tens of percents) in microbenchmarks. Could you please create such microbenchmarks to demonstrate this? Something that hashes strings in tight loop.

Of course. Thank you for your quick review. Let me show the demo after my weekend.

@PeterYang12
Copy link
Author

Thank you for your paitence. I've attached the microbenchmark written by myself.
The patch shows ~30% on my SUT.

djbx33a_optimization.zip

PeterYang12 added a commit to PeterYang12/cpython that referenced this issue Feb 27, 2024
Accelerating python hash algorithm by "unoptimizing" it when using
DJBX33A as hash algorithm. See Daniel Lemire's blog post:
https://lemire.me/blog/2016/07/21/accelerating-php-hashing-by-unoptimizing-it/

Signed-off-by: PeterYang12 <yuhan.yang@intel.com>
@serhiy-storchaka
Copy link
Member

Could you show a Python code?

@jxu
Copy link

jxu commented Aug 24, 2024

If you are getting rid of Duff's device (which I support), why not get rid of unrolling the polynomial and let the compiler generate SIMD instructions for it?

@PeterYang12
Copy link
Author

If you are getting rid of Duff's device (which I support), why not get rid of unrolling the polynomial and let the compiler generate SIMD instructions for it?

Thank you for your question. I tried to use SIMD instructions, using intrinsics (SSE, avx256, avx512) before, but no obvious perf improvement. For avx512, it even got worse due to the CPU frequency drop. I haven't tried GCC options to do such operation. Do you have any good idea?

@gpshead gpshead removed their assignment Aug 25, 2024
@serhiy-storchaka
Copy link
Member

Please provide a Python demo to show the effect of this optimization for different examples.

My intuition says that the difference should be insignificant and hardly noticeable on Python level, especially taking into account that the hash is cached in the string object.

@mdboom
Copy link
Contributor

mdboom commented Aug 26, 2024

I ran the linked PR (#115705) against the full pyperformance suite, and the change is basically below the threshold of noise, except maybe a small improvement on x86_64 Windows. That's not an argument for or against (it still might make sense to merge the PR with a clear microbenchmark result), but it's quite clear this doesn't have an overall impact in general.

PeterYang12 added a commit to PeterYang12/cpython that referenced this issue Aug 27, 2024
Accelerating python hash algorithm by "unoptimizing" it when using
DJBX33A as hash algorithm. See Daniel Lemire's blog post:
https://lemire.me/blog/2016/07/21/accelerating-php-hashing-by-unoptimizing-it/

Signed-off-by: PeterYang12 <yuhan.yang@intel.com>
PeterYang12 added a commit to PeterYang12/cpython that referenced this issue Aug 27, 2024
Accelerating python hash algorithm by "unoptimizing" it when using
DJBX33A as hash algorithm. See Daniel Lemire's blog post:
https://lemire.me/blog/2016/07/21/accelerating-php-hashing-by-unoptimizing-it/

Signed-off-by: PeterYang12 <yuhan.yang@intel.com>
PeterYang12 added a commit to PeterYang12/cpython that referenced this issue Aug 27, 2024
Accelerating python hash algorithm by "unoptimizing" it when using
DJBX33A as hash algorithm. See Daniel Lemire's blog post:
https://lemire.me/blog/2016/07/21/accelerating-php-hashing-by-unoptimizing-it/

Signed-off-by: PeterYang12 <yuhan.yang@intel.com>
PeterYang12 added a commit to PeterYang12/cpython that referenced this issue Aug 27, 2024
Accelerating python hash algorithm by "unoptimizing" it when using
DJBX33A as hash algorithm. See Daniel Lemire's blog post:
https://lemire.me/blog/2016/07/21/accelerating-php-hashing-by-unoptimizing-it/

Signed-off-by: PeterYang12 <yuhan.yang@intel.com>
PeterYang12 added a commit to PeterYang12/cpython that referenced this issue Aug 27, 2024
Accelerating python hash algorithm by "unoptimizing" it when using
DJBX33A as hash algorithm. See Daniel Lemire's blog post:
https://lemire.me/blog/2016/07/21/accelerating-php-hashing-by-unoptimizing-it/

Signed-off-by: PeterYang12 <yuhan.yang@intel.com>
PeterYang12 added a commit to PeterYang12/cpython that referenced this issue Aug 27, 2024
Accelerating python hash algorithm by "unoptimizing" it when using
DJBX33A as hash algorithm. See Daniel Lemire's blog post:
https://lemire.me/blog/2016/07/21/accelerating-php-hashing-by-unoptimizing-it/

Signed-off-by: PeterYang12 <yuhan.yang@intel.com>
@PeterYang12
Copy link
Author

I ran the linked PR (#115705) against the full pyperformance suite, and the change is basically below the threshold of noise, except maybe a small improvement on x86_64 Windows. That's not an argument for or against (it still might make sense to merge the PR with a clear microbenchmark result), but it's quite clear this doesn't have an overall impact in general.

Did you turn on the Macro? Like: CFLAGS="-g -DPy_HASH_CUTOFF=7" ../configure

@mdboom
Copy link
Contributor

mdboom commented Aug 27, 2024

Did you turn on the Macro? Like: CFLAGS="-g -DPy_HASH_CUTOFF=7" ../configure

No, I missed that that was required. I will run again.

@mdboom
Copy link
Contributor

mdboom commented Aug 27, 2024

With this flag, the overall results are still "within the noise", however, it does (as mentioned above) get between 3%-7% faster on the tomli_loads benchmark. Seems like a worthwhile improvement (but only with Py_HASH_CUTOFF set to 7 by default).

@gpshead
Copy link
Member

gpshead commented Aug 27, 2024

If we're going to accept this as useful, we should figure out how to not require anyone to manually set that at build time. ie: ask & answer the period questions: Why does the conditional compilation path even exist? do we still need it? if so, for what and why?

@serhiy-storchaka
Copy link
Member

tomli_loads is too large. We need simpler example for which the difference will be far larger than the noise. Without this, we do not have reason to change the code.

The code is always the compromise between readability/maintenability and efficiency. There is also a cost of changing the status quo. Note also that performance depends on the platform, so if we have an example that demonstrates the difference, it should be tested on different platforms (preferably on processors with different architecture, but even different cache size/generation/manufacturer can make a diffirence).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
performance Performance or resource usage type-feature A feature request or enhancement
Projects
None yet
Development

No branches or pull requests

6 participants