diff --git a/.jules/bolt.md b/.jules/bolt.md index eafd383..0485b96 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. +## 2024-05-19 - Pre-allocated Array Reassignment Anti-pattern +**Learning:** When using pre-allocated O(1) arrays (like `forbidden = [False] * 52`) in hot Monte Carlo loops, overwriting the array variable with a `set` or other structure inside the loop completely destroys the performance benefit. It introduces overhead from memory allocation and causes `TypeError` when later trying to perform index assignment to reset the state. +**Action:** When using a pre-allocated array for state tracking in a loop, strictly perform element-wise index assignments (`array[i] = True`) and element-wise resets, ensuring the variable itself is never reassigned to a new object type. diff --git a/services/solver_core/solver.py b/services/solver_core/solver.py index ce9fb1d..f9c6e7a 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([v0, v1] + 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[v0] = True + forbidden[v1] = 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