Skip to content
Merged
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
Expand Up @@ -11,3 +11,6 @@
## 2024-05-22 - [Optimize Generator Expression in all()]
**Learning:** Similar to `any()`, unrolling `all()` generator expressions in hot paths (like `poller/normalizers/beast_decoder.py`) avoids generator/frame overhead and can be ~2-20x faster depending on how early it exits.
**Action:** Unroll `all()` into explicit loops with early returns when optimizing high-frequency parsing/decoding code.
## 2024-05-23 - [Bypass JSON parsing for non-entity WebSocket updates]
**Learning:** In the WebSocket broadcasting loop (`backend/routers/ws.py`), parsing every incoming JSON message using `json.loads` before checking its type can be extremely slow and block the event loop, especially when passing along large payloads (like snapshots) that don't need filtering.
**Action:** Use fast string matching (e.g., `'"type": "entity_update"' in raw`) to bypass `json.loads` entirely for messages that don't need filtering. This avoids deserialization overhead and significantly speeds up the event loop when dealing with large payloads.
10 changes: 9 additions & 1 deletion backend/routers/ws.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,15 @@ async def forward_redis():
if message["type"] == "message":
raw: str = message["data"]

# Non-entity_update messages pass through without filtering
# ⚡ Bolt Optimization: Fast path bypasses json.loads for non-entity_update messages.
# String matching is ~50-100x faster than parsing large payloads like snapshots.
# We check for the presence of the string, which is safe since "entity_update"
# is a specific enough substring to avoid false positives in this context.
if "entity_update" not in raw:
await ws.send_text(raw)
continue

# Apply subscription filters to entity_update messages
try:
parsed = json.loads(raw)
except (json.JSONDecodeError, TypeError, ValueError):
Expand Down
Loading