Skip to content

perf(statistics): scale large aggregate buckets (gates F8 mace all-time) - #13

Merged
Afgan0r merged 2 commits into
masterfrom
fix/perf-large-bucket-identity
Jun 14, 2026
Merged

Afgan0r merged 2 commits into
masterfrom
fix/perf-large-bucket-identity

Conversation

@Afgan0r

@Afgan0r Afgan0r commented Jun 14, 2026

Copy link
Copy Markdown
Contributor

Summary

Behavior-preserving perf fix for large aggregate recalc buckets. After F8 (#12), the mace all-time bucket is ONE recalc pass over ~20,735 replays; the parity-driver flagged it stalls. Output is byte-identical — this is perf only.

Addresses the three perf findings from the Phase 1 review (3/4/5):

  • O(1) identity resolution — bestPlayerIdentity scanned the entire identities array per player per replay (O(replays × players × |identities|)). Replaced with buildPlayerIdentityIndex (steam_id + lower(name) maps, original array order preserved) + bestPlayerIdentityIndexed, scoring only the few reachable candidates with the unchanged priority function and first-in-array tie-break. Player + commander paths.
  • Set-based fallback resolve + index — new migration 0010 adds the functional index idx_canonical_players_display_name_lower (mirrors the existing lower(nickname) index). ensureNameFallbackIdentities resolve goes from a per-occurrence correlated where exists to two index-sargable EXISTS halves with a provably identical predicate.
  • Batched inserts — replaceAggregateRows/replaceCommanderRows/replaceBountyRows now do one insert … select * from unnest(…) per table instead of a per-row await loop inside the transaction.

Byte-identity proof

  • The real-pg parity harness (185 integration tests: per-replay vs set-based, per-type, all-time, is_show) stays green — the strongest oracle.
  • New identity-index.test.ts differentially cross-checks the index vs a verbatim copy of the OLD scan over an adversarial seeded corpus across a timestamp sweep.
  • New batched-write.test.ts asserts exactly one insert per table and a bounded set-based fallback resolve.

Code review: APPROVE (no blockers; 3 behavior-equivalent medium notes documented in PERF-REVIEW.md). pnpm verify green: 100% coverage, OpenAPI contract diff empty.

Deploy: pnpm db:migrate applies index 0010 (additive, index-only, no recalc needed).

🤖 Generated with Claude Code

Afgan0r and others added 2 commits June 14, 2026 18:39
… resolve, batched inserts)

Behavior-preserving perf fixes for the F8 large all-time buckets (gates the
~20735-replay mace all-time recalc). 01-REVIEW findings 3/4/5.

- Finding 3: replace the per-player/per-replay O(|identities|) bestPlayerIdentity
  scan with a steam_id + lower(name) lookup index built once per bucket; resolves
  the SAME identity (same steam>nickname>display priority, same first-in-array
  tie-break). Applied to player and commander paths.
- Finding 4: add migration 0010 functional index on lower(canonical_players.
  display_name) (mirrors idx_player_nicknames_nickname); restructure the
  name-fallback resolve from a per-occurrence correlated `where exists` (OR across
  cp x pn) into one set-based pass of two index-sargable EXISTS halves (identical
  predicate). loadPlayerIdentities fan-out left as-is (bounded; dedup would risk
  tie-break drift).
- Finding 5: convert replaceAggregateRows/replaceCommanderRows/replaceBountyRows
  from per-row INSERT loops to a single multi-row `insert ... select from unnest`
  per table (byte-identical rows; jsonb columns JSON-serialized per element).

Tests: identity-index cross-checks the index vs the prior per-player scan on an
adversarial seeded set across a timestamp sweep; batched-write asserts one insert
per table and a bounded (1) set-based fallback resolve. Existing real-pg parity
harness (per-replay vs set-based, per-type, all-time, is_show) stays green,
proving byte-identity. Full pnpm verify green: 100% coverage, OpenAPI diff empty.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
@Afgan0r
Afgan0r merged commit 640724f into master Jun 14, 2026
3 checks passed
@Afgan0r
Afgan0r deleted the fix/perf-large-bucket-identity branch June 14, 2026 12:00
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