GeezerVison is showing github.com — gh-109653: Fix py312 regression in the import time of `random` by AlexWaygood · Pull Request #110221 · python/cpython
Conversation
AlexWaygood
commented
Oct 2, 2023
•
edited by bedevere-app
Bot
Loading
Uh oh!
There was an error while loading. Please reload this page.
As an optimisation to reduce the import time of the module, random first tries to import sha512 from the internal _sha512 module before falling back to hashlib. The problem, however, is that Python no longer has a _sha512 module! It was removed in 0b13575, by @gpshead. That means we're currently always falling back to the slow path in random.py, leading to the import time of random being far slower than it should be.
Importing sha512 from the correct module in the fast path cuts 60% off the import time of random.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
gpshead reacted with thumbs up emoji
AlexWaygood
changed the title
Reduce the import time of random by 60%
gh-109653: Reduce the import time of random by 60%
Oct 2, 2023
Uh oh!
There was an error while loading. Please reload this page.
|
Given that this is a regression, should we backport it into 3.12?
|
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
AlexWaygood
commented
Oct 2, 2023
•
edited
Loading
Uh oh!
There was an error while loading. Please reload this page.
Given that this is a regression, should we backport it into 3.12?
I was wondering that. I'd vote in favour of doing so, since it doesn't seem particularly high-risk to me. But I'd like to hear Raymond's and/or Greg's thoughts.
|
JelleZijlstra and gpshead reacted with thumbs up emoji
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
|
A backport to 3.12 would be reasonable.
|
AlexWaygood reacted with thumbs up emoji
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
A backport to 3.12 would be reasonable.
Great, I've scheduled the backport. Thanks for the review!
|
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
AlexWaygood
changed the title
gh-109653: Reduce the import time of random by 60%
gh-109653: Fix py312 regression in the import time of random
Oct 2, 2023
|
Thanks @AlexWaygood for the PR 🌮🎉.. I'm working now to backport this PR to: 3.12.
🐍🍒⛏🤖
|
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
gpshead reacted with thumbs up emoji
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
This comment was marked as duplicate.
⚠️⚠️⚠️ Buildbot failure ⚠️⚠️⚠️
Hi! The buildbot aarch64 Debian Clang LTO + PGO 3.x has failed when building commit 21a6263.
What do you need to do:
- Don't panic.
- Check the buildbot page in the devguide if you don't know what the buildbots are or how they work.
- Go to the page of the buildbot that failed (https://buildbot.python.org/all/#builders/1084/builds/2183) and take a look at the build logs.
- Check if the failure is related to this commit (21a6263) or if it is a false positive.
- If the failure is related to this commit, please, reflect that on the issue and make a new Pull Request with a fix.
You can take a look at the buildbot page here:
https://buildbot.python.org/all/#builders/1084/builds/2183
Failed tests:
- test.test_concurrent_futures.test_shutdown
Failed subtests:
- test_interpreter_shutdown - test.test_concurrent_futures.test_shutdown.ProcessPoolSpawnProcessPoolShutdownTest.test_interpreter_shutdown
Summary of the results of the build (if available):
==
Click to see traceback logs
Traceback (most recent call last):
File "/var/lib/buildbot/workers/arm64-clang/3.x.gps-arm64-debian.clang.lto-pgo/build/Lib/test/test_concurrent_futures/test_shutdown.py", line 50, in test_interpreter_shutdown
self.assertEqual(out.strip(), b"apple")
AssertionError: b'' != b'apple'
|
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
|
|
@@ -65,7 +65,7 @@ |
|
|
|
|
|
try:
|
|
|
# hashlib is pretty heavy to load, try lean internal module first
|
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.
This code is technically awkward... It tried to speed up import time and did so by circumventing hashlib which means that it is loading and using the slowest possible sha512 implementation by default (hashlib will pick up openssl 3's accelerated sha512 support by default on most platforms and not use our builtin). so faster startup time for a slower runtime computation? thankfully this is only ever used by seed() which is a single/constant number of calls for most programs on tiny data so there is zero reason to care about sha512 performance for its purposes. The slower implementation may still be faster on small seed data anyways due to less setup overhead.
Nothing to do here. This works for random's purposes & thanks for the fixup. But this being a regression in the first place demonstrates how fragile direct use of internal details can be. (and indirectly how much in need of an overhaul hashlib.py could use)
I'd file an issue rather than leaving this comment in the merged PR void if there were anything concrete to describe and tackle, there isn't. :)
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
AlexWaygood reacted with heart emoji
Reviewers
gpshead
gpshead left review comments
Uh oh!
There was an error while loading. Please reload this page.
Assignees
No one assigned
Labels
performance
Performance or resource usage
stdlib
Standard Library Python modules in the Lib/ directory
Milestone
No milestone
Development
Successfully merging this pull request may close these issues.
Uh oh!
There was an error while loading. Please reload this page.
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.
|