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:
BurrowdevandClaude Opus 5.5 committed 2026-10-20 14:44:07 +03:00
1 parent c5b1bc37d9
commit 04b8d325ea
7 files changed
+178 -64

No files matched your search

+3 -1
View File
@@ -166,7 +166,9 @@ const withServer = async (name, httpPort, gamePort, run, {dataDir: keptDir = nul
// everyone arrives in the township, as the tests expect
AJ_SPAWN_ROOMS: process.env.AJ_SPAWN_ROOMS || "jamaa_township.room_main",
// and in the one world
AJ_WORLDS: process.env.AJ_WORLDS || "1"},
AJ_WORLDS: process.env.AJ_WORLDS || "1",
// the packs made again at once after a custom item changes
AJ_PACK_REMAKE_MS: process.env.AJ_PACK_REMAKE_MS || "0"},
stdio: ["ignore", "pipe", "inherit"],
});
let output = "";
+23
View File
@@ -632,11 +632,34 @@ const second = async () => {
lou.client.socket.destroy();
};
// the packs made again at most every AJ_PACK_REMAKE_MS after a change: one
// fetched before then is the one made before, and a game whose address
// fetched it is sent at login the item it lacks.
const REMAKE_MS = 5000;
const third = async () => {
const fetchedEarlier = "127.0.0.3";
await packsFrom(fetchedEarlier);
const lou = await arrive("Lou", people.Lou);
lou.client.xt("xcm", lou.roomId, "make", "clothing", made.base.id, "Lou Hat");
const answer = await lou.client.untilXt("xcm");
assert.strictEqual(answer[2], "1", answer[3]);
const louHat = JSON.parse(answer[6]).find(entry => entry.name === "Lou Hat");
assert.ok(!byId(await pack(1000), louHat.id)); // the pack of before, for a moment
const atLogin = await defsAtLogin("Pat", people.Pat, fetchedEarlier);
assert.deepStrictEqual(atLogin.clothing.filter(row => row.id === String(louHat.id)).map(row => row.name), ["Lou Hat"]);
await new Promise(resolve => setTimeout(resolve, REMAKE_MS + 200));
assert.strictEqual(byId(await pack(1000), louHat.id).titleStrId, String(9100000 + louHat.id - 50000));
assert.strictEqual((await strings())[9100000 + louHat.id - 50000], "Lou Hat");
lou.client.socket.destroy();
};
(async () => {
const dataDir = fs.mkdtempSync(path.join(os.tmpdir(), "aj-test-"));
try {
await harness.withServer("items test", HTTP_PORT, GAME_PORT, first, {dataDir});
await harness.withServer("items test (after a restart)", HTTP_PORT, GAME_PORT, second, {dataDir});
process.env.AJ_PACK_REMAKE_MS = String(REMAKE_MS);
await harness.withServer("items test (packs made again at most so often)", HTTP_PORT, GAME_PORT, third, {dataDir});
}
finally {
fs.rmSync(dataDir, {recursive: true, force: true, maxRetries: 10, retryDelay: 200});