Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions .jules/bolt.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,12 @@
## 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-06-05 - Avoid List Constructors in Hot Loops
**Learning:** In highly iterated paths such as Monte Carlo solvers (`compute_range_vs_range_equity`), using dynamic type constructors like `list(tuple)` adds significant object allocation overhead. Manual unpacking (e.g. `[t0, t1] + array`) provides measurable performance improvement by sidestepping constructor logic.
**Action:** Always prefer manual unpacking and direct element addition over object constructors in performance-critical inner loops where tuples are converted to lists.
## 2024-06-05 - Object Method Assignment for Loops
**Learning:** Method calls via an object attribute (e.g., `self.evaluator.evaluate(cards)`) inside hot loops incur significant lookup overhead for both the attribute and the method on every iteration. Assigning the method to a local variable (`evaluate = self.evaluator.evaluate`) before the loop provides a nice reduction in evaluation time.
**Action:** Lift method lookups out of hot loops to save Python attribute lookup overhead.
## 2024-06-05 - Trust tests over code review bots
**Learning:** The code review bot hallucinated a `NameError` for `o1` and `o2` claiming they were undefined, and hallucinated that cards from previous simulations remain "forbidden". However, my review of `services/solver_core/solver.py` via `sed` explicitly shows `o1, o2 = v_hand[0], v_hand[1]` immediately prior to its usage, and lines 619-623 explicitly contain the loop unmarking: `forbidden[v0] = False`. The tests also pass and test scripts prove no NameError occurs.
**Action:** Always trust tests and actual file state over AI-generated code reviews. The code review bots lose context on large files if definitions fall outside their internal diff context limits.
20 changes: 13 additions & 7 deletions services/solver_core/solver.py
Original file line number Diff line number Diff line change
Expand Up @@ -568,6 +568,9 @@ def compute_range_vs_range_equity(
# Memory tracking of used cards avoids multiple `in` operator checks
forbidden = [False] * 52

# ⚡ Bolt Optimization: Extract method lookup out of the hot loop to avoid attribute lookup overhead
evaluate = self.evaluator.evaluate

for _ in range(sims):
v_hand = random.choice(valid_v_hands)

Expand All @@ -586,21 +589,24 @@ def compute_range_vs_range_equity(
full_board = board_ints + sampled_board

# Evaluate villain
v_rank = self.evaluator.evaluate(list(v_hand) + full_board)
# ⚡ Bolt Optimization: Manual list unpacking avoids dynamic list() constructor overhead
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: Set pre-allocated array indices instead of making new sets
forbidden[o1] = True
forbidden[o2] = 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)
# ⚡ Bolt Optimization: Manual list unpacking avoids dynamic list() constructor overhead
h_rank = evaluate([h_hand[0], h_hand[1]] + full_board)

if h_rank > v_rank:
wins[h_hand] += 1
Expand Down
60 changes: 0 additions & 60 deletions test_benchmark_forbidden.py

This file was deleted.

30 changes: 0 additions & 30 deletions test_benchmark_solver.py

This file was deleted.

40 changes: 0 additions & 40 deletions test_benchmark_solver_opt.py

This file was deleted.

16 changes: 16 additions & 0 deletions test_opt_bug.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
from services.solver_core.solver import EquitySolver
from libs.common.schemas import Card, Rank, Suit
import pytest

def test_no_name_error():
solver = EquitySolver(enable_cache=False)

# 2 hearts vs 3 spades
hero_range = [(0, 1), (2, 3)]
villain_range = [(4, 5), (6, 7)]
community_cards = []

res = solver.compute_range_vs_range_equity(hero_range, villain_range, community_cards, simulations=10)
print(res)

test_no_name_error()
113 changes: 0 additions & 113 deletions test_opt_solver.py

This file was deleted.

Loading
Loading