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
3 changes: 3 additions & 0 deletions .jules/bolt.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
## 2025-04-06 - Dict setdefault in hot loops
**Learning:** `dict.setdefault(key, complex_default)` eagerly evaluates `complex_default` on every iteration, leading to massive memory allocation and performance overhead when creating default lists/dicts in hot loops (like in `HullKVCache._rebuild_if_needed`).
**Action:** Replace `setdefault` with an explicit `if key not in dict:` membership check to lazily evaluate the default structure only when necessary.
1 change: 0 additions & 1 deletion scripts/export_p3_paper_freeze.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import json
from pathlib import Path
import re
from typing import Any

from utils import detect_runtime_environment

Expand Down
1 change: 0 additions & 1 deletion scripts/export_p5_public_surface_sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

import json
from pathlib import Path
from typing import Any

from utils import detect_runtime_environment

Expand Down
2 changes: 1 addition & 1 deletion scripts/export_r10_d0_same_endpoint_cost_attribution.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
subroutine_braid_long_program,
verify_program,
)
from exec_trace import ExecutionState, Program, TraceEvent, replay_trace, TraceInterpreter
from exec_trace import Program, TraceEvent, replay_trace, TraceInterpreter
from geometry import brute_force_hardmax_2d
from model.exact_hardmax import encode_latest_write_query
from model.free_running_executor import (
Expand Down
2 changes: 1 addition & 1 deletion scripts/export_r2_systems_baseline_gate.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

from __future__ import annotations

from collections import Counter, defaultdict
from collections import defaultdict
import csv
import json
from pathlib import Path
Expand Down
1 change: 0 additions & 1 deletion scripts/export_r3_d0_exact_execution_stress_gate.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

import json
from pathlib import Path
from typing import Any

from bytecode import (
lower_program,
Expand Down
1 change: 0 additions & 1 deletion scripts/export_release_worktree_hygiene_snapshot.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import json
from pathlib import Path
import subprocess
from typing import Any

from utils import detect_runtime_environment

Expand Down
26 changes: 15 additions & 11 deletions src/geometry/hull_kv.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
HardmaxResult,
NumberLike,
ValueLike,
_as_fraction,
_coerce_key,
_coerce_value,
_normalize_number,
Expand Down Expand Up @@ -205,15 +204,18 @@ def _rebuild_if_needed(self) -> None:
total_value_sum = [Fraction(0) for _ in range(self._value_width or 0)]

for index, (key, value) in enumerate(self._entries):
bucket = aggregates.setdefault(
key,
{"value_sum": [Fraction(0) for _ in value], "count": 0, "entry_indices": []},
)
for coord_index, coord in enumerate(value):
bucket["value_sum"][coord_index] += coord
total_value_sum[coord_index] += coord
bucket["count"] += 1
bucket["entry_indices"].append(index)
if key not in aggregates:
bucket = {"value_sum": list(value), "count": 1, "entry_indices": [index]}
aggregates[key] = bucket
for coord_index, coord in enumerate(value):
total_value_sum[coord_index] += coord
else:
bucket = aggregates[key]
Comment on lines +207 to +213

Copilot AI Apr 6, 2026

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In _rebuild_if_needed, the if key not in aggregates pattern performs two dictionary lookups for the common (existing-key) case (__contains__ + __getitem__). Since this is a hot loop, consider using a single-lookup pattern (e.g., bucket = aggregates.get(key) with a None sentinel, or try/except KeyError) to keep the allocation win without adding extra hash probes.

Suggested change
if key not in aggregates:
bucket = {"value_sum": list(value), "count": 1, "entry_indices": [index]}
aggregates[key] = bucket
for coord_index, coord in enumerate(value):
total_value_sum[coord_index] += coord
else:
bucket = aggregates[key]
bucket = aggregates.get(key)
if bucket is None:
bucket = {"value_sum": list(value), "count": 1, "entry_indices": [index]}
aggregates[key] = bucket
for coord_index, coord in enumerate(value):
total_value_sum[coord_index] += coord
else:

Copilot uses AI. Check for mistakes.
for coord_index, coord in enumerate(value):
bucket["value_sum"][coord_index] += coord
total_value_sum[coord_index] += coord
bucket["count"] += 1
bucket["entry_indices"].append(index)

points: list[_PointAggregate] = []
for key in sorted(aggregates):
Expand Down Expand Up @@ -243,9 +245,11 @@ def _scan_maximizers(

best_score = target_score
maximizers: list[_PointAggregate] = []
qx, qy = query

for point in self._points:
score = dot_2d(point.key, query)
px, py = point.key
score = (px * qx) + (py * qy)
if best_score is None or score > best_score:
best_score = score
maximizers = [point]
Expand Down
1 change: 0 additions & 1 deletion tests/test_bytecode_harness.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
from __future__ import annotations

from bytecode import (
accumulator_loop_program,
alternating_memory_loop_bytecode_program,
arithmetic_smoke_program,
branch_then_call_false_program,
Expand Down
1 change: 0 additions & 1 deletion tests/test_bytecode_memory_surfaces.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
from bytecode import (
analyze_memory_surfaces,
call_frame_roundtrip_program,
countdown_helper_call_program,
memory_surface_cases,
memory_surface_negative_programs,
run_memory_surface_case,
Expand Down
1 change: 0 additions & 1 deletion tests/test_model_softmax_baseline.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
evaluate_free_running_rollout,
evaluate_teacher_forced_model,
require_torch,
serialize_event_tokens,
SoftmaxBaselineConfig,
SoftmaxTrainingConfig,
summarize_trace_sequences,
Expand Down