Skip to content

Avoid temporary big-integer storage for small getrandbits calls - #9233

Merged
youknowone merged 1 commit into
RustPython:mainfrom
1ndahous3:random_small_bits
Oct 20, 2026
Merged

youknowone merged 1 commit into
RustPython:mainfrom
1ndahous3:random_small_bits

Conversation

@1ndahous3

@1ndahous3 1ndahous3 commented Oct 19, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Generate getrandbits(1..32) directly from one MT19937 word, avoiding the temporary Vec and BigUint. Fixed-seed output and generator state are unchanged.

Performance

Release builds, median of three paired runs; each run uses five timing samples.

Windows 11 x64

Operation Main, ns/op Patched, ns/op Speedup
getrandbits(8) 221 183 1.21×
getrandbits(32) 250 208 1.20×
randrange(100) 1,140 1,035 1.10×
choice(64 items) 1,209 1,119 1.08×
shuffle(64 items) 50,734 46,302 1.10×

Debian 12 amd64

Operation Main, ns/op Patched, ns/op Speedup
getrandbits(8) 229 170 1.35×
getrandbits(32) 239 191 1.25×
randrange(100) 1,042 945 1.10×
choice(64 items) 1,132 980 1.16×
shuffle(64 items) 50,453 43,845 1.15×

AI assistance

Written with Codex (GPT-6), reviewed by a human before submission.

Summary by CodeRabbit

  • Bug Fixes
    • Corrected random bit generation for requests of 1–32 bits. These requests now return the requested number of high-order bits from a generated random value.

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 19, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-19T18:23:14.489332Z 6bcf8af PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@coderabbitai

coderabbitai Bot commented Oct 19, 2026 •

Copy link
Copy Markdown
Contributor

Review in Change Stack →

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration
  • Configuration used: Repository: RustPython/RustPython/.coderabbit.yml
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 5f15360c-3535-414d-b514-d203c7adb84d
📥 Commits

Reviewing files that changed from the base of the PR and between c08375b and 6bcf8af.

📒 Files selected for processing: 1
  • crates/stdlib/src/random.rs

Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 4 remain after this review.

📝 Walkthrough

getrandbits now handles requests for 1–32 bits by returning the high-order bits of one generated u32. Requests larger than 32 bits continue through the existing multiword path.

Changes: Random Bit Generation

Layer / File(s) Summary
Handle requests of 1–32 bits
crates/stdlib/src/random.rs
For k from 1 through 32, getrandbits shifts one generated u32 right by 32 - k.

Priority: ⬇️ Low

Estimated code review effort: 2 (Simple) | ~8 minutes

Change: Refactor

Suggested reviewers: youknowone

Merge Risk: ⚪ Minimal · up to 6bcf8

The small-request path is intended to preserve seeded values and generator state while avoiding temporary big-integer storage. No actionable merge risk is evident from the supplied context.

Pre-merge checks: Passed 5
Check name Status Explanation
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check Passed The title clearly and concisely describes the main change: removing temporary big-integer storage for small getrandbits calls.
Docstring Coverage Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 1 functions across 1 files.
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@youknowone youknowone left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

@youknowone
youknowone merged commit c025843 into RustPython:main Oct 20, 2026
30 checks passed
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.

2 participants