diff --git a/.jules/bolt.md b/.jules/bolt.md index eafd383..542a5f9 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 Deck Sampling and Inner Loop List Comprehensions +**Learning:** Contrary to intuition, initializing new variables using list comprehensions like `tmp_deck = [c for c in deck if c != o1 and c != o2]` followed by `random.sample` is significantly slower inside highly iterative loops than utilizing `random.choice` on a generic array within a simple `while` loop that validates against an O(1) indexed boolean array constraint `forbidden`. +**Action:** Replace `random.sample` and dynamically generated filtered arrays inside hot inner loops with simple `while` loops that continuously randomly pick from the full set and perform a quick `O(1)` bounds check array. Ensure all modifications to bounds check array are reverted after. diff --git a/services/solver_core/solver.py b/services/solver_core/solver.py index ce9fb1d..6c60b24 100644 --- a/services/solver_core/solver.py +++ b/services/solver_core/solver.py @@ -578,10 +578,17 @@ def compute_range_vs_range_equity( v0, v1 = v_hand # Draw board - # ⚡ Bolt Optimization: Use random.sample over a dynamically filtered deck. + # ⚡ Bolt Optimization: Use while loop with random.choice and O(1) tracking array + # over random.sample on dynamically filtered deck. o1, o2 = v_hand[0], v_hand[1] - tmp_deck = [c for c in deck if c != o1 and c != o2] - sampled_board = random.sample(tmp_deck, cards_needed) + forbidden[o1] = True + forbidden[o2] = True + sampled_board = [] + while len(sampled_board) < cards_needed: + c = random.choice(deck) + if not forbidden[c]: + forbidden[c] = True + sampled_board.append(c) full_board = board_ints + sampled_board @@ -590,14 +597,11 @@ def compute_range_vs_range_equity( # 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) # Evaluate hero hands - # ⚡ Bolt Optimization: Use forbidden set instead of list lookups in loop. - forbidden = {o1, o2} - forbidden.update(sampled_board) + # ⚡ Bolt Optimization: Use pre-allocated boolean array over set creations in loop. 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)