diff --git a/.jules/bolt.md b/.jules/bolt.md index eafd383..2f27e85 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-18 - Fast array lookups in hot Monte Carlo loops +**Learning:** Extracting an inner loop check from `in {set}` over to reading `array[i]` from a pre-allocated boolean array reduces significant set instantiation and hashing overhead in tight loops millions of times per simulation. The key is un-marking the specific indices updated at the end of each iteration so the same array can be reused across iterations instead of allocating `[False] * 52` repeatedly. +**Action:** In Python performance-critical hot paths like Monte Carlo inner loops tracking used cards, pre-allocating a single O(1) boolean array outside the loop and updating/un-updating individual elements is much faster than doing repeated `set` creation and intersection checks. diff --git a/services/solver_core/solver.py b/services/solver_core/solver.py index ce9fb1d..892bc0a 100644 --- a/services/solver_core/solver.py +++ b/services/solver_core/solver.py @@ -568,6 +568,9 @@ def compute_range_vs_range_equity( # Memory tracking of used cards avoids multiple `in` operator checks forbidden = [False] * 52 + # ⚡ Bolt: Extract instance method reference outside the loop to reduce lookup overhead + evaluate = self.evaluator.evaluate + for _ in range(sims): v_hand = random.choice(valid_v_hands) @@ -575,32 +578,27 @@ def compute_range_vs_range_equity( if len(deck) - 2 < cards_needed: continue - v0, v1 = v_hand - # Draw board # ⚡ Bolt Optimization: Use random.sample over a dynamically filtered deck. - o1, o2 = v_hand[0], v_hand[1] - tmp_deck = [c for c in deck if c != o1 and c != o2] + tmp_deck = [c for c in deck if c != v_hand[0] and c != v_hand[1]] sampled_board = random.sample(tmp_deck, cards_needed) full_board = board_ints + sampled_board # Evaluate villain - v_rank = self.evaluator.evaluate(list(v_hand) + full_board) + v_rank = 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) + # ⚡ Bolt Optimization: Use pre-allocated O(1) array instead of sets in hot loop. + 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 = evaluate([h_hand[0], h_hand[1]] + full_board) if h_rank > v_rank: wins[h_hand] += 1 @@ -609,8 +607,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