From 89c155e94804c507b5644e715d98aac4a5e22f82 Mon Sep 17 00:00:00 2001 From: "google-labs-jules[bot]" <161369871+google-labs-jules[bot]@users.noreply.github.com> Date: Mon, 6 Jul 2026 16:47:05 +0000 Subject: [PATCH 1/2] Optimize const_roll_table::init by replacing std::queue with std::vector 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> --- CMakeLists.txt | 1 + dice.cpp | 20 +++++++++++--------- 2 files changed, 12 insertions(+), 9 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index fbda7bf..e15ed73 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -101,3 +101,4 @@ ADD_TEST(perf_prd perf_prd) ADD_TEST(visualization visualization) ADD_TEST(perten_test perten_test) ADD_TEST(perf_perten perf_perten) +add_executable(perf_init tests/perf_init.cpp dice.cpp) diff --git a/dice.cpp b/dice.cpp index 620ed49..40a165f 100644 --- a/dice.cpp +++ b/dice.cpp @@ -12,8 +12,10 @@ void const_roll_table::init(const std::vector& weights) size = (int)weights.size(); alias.assign(size, -1); probability.resize(size); - std::queue small; - std::queue large; + std::vector small; + std::vector large; + small.reserve(size); + large.reserve(size); std::vector w; w.reserve(size); long s = 0; @@ -25,23 +27,23 @@ void const_roll_table::init(const std::vector& weights) sum = s; for (int i = 0; i < size; i++) { - if (w[i] < sum) { small.push(i); } - else { large.push(i); } + if (w[i] < sum) { small.push_back(i); } + else { large.push_back(i); } } while (small.size() > 0 && large.size() > 0) { - const int lv = large.front(); large.pop(); - const int sv = small.front(); small.pop(); + const int lv = large.back(); large.pop_back(); + const int sv = small.back(); small.pop_back(); probability[sv] = w.at(sv); alias[sv] = lv; const int tmp = w[lv] + w[sv] - sum; w[lv] = tmp; - if (tmp < sum) small.push(lv); - else large.push(lv); + if (tmp < sum) small.push_back(lv); + else large.push_back(lv); } while (large.size() > 0) { - int lv = large.front(); large.pop(); + int lv = large.back(); large.pop_back(); probability[lv] = sum; } } From d1da4179e14445e55c1472a20e24af3c47941468 Mon Sep 17 00:00:00 2001 From: "google-labs-jules[bot]" <161369871+google-labs-jules[bot]@users.noreply.github.com> Date: Mon, 6 Jul 2026 16:50:28 +0000 Subject: [PATCH 2/2] Optimize const_roll_table::init by replacing std::queue with std::vector 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> --- CMakeLists.txt | 1 - 1 file changed, 1 deletion(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index e15ed73..fbda7bf 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -101,4 +101,3 @@ ADD_TEST(perf_prd perf_prd) ADD_TEST(visualization visualization) ADD_TEST(perten_test perten_test) ADD_TEST(perf_perten perf_perten) -add_executable(perf_init tests/perf_init.cpp dice.cpp)