perf: check tnr_cache before discover_max_tnr (instant search)
This commit is contained in:
parent
9a583c8fa5
commit
34dc8b7ac6
1 changed files with 8 additions and 15 deletions
|
|
@ -525,17 +525,7 @@ def scan_for_player(fide_id: int, max_tnr: int = None,
|
||||||
results = []
|
results = []
|
||||||
seen_tnrs = set()
|
seen_tnrs = set()
|
||||||
|
|
||||||
# Real TNR range on chess-results is not contiguous — multiple clusters exist.
|
# Phase 1: check tnr_cache (instant SQL, no HTTP). Return immediately if found.
|
||||||
# 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).
|
|
||||||
for cluster_lo, cluster_hi in [(1434000, 1450000)]:
|
for cluster_lo, cluster_hi in [(1434000, 1450000)]:
|
||||||
conn = _get_conn()
|
conn = _get_conn()
|
||||||
cached_tnrs = [
|
cached_tnrs = [
|
||||||
|
|
@ -545,9 +535,6 @@ def scan_for_player(fide_id: int, max_tnr: int = None,
|
||||||
]
|
]
|
||||||
conn.close()
|
conn.close()
|
||||||
for tnr in cached_tnrs:
|
for tnr in cached_tnrs:
|
||||||
if tnr in seen_tnrs:
|
|
||||||
continue
|
|
||||||
seen_tnrs.add(tnr)
|
|
||||||
cdata = _get_cached_tnr(tnr)
|
cdata = _get_cached_tnr(tnr)
|
||||||
if cdata is None:
|
if cdata is None:
|
||||||
continue
|
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)))
|
set_tnr_state('max_tnr_seen', str(max(r['tnr'] for r in results)))
|
||||||
return 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 start, win_limit in windows:
|
||||||
for tnr in range(start, max(1, start - win_limit), -1):
|
for tnr in range(start, max(1, start - win_limit), -1):
|
||||||
if tnr in seen_tnrs:
|
if tnr in seen_tnrs:
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue