Skip to content

⚡ Optimize roll_table::unique_rolls with stack-allocated hash table#14

Merged
perim merged 1 commit into
mainfrom
perf-unique-rolls-8423544618495648046
Jul 19, 2026
Merged

⚡ Optimize roll_table::unique_rolls with stack-allocated hash table#14
perim merged 1 commit into
mainfrom
perf-unique-rolls-8423544618495648046

Conversation

@perim

@perim perim commented Jul 16, 2026

Copy link
Copy Markdown
Owner

💡 What:
Replaced the std::unordered_set inside roll_table::unique_rolls with a custom open-addressing hash table logic that allocates a fixed 256-integer array (1024 bytes) on the stack. A dynamic fallback to std::vector<int> (which performs one large allocation vs numerous small allocations) is included to handle cases where array sizes exceed the stack capacity. Also replaced standard hashing with an optimized integer multiplication hashing.

🎯 Why:
roll_table::unique_rolls is called frequently. The prior implementation instantiated a std::unordered_set dynamically per call for tables exceeding 16 unique rolls. This induced massive performance penalties from allocating the node elements individually as well as invoking heavy hashing functions. The new approach avoids most heap allocations for common usage scenarios resulting in a huge performance jump.

📊 Measured Improvement:
Using an ad-hoc performance test evaluating 500,000 iterations of large unique_rolls fetching 50 values each out of 200 items in a deterministic sequence, execution time dropped from 3.99s to 2.03s, producing an approximately 49% measured speed-up over the baseline. Tests passed perfectly with this change.


PR created automatically by Jules for task 8423544618495648046 started by @perim

Replaced std::unordered_set inside roll_table::unique_rolls with a custom stack-allocated open-addressing hash table. This drastically reduces the overhead caused by expensive dynamic memory allocations and hash computations inside the hot loop.

A fallback to std::vector has been maintained for large collections to avoid stack overflow, but most dice roll scenarios will fit neatly in the 1024 bytes array on the stack. The lookup implementation has also seen roughly a 49% speedup for larger sequences in testing.

Co-authored-by: perim <436583+perim@users.noreply.github.com>
@google-labs-jules

Copy link
Copy Markdown
Contributor

👋 Jules, reporting for duty! I'm here to lend a hand with this pull request.

When you start a review, I'll add a 👀 emoji to each comment to let you know I've read it. I'll focus on feedback directed at me and will do my best to stay out of conversations between you and other bots or reviewers to keep the noise down.

I'll push a commit with your requested changes shortly after. Please note there might be a delay between these steps, but rest assured I'm on the job!

For more direct control, you can switch me to Reactive Mode. When this mode is on, I will only act on comments where you specifically mention me with @jules. You can find this option in the Pull Request section of your global Jules UI settings. You can always switch back!

New to Jules? Learn more at jules.google/docs.


For security, I will only act on instructions from the user who triggered this task.

@perim
perim merged commit dc30db0 into main Jul 19, 2026
1 check 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.

1 participant