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 @@
## 2024-04-27 - [Avoid setdefault with eager initialization in hot loops]
**Learning:** Using `dict.setdefault(key, complex_initialization())` inside a hot loop forces the Python interpreter to eagerly evaluate the default value (such as a list comprehension with `Fraction(0)`) on every single iteration, even if the key already exists. This can cause severe performance degradation, especially when dealing with computationally expensive object creation or large iterations.
**Action:** Replace `dict.setdefault` with explicit `if key not in dict:` checks when the default value involves function calls, list comprehensions, or object instantiations. This ensures the default value is only evaluated when necessary (i.e., on cache miss).
13 changes: 8 additions & 5 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,10 +204,14 @@ 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": []},
)
# Performance optimization: explicit check to avoid eagerly evaluating default dict
if key not in aggregates:
aggregates[key] = {
"value_sum": [Fraction(0) for _ in value],
"count": 0,
"entry_indices": [],
}
bucket = aggregates[key]
Comment on lines +207 to +214

Copilot AI Apr 27, 2026

Copy link

Choose a reason for hiding this comment

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

In this hot loop, if key not in aggregates: followed by bucket = aggregates[key] performs two dict lookups on the common (key-present) path. For maximum performance, consider a single-lookup pattern (e.g., try/except KeyError or bucket = aggregates.get(key) with a sentinel) so the steady-state path only hashes the key once per iteration.

Suggested change
# Performance optimization: explicit check to avoid eagerly evaluating default dict
if key not in aggregates:
aggregates[key] = {
"value_sum": [Fraction(0) for _ in value],
"count": 0,
"entry_indices": [],
}
bucket = aggregates[key]
try:
bucket = aggregates[key]
except KeyError:
bucket = {
"value_sum": [Fraction(0) for _ in value],
"count": 0,
"entry_indices": [],
}
aggregates[key] = bucket

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
Expand Down