⚡ Optimize roll_table::unique_rolls with stack-allocated hash table#14
Conversation
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>
|
👋 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 New to Jules? Learn more at jules.google/docs. For security, I will only act on instructions from the user who triggered this task. |
💡 What:
Replaced the
std::unordered_setinsideroll_table::unique_rollswith a custom open-addressing hash table logic that allocates a fixed 256-integer array (1024 bytes) on the stack. A dynamic fallback tostd::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_rollsis called frequently. The prior implementation instantiated astd::unordered_setdynamically 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_rollsfetching 50 values each out of 200 items in a deterministic sequence, execution time dropped from3.99sto2.03s, producing an approximately49%measured speed-up over the baseline. Tests passed perfectly with this change.PR created automatically by Jules for task 8423544618495648046 started by @perim