Skip to content

A fast 2D and 3D game engine — TypeScript SDK, C++/WebAssembly core, visual editor. Ship one project to web, desktop, WeChat MiniGames, playable ads, and native Android / iOS.

perf(net): a budget for the sample, and it is not paying for the wire

esengine2a13cad2199d
diff --git a/bench/sample-budget/README.md b/bench/sample-budget/README.md
new file mode 100644
index 000000000..3e391bef0
--- /dev/null
+++ b/bench/sample-budget/README.md
@@ -0,0 +1,109 @@
+# What a replication sample is actually spent on
+
+Seven population scans came out of this path — registry, dirty discovery,
+ownership, position reads, grid rebuild, row routing, idle visibility. Guessing
+the next wall from the source after that is how you start optimising second-order
+terms. So this stops guessing and takes a budget instead.
+
+```
+node bench/sample-budget/arm.mjs --scenario mixed
+```
+
+100k entities, 32 connections, ~1% visible, the shipped `radiusInterestProvider`,
+the real `ReplicationServer` sampling every fixed tick. Every phase is timed
+where it happens; `other` is `total` minus the sum of the leaves, so **a budget
+that does not close has not been decomposed**. These close to within 0.5%.
+
+## The four workloads
+
+| | what moves |
+|---|---|
+| **still** | nothing — the N3e baseline. A health check, not a subject. |
+| **movement** | 1% of the population, anchors stationary |
+| **anchors** | that, and every connection's OWN anchor as well |
+| **mixed** | that, plus 1% dirty fields and 0.1% component removals |
+
+**`anchors` is mandatory, not a variant.** A connection's owned anchor is one of
+its query's inputs, so it invalidates that connection by itself. Any claim that a
+movement workload got cheaper has to survive it or it is a benchmark-only cache
+(see `bench/interest-regional/`).
+
+## The budget, µs per sample
+
+| phase | still | movement | anchors | mixed |
+|---|---|---|---|---|
+| composition | 0 | 914 | 919 | 873 |
+| registry | 0 | 2 | 3 | 2 |
+| owner index | 0 | 1 | 1 | 1 |
+| interest prepare | 3 | 802 | 871 | 830 |
+| **visibility query** | 0 | **3,849** | 3,972 | 3,768 |
+| visibility diff | 0 | 895 | 1,342 | 1,235 |
+| **dirty discovery** | 1 | **4,225** | 4,360 | **7,521** |
+| routing | 2 | 185 | 213 | 412 |
+| **spawn payload** | 0 | 136 | **9,940** | **9,608** |
+| frame encode | 0 | 63 | 89 | 263 |
+| control send | 2 | 61 | 1,611 | 1,516 |
+| transport send | 0 | 44 | 35 | 33 |
+| ack + reader floors | 2 | 6 | 10 | 12 |
+| other | 8 | 32 | 112 | 101 |
+| **sample total** | **19** | **11,216** | **23,477** | **26,176** |
+| accounted | 58% | **99.7%** | **99.5%** | **99.6%** |
+| enters / leaves per sample | 0 / 0 | 6 / 6 | 675 / 676 | 675 / 676 |
+
+`still` accounts for only 58% because the sample is 19 µs and the instrument
+costs about 8 of them. At every workload that matters the instrument is under
+0.1%. Runs reproduce within 5%.
+
+## Three things this says, and one it refuses
+
+### Encoding and transport are 1.5% of the sample, not the remainder
+
+`frame encode` + `transport send` is 107 µs of 11,216 at `movement` and 296 µs of
+26,176 at `mixed`. **The server has not become a thing that pays for network
+work.** It is still overwhelmingly a thing that pays to find out what happened —
+the hypothesis that the leftover cost would track actual wire entries is refuted,
+and serializer or frame-batching work would be optimising 1% of the sample.
+
+### `dirty discovery` is still O(population) whenever anything at all changed
+
+4,225 µs at `movement`, where **1,000 entities of 100,000 moved**. `collectDirty_`
+has an O(1) gate — `anyChangedSince` — and it is exactly why `still` is 1 µs. But
+once any entity of a component has changed, the gate opens and the pass walks the
+whole of `known_` asking `isChangedSince` per entity, per replicated component.
+
+The enumeration it wants already exists and is already used one function earlier:
+`refreshOwnerIndex_` reads `getWrittenEntitiesSince(Replicated, floor)` off the
+write journal to get exactly the entities written since its floor. Dirty
+discovery scans instead.
+
+### `spawn payload` dominates the moment views actually churn
+
+Nine point nine milliseconds — **42% of the `anchors` sample** — to serialize 675
+entering entities, about **14.7 µs each**. That is what player movement costs: an
+anchor shifting one unit sweeps entities across the edge of a 17.8-unit radius, and
+each one that enters a view is serialized in full.
+
+`serializeEntityComponents` writes EVERY non-structural component on the entity,
+not the replicated ones the wire table declares — a component list read across the
+wasm boundary, a `tryGet` per component, a clone for script components and a codec
+lookup, all to produce the fields the delta path already knows how to name.
+
+### What it refuses to say
+
+Whether either is worth fixing. This is a budget, not a plan: `dirty discovery` is
+the biggest item at realistic movement and `spawn payload` the biggest when views
+churn, and which of those a game is actually shaped like decides the order.
+
+## What this does not cover
+
+- One anchor per connection, a uniform grid world, and clients connected but not
+ ticked. What is timed is the server's own `sample`.
+- `control send` is JSON encoding AND transport — `NetChannel` does both behind
+ one call. Only the delta frame is split, which is where the split is worth
+ having.
+- `spawn payload` is serialization only. It is timed before the send that carries
+ it, because inside it the two would be counted twice — which is how the first
+ version of this table came out at 142% accounted.
+- Phase times are means over the window; the total is reported as a mean and a
+ minimum. A budget cannot be built from minima, since the phases of one sample
+ are not the phases of another.
diff --git a/bench/sample-budget/arm.mjs b/bench/sample-budget/arm.mjs
new file mode 100644
index 000000000..a8abfc7f2
--- /dev/null
+++ b/bench/sample-budget/arm.mjs
@@ -0,0 +1,199 @@
+// SPDX-License-Identifier: Apache-2.0
+// SPDX-FileCopyrightText: Copyright (c) 2024-present ESEngine Team
+/**
+ * @file Where a replication sample's time goes, after the optimisations.
+ *
+ * Seven population scans came out of this path — registry, dirty discovery,
+ * ownership, position reads, grid rebuild, row routing, idle visibility. This
+ * asks what is left, on the real server, as a budget rather than a total: every
+ * phase is timed where it happens and `other` is the part the decomposition
+ * fails to explain. A budget with a large remainder has not been decomposed.
+ *
+ * node bench/sample-budget/arm.mjs --scenario mixed
+ */
+import path from 'node:path';
+import { fileURLToPath, pathToFileURL } from 'node:url';
+
+const ROOT = path.resolve(path.dirname(fileURLToPath(import.meta.url)), '..', '..');
+const argv = process.argv.slice(2);
+const flag = (name, fallback) => {
+ const i = argv.indexOf(`--${name}`);
+ return i >= 0 && argv[i + 1] !== undefined ? argv[i + 1] : fallback;
+};
+
+const ENTITIES = Number(flag('entities', '100000'));
+const CONNECTIONS = Number(flag('connections', '32'));
+const VISIBLE = Number(flag('visible', '0.01'));
+const SCENARIO = flag('scenario', 'movement');
+const WARMUP = Number(flag('warmup', '12'));
+const MEASURE = Number(flag('measure', '60'));
+const STEP = 1 / 60;
+
+/**
+ * `anchors` is mandatory rather than a variant: an owned anchor is one of its
+ * own query's inputs, so a movement claim that skips it is a benchmark-only
+ * cache.
+ */
+const PROFILE = {
+ still: { movement: 0, dirty: 0, removals: 0, anchors: false },
+ movement: { movement: 0.01, dirty: 0, removals: 0, anchors: false },
+ anchors: { movement: 0.01, dirty: 0, removals: 0, anchors: true },
+ mixed: { movement: 0.01, dirty: 0.01, removals: 0.001, anchors: true },
+}[SCENARIO];
+if (!PROFILE) throw new Error(`unknown scenario ${SCENARIO}`);
+
+const { loadSdk, sdkIdentity } = await import(pathToFileURL(
+ path.join(ROOT, 'bench/replication-interest/workload.mjs')).href);
+const sdk = await loadSdk(ROOT);
+const wasmDir = process.env.ESENGINE_WASM_DIR ?? path.join(ROOT, 'build', 'wasm', 'web');
+const factory = (await import(pathToFileURL(path.join(wasmDir, 'esengine.js')).href)).default;
+const engine = await factory({ locateFile: (f) => path.join(wasmDir, f) });
+
+const Health = sdk.defineComponent('BudgetHealth', { hp: 0 }, { replicatedFields: ['hp'] });
+
+const serverApp = sdk.App.new();
+serverApp.connectCpp(new engine.Registry(), engine, { strict: false });
+serverApp.addPlugin(sdk.replicationPlugin);
+const server = serverApp.getResource(sdk.Net).startServer();
+const world = serverApp.world;
+
+const side = Math.ceil(Math.sqrt(ENTITIES));
+const RADIUS = VISIBLE >= 1 ? side * 2 : Math.sqrt((VISIBLE * ENTITIES) / Math.PI);
+server.setInterestProvider(sdk.radiusInterestProvider(RADIUS));
+
+const clients = [];
+for (let i = 0; i < CONNECTIONS; i++) {
+ const app = sdk.App.new();
+ app.connectCpp(new engine.Registry(), engine, { strict: false });
+ app.addPlugin(sdk.replicationPlugin);
+ const [ta, tb] = sdk.MemoryTransport.pair();
+ const id = server.attachConnection(ta);
+ await app.getResource(sdk.Net).connect(tb, { interpolationDelayTicks: 0 });
+ clients.push({ app, id });
+}
+
+const entities = new Array(ENTITIES);
+for (let i = 0; i < ENTITIES; i++) {
+ const e = world.spawn();
+ world.insert(e, sdk.Replicated, { owner: -1 });
+ world.insert(e, sdk.Transform, { position: { x: i % side, y: Math.floor(i / side), z: 0 } });
+ world.insert(e, Health, { hp: 100 });
+ entities[i] = e;
+}
+/** Keyed by the id the SERVER gave the connection: ids start at one, and a
+ * connection owning nothing fails open to `'all'` — the bug that was every
+ * wall time in this campaign until it was reconciled against the census. */
+const ownedIdx = new Map();
+const stride = Math.max(1, Math.floor(ENTITIES / CONNECTIONS));
+for (const [c, client] of clients.entries()) {
+ const i = (c * stride) % ENTITIES;
+ world.set(entities[i], sdk.Replicated, { owner: client.id });
+ ownedIdx.set(client.id, i);
+}
+
+const MOVE = Math.round(ENTITIES * PROFILE.movement);
+const DIRTY_ROWS = Math.round(ENTITIES * PROFILE.dirty);
+const REMOVE_ROWS = Math.round(ENTITIES * PROFILE.removals);
+let restoring = [];
+
+function mutate(tick) {
+ for (let k = 0; k < MOVE; k++) {
+ const e = entities[(tick * 7919 + k * 7919) % ENTITIES];
+ world.update(e, sdk.Transform, (d) => { d.position.x += ((tick + k) % 3) - 1; });
+ }
+ if (PROFILE.anchors) {
+ for (const i of ownedIdx.values()) {
+ world.update(entities[i], sdk.Transform, (d) => { d.position.x += (tick % 3) - 1; });
+ }
+ }
+ for (let k = 0; k < DIRTY_ROWS; k++) {
+ const e = entities[(tick * 104729 + k * 104729) % ENTITIES];
+ if (!world.has(e, Health)) continue;
+ world.update(e, Health, (d) => { d.hp = (d.hp + 1) & 0xffff; });
+ }
+ for (const e of restoring) {
+ if (world.valid(e)) world.insert(e, Health, { hp: 100 });
+ }
+ restoring = [];
+ for (let k = 0; k < REMOVE_ROWS; k++) {
+ const e = entities[(tick * 15485863 + k * 15485863) % ENTITIES];
+ if (!world.has(e, Health)) continue;
+ world.remove(e, Health);
+ restoring.push(e);
+ }
+}
+
+/** The server's own view against an independent one. They diverged silently for
+ * a whole campaign and every wall time was the difference. */
+const canonical = (w, e) => {
+ if (!w.has(e, sdk.Transform)) return null;
+ const t = w.tryGet(e, sdk.Transform);
+ return t ? { x: t.worldPosition.x, y: t.worldPosition.y, z: t.worldPosition.z } : null;
+};
+function census() {
+ world.ensureTransformsComposed();
+ const prepared = sdk.radiusInterestProvider(RADIUS, { position: canonical })
+ .prepare({ world, entities, entityCount: entities.length });
+ let counted = 0;
+ for (const [c, i] of ownedIdx) {
+ const answer = prepared.query({ connectionId: c, owned: [entities[i]] });
+ counted += answer === 'all' ? entities.length : answer.size;
+ }
+ const links = server.viewerLinks;
+ if (Math.abs(links - counted) > CONNECTIONS) {
+ process.stderr.write(`the server holds ${links} viewer links where an independent `
+ + `provider counts ${counted} — they are not looking at the same world\n`);
+ process.exit(1);
+ }
+ return counted;
+}
+
+for (let t = 0; t < WARMUP; t++) { mutate(t); await serverApp.tick(STEP); }
+const visible = census();
+
+server.samplePhases.clear();
+server.visibilityRecomputes = 0;
+server.entersSent = 0; server.leavesSent = 0; server.payloadsBuilt = 0;
+server.profileSample = true;
+let tickNs = 0n;
+let fastest = Infinity;
+for (let t = 0; t < MEASURE; t++) {
+ mutate(WARMUP + t);
+ const t0 = process.hrtime.bigint();
+ await serverApp.tick(STEP);
+ const dt = Number(process.hrtime.bigint() - t0);
+ tickNs += BigInt(dt);
+ fastest = Math.min(fastest, dt / 1000);
+}
+server.profileSample = false;
+census();
+
+const LEAVES = [
+ 'composition', 'registry', 'owner index', 'interest prepare', 'visibility query',
+ 'visibility diff', 'dirty discovery', 'routing', 'spawn payload', 'frame encode',
+ 'control send', 'transport send', 'ack', 'reader floors',
+];
+const ms = (k) => (server.samplePhases.get(k) ?? 0) / MEASURE;
+const phases = Object.fromEntries(LEAVES.map((k) => [k, ms(k) * 1000]));
+const total = ms('total') * 1000;
+const accounted = LEAVES.reduce((a, k) => a + phases[k], 0);
+phases.other = total - accounted;
+
+const tickUs = Number(tickNs) / 1000 / MEASURE;
+process.stdout.write(`${JSON.stringify({
+ scenario: SCENARIO, entities: ENTITIES, connections: CONNECTIONS, visible: VISIBLE,
+ ...PROFILE, samples: MEASURE,
+ visiblePerConnection: visible / CONNECTIONS,
+ viewerLinks: server.viewerLinks,
+ visibilityRecomputesPerSample: server.visibilityRecomputes / MEASURE,
+ entersPerSample: server.entersSent / MEASURE,
+ leavesPerSample: server.leavesSent / MEASURE,
+ spawnPayloadsPerSample: server.payloadsBuilt / MEASURE,
+ // The whole App tick, the server's own sample, and what the phases explain.
+ usPerTickMean: tickUs, usPerTickMin: fastest,
+ usPerSampleTotal: total,
+ usOutsideSample: tickUs - total,
+ accountedPercent: total > 0 ? (accounted / total) * 100 : 0,
+ phases,
+ ...sdkIdentity(ROOT),
+})}\n`);
diff --git a/sdk/etc/index.api.md b/sdk/etc/index.api.md
index f34b3a783..7e9a13686 100644
--- a/sdk/etc/index.api.md
+++ b/sdk/etc/index.api.md
@@ -6647,8 +6647,12 @@ static prototype: ReplicationPlugin
## ReplicationServer — class @experimental
```
+@internal entersSent: number
@internal fullScans: number
@internal ownerScanVisits: number
+@internal payloadsBuilt: number
+@internal profileSample: boolean
+@internal samplePhases: Map<string, number>
@internal viewerLinks: number
@internal visibilityRecomputes: number
attachConnection: (transport: ReliableOrderedTransport) => number
@@ -6658,6 +6662,7 @@ connectionCount: number
detachConnection: (id: number) => void
dispose: () => void
inputOf: (connectionId: number) => ReplInputMsg | null
+leavesSent: number
netIds: NetIds
sample: (tick: number) => void
setInterestPolicy: (policy: InterestPolicy | null) => void
diff --git a/sdk/etc/index.native.api.md b/sdk/etc/index.native.api.md
index 2ac708538..8f95607a0 100644
--- a/sdk/etc/index.native.api.md
+++ b/sdk/etc/index.native.api.md
@@ -6840,8 +6840,12 @@ static prototype: ReplicationPlugin
## ReplicationServer — class @experimental
```
+@internal entersSent: number
@internal fullScans: number
@internal ownerScanVisits: number
+@internal payloadsBuilt: number
+@internal profileSample: boolean
+@internal samplePhases: Map<string, number>
@internal viewerLinks: number
@internal visibilityRecomputes: number
attachConnection: (transport: ReliableOrderedTransport) => number
@@ -6851,6 +6855,7 @@ connectionCount: number
detachConnection: (id: number) => void
dispose: () => void
inputOf: (connectionId: number) => ReplInputMsg | null
+leavesSent: number
netIds: NetIds
sample: (tick: number) => void
setInterestPolicy: (policy: InterestPolicy | null) => void
diff --git a/sdk/etc/index.node.api.md b/sdk/etc/index.node.api.md
index e90e45166..86ce7d58c 100644
--- a/sdk/etc/index.node.api.md
+++ b/sdk/etc/index.node.api.md
@@ -6642,8 +6642,12 @@ static prototype: ReplicationPlugin
## ReplicationServer — class @experimental
```
+@internal entersSent: number
@internal fullScans: number
@internal ownerScanVisits: number
+@internal payloadsBuilt: number
+@internal profileSample: boolean
+@internal samplePhases: Map<string, number>
@internal viewerLinks: number
@internal visibilityRecomputes: number
attachConnection: (transport: ReliableOrderedTransport) => number
@@ -6653,6 +6657,7 @@ connectionCount: number
detachConnection: (id: number) => void
dispose: () => void
inputOf: (connectionId: number) => ReplInputMsg | null
+leavesSent: number
netIds: NetIds
sample: (tick: number) => void
setInterestPolicy: (policy: InterestPolicy | null) => void
diff --git a/sdk/etc/minigame.api.md b/sdk/etc/minigame.api.md
index 7ab43387f..432a9d639 100644
--- a/sdk/etc/minigame.api.md
+++ b/sdk/etc/minigame.api.md
@@ -6888,8 +6888,12 @@ static prototype: ReplicationPlugin
## ReplicationServer — class @experimental
```
+@internal entersSent: number
@internal fullScans: number
@internal ownerScanVisits: number
+@internal payloadsBuilt: number
+@internal profileSample: boolean
+@internal samplePhases: Map<string, number>
@internal viewerLinks: number
@internal visibilityRecomputes: number
attachConnection: (transport: ReliableOrderedTransport) => number
@@ -6899,6 +6903,7 @@ connectionCount: number
detachConnection: (id: number) => void
dispose: () => void
inputOf: (connectionId: number) => ReplInputMsg | null
+leavesSent: number
netIds: NetIds
sample: (tick: number) => void
setInterestPolicy: (policy: InterestPolicy | null) => void
diff --git a/sdk/etc/wechat.api.md b/sdk/etc/wechat.api.md
index caeb4ba79..f3b8fcaf8 100644
--- a/sdk/etc/wechat.api.md
+++ b/sdk/etc/wechat.api.md
@@ -6649,8 +6649,12 @@ static prototype: ReplicationPlugin
## ReplicationServer — class @experimental
```
+@internal entersSent: number
@internal fullScans: number
@internal ownerScanVisits: number
+@internal payloadsBuilt: number
+@internal profileSample: boolean
+@internal samplePhases: Map<string, number>
@internal viewerLinks: number
@internal visibilityRecomputes: number
attachConnection: (transport: ReliableOrderedTransport) => number
@@ -6660,6 +6664,7 @@ connectionCount: number
detachConnection: (id: number) => void
dispose: () => void
inputOf: (connectionId: number) => ReplInputMsg | null
+leavesSent: number
netIds: NetIds
sample: (tick: number) => void
setInterestPolicy: (policy: InterestPolicy | null) => void
diff --git a/sdk/src/net/replication/server.ts b/sdk/src/net/replication/server.ts
index cffc57b08..81665300d 100644
--- a/sdk/src/net/replication/server.ts
+++ b/sdk/src/net/replication/server.ts
@@ -220,6 +220,30 @@ export class ReplicationServer {
* that is the whole claim of the stamp.
*/
visibilityRecomputes = 0;
+ /** @internal Entities entering and leaving views — what spawn serialization
+ * and the control plane are actually paid for. */
+ entersSent = 0;
+ leavesSent = 0;
+ /** @internal Distinct entities serialized into a spawn payload this run. */
+ payloadsBuilt = 0;
+ /**
+ * @internal Milliseconds attributed to each LEAF phase of `sample`, plus
+ * `total` for the whole call. Nothing nests, so `total` minus the rest is
+ * exactly what the decomposition fails to explain — a budget with a large
+ * remainder has not been decomposed. Off unless `profileSample` is set.
+ */
+ readonly samplePhases = new Map<string, number>();
+ /** @internal Turns the phase accounting on; it costs two clock reads a phase. */
+ profileSample = false;
+
+ private mark_(): number {
+ return this.profileSample && typeof performance !== 'undefined' ? performance.now() : 0;
+ }
+
+ private since_(phase: string, at: number): void {
+ if (!this.profileSample || typeof performance === 'undefined') return;
+ this.samplePhases.set(phase, (this.samplePhases.get(phase) ?? 0) + (performance.now() - at));
+ }
/**
* How many (entity, connection) links the reverse-interest index holds.
@@ -584,14 +608,21 @@ export class ReplicationServer {
return;
}
+ const started = this.mark_();
+ let at = started;
const { spawnedEntities, despawned } = this.reconcileRegistryIncremental_();
+ this.since_('registry', at);
// Before anything reads ownership: an owner that changed this frame must
// place the view THIS sample, not the next one.
+ at = this.mark_();
this.refreshOwnerIndex_();
+ this.since_('owner index', at);
// Diff once against the shadow; frames below share the result.
+ at = this.mark_();
const sample = this.collectDirty_();
+ this.since_('dirty discovery', at);
if (!this.interest_) {
this.sampleBroadcast_(tick, spawnedEntities, despawned, sample);
@@ -601,16 +632,21 @@ export class ReplicationServer {
// Acknowledge consumed inputs: this tick's state incorporates each
// connection's commands through the seq its gameplay ran against.
+ at = this.mark_();
for (const conn of [...this.connections_.values()]) {
if (!conn.ready || !conn.applied || conn.applied.seq <= conn.ackedSeq) continue;
conn.ackedSeq = conn.applied.seq;
this.sendTo_(conn, (c) => c.channel.send<ReplAckMsg>(ReplMsg.ack, { tick, seq: conn.ackedSeq }));
}
+ this.since_('ack', at);
// Last: the windows are given up only once their rows are on the wire.
+ at = this.mark_();
this.advanceObservationFloor_();
this.advanceRegistryFloor_();
this.advanceOwnerFloor_();
+ this.since_('reader floors', at);
+ this.since_('total', started);
}
/** Fast path (no policy): everything to every ready connection on shared
@@ -713,7 +749,10 @@ export class ReplicationServer {
continue;
}
this.visibilityRecomputes++;
+ let at = this.mark_();
const visible = visibility.visible(conn.id);
+ this.since_('visibility query', at);
+ at = this.mark_();
const enters: Entity[] = [];
for (const e of visible) {
if (conn.interest.has(e)) continue;
@@ -730,10 +769,13 @@ export class ReplicationServer {
conn.interest = visible;
conn.provenance = stamp;
plans.push({ conn, visible, enters, leaveIds, entered: new Set(enters) });
+ this.since_('visibility diff', at);
}
// Whose debt is whose.
+ const routing = this.mark_();
const routed = this.route_(plans, dirty, removals);
+ this.since_('routing', routing);
// The wire. Spawn payloads serialize once per entity per tick, however
// many connections it enters.
@@ -741,16 +783,25 @@ export class ReplicationServer {
const payloadOf = (e: Entity): ReplSpawnEntity => {
let p = payloads.get(e);
if (!p) {
+ const at = this.mark_();
p = this.spawnPayload_(e, this.knownNetIds_.get(e)!);
+ this.since_('spawn payload', at);
+ this.payloadsBuilt++;
payloads.set(e, p);
}
return p;
};
for (const { conn, enters, leaveIds } of plans) {
const mine = routed.get(conn.id)!;
+ this.entersSent += enters.length;
+ this.leavesSent += leaveIds.length;
this.sendTo_(conn, (c) => {
- if (enters.length > 0) {
- c.channel.send<ReplSpawnBatch>(ReplMsg.spawn, { tick, entities: enters.map(payloadOf) });
+ // Serialized before `control send` is timed: `payloadOf` has a
+ // phase of its own and would otherwise be counted twice.
+ const spawning = enters.length > 0 ? enters.map(payloadOf) : null;
+ const control = this.mark_();
+ if (spawning) {
+ c.channel.send<ReplSpawnBatch>(ReplMsg.spawn, { tick, entities: spawning });
}
if (leaveIds.length > 0) {
c.channel.send<ReplDespawnBatch>(ReplMsg.despawn, { tick, netIds: leaveIds });
@@ -759,13 +810,21 @@ export class ReplicationServer {
c.channel.send<ReplComponentRemoveBatch>(
ReplMsg.componentRemove, removeBatch(tick, mine.removals.map((i) => removals[i]!)));
}
+ this.since_('control send', control);
if (mine.dirty.length > 0) {
+ const encoding = this.mark_();
const frame = new FrameWriter(tick);
for (const i of mine.dirty) {
const d = dirty[i]!;
frame.entry(d.netId, d.te, d.mask, d.data, this.refs_);
}
- if (frame.entryCount > 0) c.channel.sendBinary(REPLICATION_CHANNEL, frame.finish());
+ const payload = frame.entryCount > 0 ? frame.finish() : null;
+ this.since_('frame encode', encoding);
+ if (payload) {
+ const sending = this.mark_();
+ c.channel.sendBinary(REPLICATION_CHANNEL, payload);
+ this.since_('transport send', sending);
+ }
}
});
}
@@ -877,7 +936,9 @@ export class ReplicationServer {
// Relevance is decided in world space, so the composition has to be
// current before a snapshot is taken — here rather than inside a position
// reader, so a caller's own reader sees the same composed fact.
+ const composed = this.mark_();
this.world_.ensureTransformsComposed();
+ this.since_('composition', composed);
const source = this.interest_;
if (!source) {
const all = [...this.known_];
@@ -885,6 +946,7 @@ export class ReplicationServer {
}
if (source.kind === 'provider') {
const known = this.known_;
+ const prepping = this.mark_();
const prepared = source.value.prepare({
world: this.world_,
// A wrapper, not the registry itself: a caller that casts the
@@ -893,6 +955,7 @@ export class ReplicationServer {
entityCount: known.size,
...membership,
});
+ this.since_('interest prepare', prepping);
return {
snapshot: prepared.generation ?? null,
visible: (connectionId) => {