Repository navigation
perf(cpython): lazy random seeding - #2540
Merged
Merged
Conversation
Seed random on first use in a call instead of every call. Claude-Session: https://claude.ai/code/session_01Ke1iW2hQi9VXioZi1avPaW
Deploying with
|
| Status | Name | Latest Commit | Preview URL | Updated (UTC) |
|---|---|---|---|---|
| ✅ Deployment successful! View logs |
bashkit | de56d2b | Commit Preview URL Branch Preview URL |
Oct 07 2026, 05:17 AM |
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.
What changed
The CPython guest no longer reseeds
randomon everypython3call. The snapshot's module instance seeds itself fromos.urandomthe first time a call uses it, then turns back into a plainRandom.Why
Most calls never touch
random, and the reseed was ~0.2 ms of per-call driver work on Pulley. Per-tenant randomness is unchanged: every call that draws numbers is still seeded fresh.Before
python_call/cpython/pass: 3.7 ms.randomreseeded in_refresh_process_stateon every call.After
python_call/cpython/pass: 3.49 ms (criterion, −7%, p = 0.00).printwithin noise (4.37 ms). New testlazy_random_seed_covers_every_entry_pointchecks that every module entry point (random,randint,choice,shuffle,getrandbits,getstate,seed,setstate,randbytes, ...) differs across calls and thatseed(n)stays deterministic.Risk
Low. Calling the C base method directly (
_random.Random.random(random._inst)) skips the seed and sees the snapshot state; documented in the driver.randomis not a CSPRNG.Checklist
cargo test -p bashkit --features cpython --test integration -- cpython(112 passed)cargo fmt --checkcpython-wasm.md, log)Generated by Claude Code