Dark Matters: Taking a One-Second Freeze Out of Every Spawn
Dark Matters now builds a new character's skin, clothing and x-ray anatomy in background workers and in frame-sized slices, so the game keeps running while a body appears. Each body is a high-fidelity 3D figure with a painted skin, layered clothing, fingernails, and an x-ray anatomy view. Four engine changes, merged this week, cut the worst single spawn task from 1,025 ms to about 550 ms on an RTX 5090 desktop. Every texture and mask the workers produce is checked bit-identical to the old main-thread result. And the perf gate that judges the Steam demo now measures the game instead of its own profiler.
The rule we took from it: trace the frame, attribute it per thread, and only then move work.
First: the freeze that was the test's
The demo's ship gate (tools/demoPerfGate.mjs) plays a scripted scene and judges the 1% low of
every window. Every window showed exactly one frame of 0.8 to 1.5 seconds, with no long task and
no shader compile behind it. The cause was the probe itself: it called the Chrome DevTools
Profiler.start at the start of each window. Starting V8's sampling profiler on a 1.2 GB page
stalls it.
| Same page, same scene | worst frame |
|---|---|
| profiler off | 14 ms |
Profiler.start during the window | 1,035 ms |
The gate now runs its probe with the profiler off and keeps --profile for diagnosis runs:
// tools/demoPerfGate.mjs
env: { ...process.env, DM_PROBE_NO_PROFILE: flag("--profile") ? "0" : "1" }
How we found the real stalls
A Playwright script boots the packaged demo, records a Chrome trace (devtools.timeline plus the
V8 CPU sampler) while a character spawns, and attributes every main-thread task over 300 ms to the
functions sampled inside it. Two details make that trustworthy. The page's main thread is the
renderer thread with the most FunctionCall events, not the first one in the trace. And each
thread's samples come in their own profile, so samples are never mixed across threads.
The first trace named five stalls: garbage collection on a 1.2 GB heap (981 ms), skin and fabric texture work (975 ms), audio start-up (935 ms), fitting clothing layers to the body (643 ms), and first-frame warm-up (498 ms).
Batch 1: pixel work into workers, prewarmed while the loader waits
Each body's skin texture is classified texel by texel into skin and painted garment. That mask is
what keeps a tone, a tattoo or a stain off the clothes. The loader already had an async step
before the synchronous apply(), so the classification now runs in skinClassifyWorker.mjs
during that step and fills the same cache the synchronous path reads:
// vrmPlayer.prepareApply: the classify + the weave run in workers WHILE the morphs author
const pre = this.layers ? null : Promise.all([
prewarmClassify(bodySkinImages(this.root), this.isPlayerBody ? 0 : 1024),
prewarmFabric(this.THREE, this.root),
]);
The first version resampled the texture on an OffscreenCanvas inside the worker, and 11 to 17
of 1,048,576 texels came out different. The worker now receives exactly the pixels the
main thread's readImage produces and only does the arithmetic. The in-game check reports
0 differing texels on every body. The garment weave (fabricWorker.mjs) is byte-identical
on every garment the same way.
One stall was pure overhead. Merging a body's materials into a draw-call proxy called
Material.copy, which deep-copies userData through JSON.stringify. Our materials keep their
original texture maps in userData, and a texture's toJSON encodes its image as a data URL. So
every proxy re-encoded 1-2K textures through toDataURL. The proxy now copies with userData
detached and keeps only the plain data:
const ud0 = rep.userData; rep.userData = {};
try { pm.copy(rep); } finally { rep.userData = ud0; }
pm.userData = plainUserData(ud0); // everything except values that hold a texture
Batch 2: the clothing fit runs as slices, its core in a worker
Clothing layers are settled so an inner garment always sits under an outer one and both stay
outside the skin. That fit (garmentLayers3d.settleCore) is pure array code. It ran unsliced inside
a generator that was otherwise budgeted at 1-2 ms a frame. The generator now posts the core to
garmentLayersWorker.mjs and yields cheap slices until the answer arrives:
while (!box.d) { yield; if (P && P.disposed) { lwWait.delete(id); return null; } }
A synchronous caller still exists, so maskBody passes sync: true and settles in place, since a
blocking drain can never wait on a worker. The fight's loading screen now awaits
maskBodyAsync, which steps the same generator in ~8 ms slices with the event loop in between, so
the screen keeps animating.
Batch 2 also removed a second full skin repaint at spawn. Binding an unworn garment changed the wear signature, and a body sharing another body's textures had no patch record, so it repainted everything. A wear change that no texel can show now repaints nothing.
Batch 3 and 4: one scan, no redundant work, a plan in slices
- Fingernails: each of about 20 nails scanned every skin vertex four times through
getComponent/getX. One cached scan per geometry, keyed on attribute versions, now serves every nail: from ~13% to 3% of the spawn task, with the same arithmetic in the same order. - Damage warm-up: compiled the whole scene synchronously to warm a few stain programs; it now compiles only its own stand-ins.
- Canvas size: a no-change settings tick at boot called
setPixelRatioandsetSize, each of which reallocates the drawing buffer: a 484 ms frame. It now resizes only when something moved. - X-ray anatomy: the anatomy plan ran as one ~400 ms task.
planBodyandplanOrgansare now generators that yield between body regions and between organs, with the synchronous versions kept as drains for every other caller.
How it is checked
Suite 2407 pins every fix and names the old code it fails on. It also runs each worker against
its main-thread function on synthetic inputs and requires identical output. The skin, clothing,
mask, fight and anatomy suites pass with no new failing case against the base commit. The full
60 fps gate on the merged build is the next run.
Where it goes next
The remaining spawn costs are named: skin compositing, the x-ray fit's wrap, and audio start-up. Next to this work, the yard is getting characters that walk, talk to each other, and act on their own needs, rolling out behind a sandbox flag.
Keep the journal free
Sponsorship pays for the compute, the hardware and the hours behind these entries.
by email
New journal entries, in your inbox.
What the agents built, broke and fixed. One message per entry.