From 338164fad9c75a317f17adc85c46e9be3ec4fa00 Mon Sep 17 00:00:00 2001 From: "google-labs-jules[bot]" <161369871+google-labs-jules[bot]@users.noreply.github.com> Date: Sat, 20 Jun 2026 16:10:11 +0000 Subject: [PATCH] =?UTF-8?q?=E2=9A=A1=20Bolt:=20fix=20`forbidden`=20reassig?= =?UTF-8?q?nment=20bug=20and=20list=20allocation=20overhead=20in=20`comput?= =?UTF-8?q?e=5Frange=5Fvs=5Frange=5Fequity`?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: wavehs <156133648+wavehs@users.noreply.github.com> --- .jules/bolt.md | 3 +++ services/solver_core/solver.py | 18 +++++++++--------- 2 files changed, 12 insertions(+), 9 deletions(-) diff --git a/.jules/bolt.md b/.jules/bolt.md index eafd383..2ae32b1 100644 --- a/.jules/bolt.md +++ b/.jules/bolt.md @@ -4,3 +4,6 @@ ## 2024-05-18 - Hand Evaluator Memory Allocations in Hot Paths **Learning:** `_evaluate_five_int` was utilizing `dict` to count elements and calling `sorted()` to identify duplicate frequencies. Since this function is called inside the innermost loops of Monte Carlo simulations millions of times, avoiding object allocations entirely and just relying on comparisons over pre-sorted array elements (`sr0 == sr1`) gave a massive ~2.7x speedup for pure evaluation and significantly dropped full simulation latencies. **Action:** In Python performance-critical hot paths, try to remove data structures like dicts, sets, and Counter in favor of simple boolean logic and manual loop unrolling, particularly for fixed-size inputs. +## 2026-06-20 - Monte Carlo Array State Leakage +**Learning:** When optimizing hot loops with pre-allocated O(1) boolean arrays (like `forbidden`), ensure the array is not inadvertently overwritten by a Python `set` or `dict` during the loop. Re-assigning the reference breaks the optimization and causes type errors when attempting to clean up state by index assignment. +**Action:** Verify variables in performance hot paths retain their intended optimized types (e.g. lists of booleans) and use index assignment rather than entire reassignment. diff --git a/services/solver_core/solver.py b/services/solver_core/solver.py index ce9fb1d..b0ff2c1 100644 --- a/services/solver_core/solver.py +++ b/services/solver_core/solver.py @@ -586,21 +586,21 @@ def compute_range_vs_range_equity( full_board = board_ints + sampled_board # Evaluate villain - v_rank = self.evaluator.evaluate(list(v_hand) + full_board) + v_rank = self.evaluator.evaluate([v_hand[0], v_hand[1]] + full_board) # Construct O(1) lookup for forbidden cards to optimize hero hand filtering # ~4x faster than repeated membership checks inside the hot loop - forbidden = set(v_hand) | set(sampled_board) + forbidden[v_hand[0]] = True + forbidden[v_hand[1]] = True + for s in sampled_board: + forbidden[s] = True # Evaluate hero hands - # ⚡ Bolt Optimization: Use forbidden set instead of list lookups in loop. - forbidden = {o1, o2} - forbidden.update(sampled_board) for h_hand in valid_h_hands: - if h_hand[0] in forbidden or h_hand[1] in forbidden: + if forbidden[h_hand[0]] or forbidden[h_hand[1]]: continue - h_rank = self.evaluator.evaluate(list(h_hand) + full_board) + h_rank = self.evaluator.evaluate([h_hand[0], h_hand[1]] + full_board) if h_rank > v_rank: wins[h_hand] += 1 @@ -609,8 +609,8 @@ def compute_range_vs_range_equity( totals[h_hand] += 1 # ⚡ Bolt: Unmark forbidden cards to reuse array - forbidden[v0] = False - forbidden[v1] = False + forbidden[v_hand[0]] = False + forbidden[v_hand[1]] = False for s in sampled_board: forbidden[s] = False