Skip to content
Closed
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
4 changes: 4 additions & 0 deletions .jules/bolt.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,7 @@
## 2026-06-10 - Optimize redundant dictionary lookups in tight loops
**Learning:** Using `dict.setdefault` and multiple `dict.get` or `key in dict` checks inside tight loops significantly impacts performance due to repeated dictionary lookups and unnecessary list allocations. Caching dictionary lookups (e.g., using a single `dict.get(key)`) and conditionally handling the logic based on the result is much more performant.
**Action:** When aggregating or grouping items in a loop, avoid `setdefault`. Instead, check if the key exists using a single `.get()`, and perform initialization/updates conditionally. Additionally, hoist loop-invariant checks (e.g., `folder == "sent"`) outside the loop to avoid redundant evaluations.

## 2026-06-10 - Optimize redundant dictionary lookups in tight loops
**Learning:** Using `dict.setdefault` and multiple `dict.get` or `key in dict` checks inside tight loops significantly impacts performance due to repeated dictionary lookups and unnecessary list allocations. Caching dictionary lookups (e.g., using a single `dict.get(key)`) and conditionally handling the logic based on the result is much more performant.
**Action:** When aggregating or grouping items in a loop, avoid `setdefault`. Instead, check if the key exists using a single `.get()`, and perform initialization/updates conditionally. Additionally, hoist loop-invariant checks (e.g., `folder == "sent"`) outside the loop to avoid redundant evaluations.
23 changes: 15 additions & 8 deletions backend/api/emails.py
Original file line number Diff line number Diff line change
Expand Up @@ -284,11 +284,15 @@ async def get_emails(
if message_is_from_user(email, user_addresses):
has_sent_message[group_key] = True

visible_groups = [
email
for group_key, email in grouped.items()
if folder != "sent" or has_sent_message.get(group_key, False)
]
if is_sent_folder:
visible_groups = [
email
for group_key, email in grouped.items()
if has_sent_message.get(group_key, False)
]
else:
visible_groups = list(grouped.values())

sorted_groups = sorted(visible_groups, key=lambda x: x.date, reverse=True)[:limit]

items = []
Expand Down Expand Up @@ -391,12 +395,15 @@ def _build_email_lookup_dicts(
by_fingerprint: dict[str, Email] = {}
for email_row in existing_emails:
for lookup_value in _email_message_lookup_values(email_row):
by_message_id.setdefault(lookup_value, email_row)
if lookup_value not in by_message_id:
by_message_id[lookup_value] = email_row
row_fingerprint = email_strong_fingerprint(email_row)
if row_fingerprint:
by_fingerprint.setdefault(row_fingerprint, email_row)
if row_fingerprint not in by_fingerprint:
by_fingerprint[row_fingerprint] = email_row
if email_row.fingerprint:
by_fingerprint.setdefault(email_row.fingerprint, email_row)
if email_row.fingerprint not in by_fingerprint:
by_fingerprint[email_row.fingerprint] = email_row
return by_message_id, by_fingerprint


Expand Down
Loading