Skip to content

perf(cpython): lazy random seeding - #2540

Merged
chaliy merged 1 commit into
mainfrom
claude/project-thread-io7h59
Oct 7, 2026
Merged

chaliy merged 1 commit into
mainfrom
claude/project-thread-io7h59

Conversation

@chaliy

@chaliy chaliy commented Oct 7, 2026 •

Copy link
Copy Markdown
Contributor

What changed

The CPython guest no longer reseeds random on every python3 call. The snapshot's module instance seeds itself from os.urandom the first time a call uses it, then turns back into a plain Random.

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. random reseeded in _refresh_process_state on every call.

After

python_call/cpython/pass: 3.49 ms (criterion, −7%, p = 0.00). print within noise (4.37 ms). New test lazy_random_seed_covers_every_entry_point checks that every module entry point (random, randint, choice, shuffle, getrandbits, getstate, seed, setstate, randbytes, ...) differs across calls and that seed(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. random is not a CSPRNG.

Checklist

  • cargo test -p bashkit --features cpython --test integration -- cpython (112 passed)
  • cargo fmt --check
  • Knowledge updated (cpython-wasm.md, log)

Generated by Claude Code

Seed random on first use in a call instead of every call.

Claude-Session: https://claude.ai/code/session_01Ke1iW2hQi9VXioZi1avPaW
@cloudflare-workers-and-pages

Copy link
Copy Markdown

Deploying with  Cloudflare Workers  Cloudflare Workers

The latest updates on your project. Learn more about integrating Git with Workers.

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

@chaliy
chaliy merged commit 85b9a78 into main Oct 7, 2026
36 checks passed
@chaliy
chaliy deleted the claude/project-thread-io7h59 branch October 7, 2026 05:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant