Skip to content

⚡ Optimize roll_table initialization performance#11

Merged
perim merged 1 commit into
mainfrom
perf/roll-table-init-5775408047988001604
Jul 6, 2026
Merged

⚡ Optimize roll_table initialization performance#11
perim merged 1 commit into
mainfrom
perf/roll-table-init-5775408047988001604

Conversation

@perim

@perim perim commented Jul 6, 2026

Copy link
Copy Markdown
Owner

💡 What: Replaced the use of std::multimap<int, int> with std::vector<std::pair<int, int>> and std::sort in the roll_table constructor (dice.cpp:84). We ensure identical sorting behavior by sorting with std::greater<std::pair<int, int>>().
🎯 Why: std::multimap is a red-black tree that performs individual heap allocations per element, which incurs massive overhead for large inputs. A simple flat std::vector + std::sort is cache-friendly, does not incur unnecessary tree overhead, and is significantly faster and uses less memory.
📊 Measured Improvement: In a custom benchmark iterating 10,000 times to create a 1,000-element roll_table, CPU cycles fell from ~1.16 billion to ~137 million, representing an 88% reduction in execution time for initialization.


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

Replaced `std::multimap` with `std::vector` + `std::sort` in `roll_table::roll_table`.
`std::multimap` performs excessive heap allocations for tree nodes per element,
leading to significantly higher initialization times.

A `std::vector` using `emplace_back` and `std::sort` is cache-friendly and uses less memory.
The sort algorithm correctly mimics the previous descending-order traversal behavior
by using `std::greater<std::pair<int, int>>()`.

This change decreases 1,000-element `roll_table` initializations by ~88% in CPU cycles.

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 26ece83 into main Jul 6, 2026
1 check passed
@perim
perim deleted the perf/roll-table-init-5775408047988001604 branch July 13, 2026 22:01
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