perf(server): index profile equipment and research refreshes
Replace O(N^2) linear lookups and repeated array filtering across modifications, weapons, modsResearch and unitResearch with single-pass Map indexes, pre-sort catalogue equipment once on load, avoid redundant pre-change disk writes in PlayerProfiles.updateWhen, and shallow-copy only top-level lists in clientProfile.
This commit is contained in:
1 parent
ebb2937a38
commit
62da2e3038
10 files changed
+401
-155
No files matched your search
@@ -15,7 +15,12 @@ import { clearStoreCart } from './profile-store-cart.ts';
|
||||
import { serverTimeNow } from './server-time.ts';
|
||||
|
||||
export function clientProfile(saved: BlkBlock, account: Account): BlkBlock {
|
||||
const profile = structuredClone(saved);
|
||||
// Shallow-copy only the top-level lists; child blocks are never mutated here.
|
||||
const profile: BlkBlock = {
|
||||
name: saved.name,
|
||||
params: [...saved.params],
|
||||
blocks: [...saved.blocks],
|
||||
};
|
||||
clearStoreCart(profile);
|
||||
const pilotIcon = appearance(profile).pilotIcon || DEFAULT_PILOT_ICON;
|
||||
setParam(profile, 'pilotIcon', { type: 'string', value: pilotIcon });
|
||||
|
||||
@@ -29,7 +29,8 @@ export class PlayerProfiles {
|
||||
}
|
||||
|
||||
profileOf(userId: string): BlkBlock {
|
||||
const profile = this.#saves.loadProfile(userId) ?? newProfile(this.#gameFiles.newAccountTemplate(), this.#startingContent);
|
||||
const loaded = this.#saves.loadProfile(userId);
|
||||
const profile = loaded ?? newProfile(this.#gameFiles.newAccountTemplate(), this.#startingContent);
|
||||
this.#refresh(profile, userId);
|
||||
this.#saves.saveProfile(userId, profile);
|
||||
return profile;
|
||||
@@ -46,10 +47,20 @@ export class PlayerProfiles {
|
||||
/**
|
||||
* Change the player's profile if `change` accepts (returns true); the change must not touch
|
||||
* the profile when it refuses. Returns the saved profile, or undefined when refused.
|
||||
*
|
||||
* Runs one pre-change refresh in memory (without an extra disk write) so handlers see current
|
||||
* derived state, then refreshes and persists once when the change succeeds.
|
||||
*/
|
||||
updateWhen(userId: string, change: (profile: BlkBlock) => boolean): BlkBlock | undefined {
|
||||
const profile = this.profileOf(userId);
|
||||
if (!change(profile)) return undefined;
|
||||
const loaded = this.#saves.loadProfile(userId);
|
||||
const profile = loaded ?? newProfile(this.#gameFiles.newAccountTemplate(), this.#startingContent);
|
||||
if (!loaded) {
|
||||
this.#refresh(profile, userId);
|
||||
}
|
||||
if (!change(profile)) {
|
||||
if (!loaded) this.#saves.saveProfile(userId, profile);
|
||||
return undefined;
|
||||
}
|
||||
this.#refresh(profile, userId);
|
||||
this.#saves.saveProfile(userId, profile);
|
||||
return profile;
|
||||
|
||||
Reference in new issue
Block a user