perf: check tnr_cache before discover_max_tnr (instant search)

This commit is contained in:
vrubel 2026-06-20 08:56:18 +00:00
parent 9a583c8fa5
commit 34dc8b7ac6

View file

@ -525,17 +525,7 @@ def scan_for_player(fide_id: int, max_tnr: int = None,
results = []
seen_tnrs = set()
# Real TNR range on chess-results is not contiguous — multiple clusters exist.
# Scan from multiple starting points to cover both current (\u223c1.44M) and
# older (\u223c500K) clusters, plus a forward probe from the saved max.
saved = int(get_tnr_state('max_tnr_seen', '0'))
if max_tnr is None:
max_tnr = max(saved, discover_max_tnr())
# Scan windows: (start, limit) — cover known clusters
windows = [(max_tnr, limit)]
# Search tnr_cache for the player in the 1.4M cluster (instant SQL, no HTTP).
# Cache is populated by warmup_cache at startup (step 5 full scan).
# Phase 1: check tnr_cache (instant SQL, no HTTP). Return immediately if found.
for cluster_lo, cluster_hi in [(1434000, 1450000)]:
conn = _get_conn()
cached_tnrs = [
@ -545,9 +535,6 @@ def scan_for_player(fide_id: int, max_tnr: int = None,
]
conn.close()
for tnr in cached_tnrs:
if tnr in seen_tnrs:
continue
seen_tnrs.add(tnr)
cdata = _get_cached_tnr(tnr)
if cdata is None:
continue
@ -568,7 +555,13 @@ def scan_for_player(fide_id: int, max_tnr: int = None,
set_tnr_state('max_tnr_seen', str(max(r['tnr'] for r in results)))
return results
# Fallback: sequential scan from discovered max_tnr window
# Phase 2: cache miss — fall back to sequential TNR scan
saved = int(get_tnr_state('max_tnr_seen', '0'))
if max_tnr is None:
max_tnr = max(saved, discover_max_tnr())
windows = [(max_tnr, limit)]
for start, win_limit in windows:
for tnr in range(start, max(1, start - win_limit), -1):
if tnr in seen_tnrs: