Skip to content

⚡ Optimize const_roll_table init by replacing queue with vector#12

Merged
perim merged 2 commits into
mainfrom
perf/optimize-alias-table-init-11820680638652560055
Jul 6, 2026
Merged

⚡ Optimize const_roll_table init by replacing queue with vector#12
perim merged 2 commits into
mainfrom
perf/optimize-alias-table-init-11820680638652560055

Conversation

@perim

@perim perim commented Jul 6, 2026

Copy link
Copy Markdown
Owner

💡 What: Replaced std::queue<int> with std::vector<int> for the small and large collections in const_roll_table::init. Used .reserve(size) to preallocate memory, and updated operations to push_back, back, and pop_back.
🎯 Why: std::queue defaults to std::deque, which allocates multiple blocks of memory. A std::vector is a contiguous block of memory and avoids these allocations entirely when properly pre-allocated with reserve(). Vose's Alias Method setup algorithm does not require FIFO ordering, so processing as a LIFO stack is perfectly safe and algorithmically equivalent.
📊 Measured Improvement:
Measured using cpu_gettime():

  • CRT init 10k times (200 weights):
    • Baseline: ~27.2 million cycles
    • Optimized: ~17.9 million cycles
    • Improvement: ~34% faster
  • CRT init 1k times (5000 weights):
    • Baseline: ~72.4 million cycles
    • Optimized: ~41.5 million cycles
    • Improvement: ~42% faster

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

Using std::vector with reserve avoids multiple memory block allocations that happen with std::queue (std::deque). LIFO processing via push_back/pop_back works perfectly fine for Vose's Alias Method setup.

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.

Using std::vector with reserve avoids multiple memory block allocations that happen with std::queue (std::deque). LIFO processing via push_back/pop_back works perfectly fine for Vose's Alias Method setup.

Co-authored-by: perim <436583+perim@users.noreply.github.com>
@perim
perim merged commit 15d80e0 into main Jul 6, 2026
1 check passed
@perim
perim deleted the perf/optimize-alias-table-init-11820680638652560055 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