Skip to content

perf(shuffle): cache the ranked library — ~2.2s per shuffle to instant - #145

Merged
aperson30 merged 1 commit into
mainfrom
perf/shuffle-cache-ranked-df
Aug 14, 2026
Merged

perf(shuffle): cache the ranked library — ~2.2s per shuffle to instant#145
aperson30 merged 1 commit into
mainfrom
perf/shuffle-cache-ranked-df

Conversation

@aperson30

Copy link
Copy Markdown
Collaborator

What

Makes shuffle much faster by caching the deterministic part of /api/random for the default "all" scope.

The problem (measured on the live data)

Every shuffle click rebuilt everything from scratch: pd.concat(PanTS + CancerVerse) (~32k rows) → .copy() → add sort columns → re-run the full quality ranking. Measured cost: 2.188s per shuffle of pure backend work.

The fix

That whole chain is deterministic for scope "all" (the dataset is loaded once at startup), so:

  • _all_dataset_prepared() caches concat + sort-cols,
  • _ranked_all(n) caches the quality-ranked frame per pick-size n.

The per-request bits — recent-exclusion and the rotating offset — are applied on top (with a defensive copy so the shared cached frame is never mutated). The filtered-scope path is byte-for-byte unchanged.

Verified on the server (real data)

OLD full path : 2.188 s
cached (warm) : 0.005 ms   → ~480,000x
same_len=True             → ranking identical to the old path

So the first shuffle after a restart costs ~2.2s (warms the cache); every one after is instant. py_compile passes.

Each shuffle re-concatenated PanTS+CancerVerse (~32k rows), copied it, added sort
columns, and re-ran the full quality ranking — all deterministic for the default
'all' scope. Cache the concat+sort-cols (_all_dataset_prepared) and the ranked frame
per pick-size n (_ranked_all); apply recent-exclusion + the rotating offset per
request on top (a defensive copy guards the shared frame). The filtered-scope path is
unchanged. Turns each shuffle from 'rank 32k rows' into a slice.
@aperson30
aperson30 merged commit fdb2f70 into main Aug 14, 2026
9 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.

1 participant