Skip to content

perf/ram: coalesce traffic commits, single-scan announcement counts, prune anomaly state#77

Merged
Egor051 merged 1 commit into
mainfrom
claude/audit-optimization-9IL2d
May 21, 2026
Merged

perf/ram: coalesce traffic commits, single-scan announcement counts, prune anomaly state#77
Egor051 merged 1 commit into
mainfrom
claude/audit-optimization-9IL2d

Conversation

@Egor051
Copy link
Copy Markdown
Owner

@Egor051 Egor051 commented May 21, 2026

Summary

Acts on the verified findings from a repo-wide optimization audit (performance / RAM / dead code). Five substantive, hand-verified fixes — false positives and cosmetic items from the audit were excluded.

  • P1 — perf (med): services/traffic_stats.py refresh_views now wraps the per-key upsert loop in one db.transaction(). Each upsert's own commit() is a no-op inside an explicit transaction, so a batch of N keys flushes once instead of issuing N fsyncs (synchronous=FULL). refresh_all_awg paginates hundreds of keys per run — this collapses hundreds of disk flushes into one per batch. Repo methods unchanged (safe for any future standalone caller).
  • P2 — perf (low-med): _ledger_result replaced three per-status SELECTs over announcement_deliveries with a single grouped query (delivery_user_ids_grouped); the now-unused delivery_user_ids_by_status was removed.
  • P3 — perf (low): refresh_batch_counts collapsed from three correlated COUNT(*) subqueries into one UPDATE ... FROM conditional-aggregate scan.
  • R1 — ram (med): anomaly_detection._check_thresholds now drops a key from _observations/_last_alerted once its samples age out of the window, so the maps no longer accumulate an entry per key ever observed.
  • D2 — dead (low): hoisted InlineKeyboardButton to the module-level import in bot/handlers/admin.py and removed two redundant in-function re-imports.

DEFAULT_PROXY_* config keys were confirmed unused but intentionally kept (documented legacy env contract).

Test plan

  • python -m pytest -q — full suite, 557 passed
  • Targeted: test_traffic_stats_hardening, test_anomaly_detection, test_key_deletion_and_announcements, test_high_blockers_and_names
  • py_compile on all changed files
  • (optional) Manual: trigger an announcement broadcast and an AWG traffic refresh to confirm counts/views unchanged

https://claude.ai/code/session_01Rfqq8EzQ7s5cY9FVLewVdn


Generated by Claude Code

…e anomaly state

- traffic_stats: wrap per-batch refresh upserts in one transaction so the
  per-row commits coalesce into a single fsync instead of one per key
- announcements: replace three per-status delivery scans with one grouped
  query, and collapse refresh_batch_counts into a single conditional-aggregate
  UPDATE...FROM scan
- anomaly_detection: drop key entries from the observation maps once their
  samples age out of the window so the dicts don't grow unbounded
- admin handlers: hoist InlineKeyboardButton to the module import and remove
  the redundant in-function re-imports

https://claude.ai/code/session_01Rfqq8EzQ7s5cY9FVLewVdn
@chatgpt-codex-connector
Copy link
Copy Markdown

You have reached your Codex usage limits for code reviews. You can see your limits in the Codex usage dashboard.

@Egor051 Egor051 merged commit e5c6bf8 into main May 21, 2026
4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants