⚡ Optimize unique_rolls with std::unordered_set#5
Conversation
Replaces the O(N^2) linear scan in `roll_table::unique_rolls` with an O(1) `std::unordered_set` lookup for situations where `count + start_index > 16`. This reduces overhead for large roll counts while preserving performance for small roll counts. 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: The optimization implemented replaces the O(N^2) linear scan in
roll_table::unique_rollswith an O(1) amortizedstd::unordered_setlookup. It conditionally executes only for loops with iterations > 16 to avoid the high allocation and hashing overhead ofunordered_setfor smallN.🎯 Why: Previously, large uniqueness checks iterating through thousands of rolls would scale poorly due to iterating over all previously seen values before pushing a new value. The unordered_set provides a safer alternative to direct-mapping
std::vectorapproaches since it handles unbounded and arbitrary indices naturally.📊 Measured Improvement: Baseline vs Optimized performance for generating 500 unique rolls out of a 1000 pool weights table (for 5000 iterations):
The result represents approximately an 11% speed improvement for typical large N cases while avoiding dangerous unchecked memory access and bloated array allocation.
PR created automatically by Jules for task 17182220992993073325 started by @perim