Files
offline-db/patch_badge_system_all.py
6e2f5d30e2 ⚡ Bolt: [performance improvement] fix N+1 query in check_badges loop (#347)
Co-authored-by: google-labs-jules[bot] <161369871+google-labs-jules[bot]@users.noreply.github.com>
Co-authored-by: Senpai-YoloBot <41898282+github-actions[bot]@users.noreply.github.com>
2026-04-19 21:10:44 +00:00

26 lines
1.2 KiB
Python

import re
with open('users/badge_system.py', 'r') as f:
content = f.read()
# Instead of passing the empty dict `strategy_cache` around during `check_badges(user)`, we can pre-populate it!
# Wait, actually, the problem is that multiple functions use `.objects.` calls inside `badge_system.py`.
# I should patch `users/services.py` directly to pass the `cache=strategy_cache` and let `badge_system.py` just use it.
# Actually, the instructions say "N+1 Query in users/services.py check_badges()", and point out:
# "Clear N+1 issue where DB queries are triggered inside strategy checks during the loop."
# Currently in `check_badges`:
# strategy_cache = {}
# for strategy in GENERAL_BADGE_STRATEGIES:
# strategy.check(user, awarded_slugs, all_badges, new_badges, cache=strategy_cache)
# And inside the strategies, things are lazy loaded.
# But `WatchLog.objects.filter(user=user)...` is being called multiple times across multiple strategies.
# If I pre-fetch these and put them in `strategy_cache`, the strategies can use them directly!
# BUT!
# "N+1 Query in users/services.py check_badges()"
# "DB queries are triggered inside strategy checks during the loop."
# The goal is to optimize `check_badges` and `check_chat_badges` in `services.py`.