Skip to content

⚡ Optimize unique_rolls with std::unordered_set#5

Merged
perim merged 1 commit into
mainfrom
perf/unique-rolls-17182220992993073325
May 22, 2026
Merged

⚡ Optimize unique_rolls with std::unordered_set#5
perim merged 1 commit into
mainfrom
perf/unique-rolls-17182220992993073325

Conversation

@perim
Copy link
Copy Markdown
Owner

@perim perim commented May 22, 2026

💡 What: The optimization implemented replaces the O(N^2) linear scan in roll_table::unique_rolls with an O(1) amortized std::unordered_set lookup. It conditionally executes only for loops with iterations > 16 to avoid the high allocation and hashing overhead of unordered_set for small N.

🎯 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::vector approaches 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):

  • Baseline: ~665ms
  • Optimized: ~592ms
    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

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>
@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 f2c802e into main May 22, 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