Skip to content

fix(api): save players and sessions to Mongoose as nested documents - #1005

Open
ugcodrr wants to merge 3 commits into
mainfrom
fix/nested-saves
Open

ugcodrr wants to merge 3 commits into
mainfrom
fix/nested-saves

Conversation

@ugcodrr

@ugcodrr ugcodrr commented Oct 21, 2026 •

Copy link
Copy Markdown
Member

Description: Player saves and session resets passed Mongoose a flat document with thousands of dotted keys, and its change tracking slows down with the square of that count.

  1. Saving a heavy player (about 3,200 keys) goes from about 101 ms to 18 ms, and a heavy session reset from 103 ms to 19 ms. Stored documents don't change.

#1006 is stacked on this PR. Merge order for the perf PRs is in #1002.

Testing: save the same big player on main and here. The stored documents match.

PlayerService.saveOne and SessionService.resetPlayer handed Mongoose the
flat serialized document, with keys like "stats.bedwars.overall.wins".
Mongoose casts a replacement by building a document and setting each key,
and its change tracking rescans every modified path on each set, so a save
with K keys cost O(K²): about 100 ms for a player with 3,200 keys.

Passing the same document unflattened avoids that: about 18 ms for that
player, and 6.5 ms instead of 12 ms for an average one. The stored
documents are byte-identical, BSON number types and key order included.

Guild saves keep the flat document. Their cost is casting the members
array, not the dotted keys, so nesting doesn't change it.
@vercel

vercel Bot commented Oct 21, 2026 •

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

1 Skipped Deployment
Project Deployment Actions Updated
statsify Ignored Ignored Nov 5, 2026 8:06am UTC

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant