garage44.eu/jeroen/cartopolis

logclone: git clone https://garage44.eu/jeroen/cartopolis.git

.. /docs/notes/simulator-tick-budget.md

raw


topic: simulator-tick-budget triggers: [tick_loop, WorldSnapshot, avatar_snapshot, broadcast, "tick timing", TickMeter, spawn_blocking, "blocking the executor", journal_mode, WAL, flush_dirty, "world write lock", RwLock, "sqlite is slow", "snapshot is large", tokio, "schedule latency", Bytes, "chat fan-out", crates/simulator/src/net, crates/simulator/src/db.rs, crates/simulator/src/state.rs, crates/simulator/src/prims.rs, crates/simulator/src/chat.rs, crates/simulator/src/library.rs] updated: 2026-11-11

What one simulator tick may spend, and on what

The simulator is a single Tokio runtime. Every connection handler, every HTTP route, the script command drain and the 20 Hz tick loop share the same worker threads, so anything one of them does synchronously is time the others are not running. This note records what that costs here and which shapes have already been ruled out, so the next person does not re-derive it.

Prompted by Principles for fast Tokio applications, which is worth reading before changing anything in net.rs.

The tick has a budget and now says when it misses it

net::TickMeter summarises roughly thirty seconds of ticks into one tick timing line. Two numbers, because they answer different questions:

over counts ticks in the window that exceeded the period. The line is info when over > 0 and debug otherwise, so a healthy server is quiet and a struggling one does not need the log level raised first.

A large total with a small locked is a busy but healthy server. A large locked is the thing to go and fix.

The repeating snapshot carries no prims, and never usefully did

Since protocol 47 (2026-11-11) there is no repeating broadcast snapshot at all: the tick leaves one table and each connection cuts its own snapshot from the avatars within its reach, and things arrive and leave by reach (interest-by-reach.md). The measurement below is why the world left the snapshot in the first place and what the per-connection path has to stay under; the Bytes rule after it still holds for every frame the tick fans out.

WorldSnapshot has a prims field and the tick loop used to fill it with the whole world — up to the 1,000-prim ceiling — twenty times a second, cloned out of SimWorld, postcard-encoded, and copied once per connected client.

No client has ever read it after the first one. network.rs builds its prim entities from the first snapshot it receives and takes the received_initial_world early return on every one after that; prims change through PrimUpsert and PrimRemoved. So the largest single thing the server did per tick reached no screen.

The split is now: SimWorld::snapshot (the full world) is sent once, inline at the handshake, and SimWorld::avatar_snapshot (an empty prim list) is what repeats. This is not a wire change — the field is still there and every connection still receives a full one — so an older client sees exactly what it always saw.

Measured 2026-09-15 in the agent container, vps-1 slot, a world filled to the 1,000-prim ceiling, no client connected (so this is the clone and the encode alone, with none of the per-client copies on top). Method: boot the server, read the tick timing line for a 600-tick window, with avatar_snapshot returning the full prim list and then the empty one.

mean tickmax tickmean lock heldencoded snapshot
prims in every snapshot3.00 ms5.58 ms0.245 ms157,841 bytes
prims at the handshake only0.063 ms0.23 ms0.012 ms~100 bytes

That is 2.9 ms of every 50 ms tick — about 6 % of the budget — and 3.2 MB/s per connected client, for something no client reads.

One consequence to know, and it predates the split: a client whose broadcast receiver falls behind drops frames as Lagged, and a dropped PrimUpsert is never resent. The repeating full prim list looked like it would repair that and never did, because the client discards it. If prim resync becomes a real problem the fix is to notice the lag and resend, not to go back to broadcasting the world.

Fan-out carries Bytes, not Vec<u8>

tokio::sync::broadcast clones its payload for every receiver, so a Sender<Vec<u8>> was a full copy of each frame per connected client. The chat hub had the same shape in frame.to_vec() per recipient. Both carry Bytes now, where a clone is a reference count. The frame is encoded once either way; this is only about the copies after it.

Anything new that fans one encoded frame out to many connections should carry Bytes for the same reason.

SQLite is in WAL, and one flush is one transaction

db::tune sets journal_mode = WAL, synchronous = NORMAL and a 5 s busy_timeout at open. The defaults — a rollback journal with synchronous = FULL — create, fsync and delete a separate journal file around every transaction, which is the worst possible shape for a connection that is shared by a mutex and written from async worker threads. db.rs's the_database_opens_in_wal_mode pins it; nothing else downstream would notice if tune stopped being called.

PrimService::flush (prims.rs) wraps the batch in one transaction rather than letting each UPDATE be its own implicit one. It used to run as SimWorld::flush_dirty on the tick loop's thread holding both the database mutex and the world write lock, so a flush of forty moved prims was forty commits inside the most contended section in the server. Since 2026-09-29 SimWorld holds no connection at all: the flush copies the dirty rows out under the world lock (take_dirty), releases it, and writes on the blocking pool, as a task of its own the tick loop starts every 40 ticks. The failure granularity is unchanged: a rolled-back batch re-marks every id dirty (restore_dirty), so the retry writes the same set the database rejected rather than a half-applied one.

File reads and writes go through spawn_blocking

Three paths were doing synchronous disk I/O on a worker thread in response to a network message, and all three now hand it to the blocking pool:

The rule for anything new: if a handler touches the filesystem, it goes through spawn_blocking. The blocking pool is a shared global resource and batching into it is worth doing, but a 16 MB read on a worker thread is never the trade.

Not done, and why