mirror of
https://github.com/burrow-dev/burrow-server.git
synced 2026-11-19 01:27:51 -05:00
perf(defpacks): make the item packs again without holding the game up
A custom item change made the item and name packs again on the next fetch, on the one thread every game shares: ~360 ms at 5000 items, ~1.6 s at 50000, after every change, players making them many times a minute. Nearly all of it was the AMF3 writer. - the writer fills one growing buffer instead of a Buffer per value (byte-identical output, checked against the old one on every cached pack): the item pack 230 -> 52 ms at 5000 items, 1524 -> 231 ms at 50000. - a changed pack is made again at most every AJ_PACK_REMAKE_MS (15 s); until then the one made before goes out with the custom item version it has, and customItems.sendAll sends a game logging in what its packs lack by that version. one remake at a time; deflated on libuv's threads. - tests make the packs again at once (AJ_PACK_REMAKE_MS=0), and a run of the items test checks the throttled case. Busy evening simulated in-process (a change every 3 s, packs fetched 6 a second, 30 s): longest hold-up 329 -> 61 ms at 5000 items, 1648 -> 359 ms at 50000 (p99 240 -> 6 ms). Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
1 parent
c5b1bc37d9
commit
04b8d325ea
7 files changed
+178
-64
No files matched your search
+41
-13
@@ -370,26 +370,51 @@ const CLIENT_PACKS = new Map([
|
||||
[DEF_NAMEBAR_BADGE, () => {}],
|
||||
]);
|
||||
const clientPackPaths = new Map([...CLIENT_PACKS.keys()].map(id => [cdn.hashedPath(`defPacks/${id}`), id]));
|
||||
const clientPackBodies = new Map(); // id -> {day, allOnSale, body}
|
||||
const clientPackBodies = new Map(); // id -> {day, allOnSale, body, version, madeAt}
|
||||
const clientPacksChanged = new Set(); // ids whose body is older than a custom item change
|
||||
const clientPacksMaking = new Map(); // id -> the promise of the body being made
|
||||
// a pack is made again after a custom item change at most every
|
||||
// config.packRemakeMs: the item pack takes ~70 ms of the one thread at 5000
|
||||
// items (~300 ms at 50000), and players make items many times a minute.
|
||||
// until then the body made before goes out, with the version it has; a
|
||||
// game logging in is sent what changed after it (lib/customitems.js sendAll).
|
||||
const deflateRaw = require("util").promisify(zlib.deflateRaw);
|
||||
// the packs custom items are in: their rows, and their names
|
||||
const CUSTOM_PACK_KINDS = new Map([[DEF_ITEM, "clothing"], [DEF_DEN_ITEM, "den"], [DEF_LOCALIZATION_EN, "names"]]);
|
||||
|
||||
// the pack at `relPath` as clients should get it, or null when it is not
|
||||
// one of the above (and comes straight from the CDN).
|
||||
const makeClientPack = async id => {
|
||||
// the version before the rows are read: a change while it is made counts
|
||||
// as after, and is sent at login (more than needed, never less)
|
||||
const version = customVersion;
|
||||
const pack = await loadChanged(id);
|
||||
CLIENT_PACKS.get(id)(pack);
|
||||
// deflated on libuv's threads, not this one
|
||||
const body = await deflateRaw(amf3.encode(pack));
|
||||
const made = {day: sales.today(), allOnSale: sales.everythingOnSale(), body, version, madeAt: Date.now()};
|
||||
clientPackBodies.set(id, made);
|
||||
if (customVersion === version) {
|
||||
clientPacksChanged.delete(id);
|
||||
}
|
||||
return made;
|
||||
};
|
||||
|
||||
// the pack at `relPath` as clients should get it, {body, version}, or null
|
||||
// when it is not one of the above (and comes straight from the CDN).
|
||||
// `version`: customVersion() of the custom items in it.
|
||||
const clientPack = async relPath => {
|
||||
const id = clientPackPaths.get(relPath);
|
||||
if (id === undefined) {
|
||||
return null;
|
||||
}
|
||||
const made = clientPackBodies.get(id);
|
||||
if (made && made.day === sales.today() && made.allOnSale === sales.everythingOnSale()) {
|
||||
return made.body;
|
||||
if (made && made.day === sales.today() && made.allOnSale === sales.everythingOnSale()
|
||||
&& (!clientPacksChanged.has(id) || Date.now() - made.madeAt < config.packRemakeMs)) {
|
||||
return made;
|
||||
}
|
||||
const pack = await loadChanged(id);
|
||||
CLIENT_PACKS.get(id)(pack);
|
||||
const body = zlib.deflateRawSync(amf3.encode(pack));
|
||||
clientPackBodies.set(id, {day: sales.today(), allOnSale: sales.everythingOnSale(), body});
|
||||
return body;
|
||||
if (!clientPacksMaking.has(id)) {
|
||||
clientPacksMaking.set(id, makeClientPack(id).finally(() => clientPacksMaking.delete(id)));
|
||||
}
|
||||
return clientPacksMaking.get(id);
|
||||
};
|
||||
|
||||
// most packs are tables: an object of rows keyed by row index.
|
||||
@@ -537,8 +562,8 @@ const setCustomItem = (kind, row, name, inShop = true) => {
|
||||
clothingIds = clothingIds.includes(id) ? clothingIds : [...clothingIds, id];
|
||||
}
|
||||
customInShop[kind][inShop ? "add" : "delete"](id);
|
||||
clientPackBodies.delete(pack);
|
||||
clientPackBodies.delete(DEF_LOCALIZATION_EN);
|
||||
clientPacksChanged.add(pack);
|
||||
clientPacksChanged.add(DEF_LOCALIZATION_EN);
|
||||
customVersion++;
|
||||
};
|
||||
|
||||
@@ -560,7 +585,7 @@ const unsetCustomItem = (kind, id) => {
|
||||
clothingIds = clothingIds.filter(one => one !== number);
|
||||
}
|
||||
customInShop[kind].delete(number);
|
||||
clientPackBodies.delete(pack);
|
||||
clientPacksChanged.add(pack);
|
||||
customVersion++;
|
||||
};
|
||||
|
||||
@@ -571,6 +596,9 @@ module.exports = {
|
||||
// the custom items on sale, the newest first.
|
||||
customShopIds: kind => [...customInShop[kind]].reverse(),
|
||||
customVersion: () => customVersion,
|
||||
// a change of a custom item that is not in the packs (its picture): one
|
||||
// more version, for a game logging in to be told of it
|
||||
customChanged: () => ++customVersion,
|
||||
// the avatarUseFlags every clothing row has in the clients' pack (any animal)
|
||||
EVERY_AVATAR,
|
||||
// "clothing", "den" or "names" for a pack path with custom items in (lib/customitems.js sawPack), else null.
|
||||
|
||||
Reference in new issue
Block a user