fix: stream map import so 16384 px at 64 px is memory- and fd-bounded (#83)

The one-shot `magick -crop 64x64 +adjoin` built an image list of all 65,536 tiles; ImageMagick's
memory/map limits are per image, so the attempt held 4.3 GB of source cache plus every tile cache and
died after minutes with "Too many open files" at 1023 fds, having published nothing.

Slice by streaming instead: one disk-cache-backed decode of the source to raw samples, the CLI reads
it one tile row at a time, and a pool of 8 magick children cuts one row band each. Live caches and
fds are bounded by the worker count, never by the tile count. Progress goes to stderr ("rows 37/256,
tiles 9472/65536") and failures quote the image, row band and magick's own message. Atomicity is
unchanged: temp dir then rename, scene row only on success, same lock and exit codes.

Measured on the owner's 16384x16384 map at 64 px: 19 s, 391 MB peak RSS, 10 fds, 65,536 tiles (was
~5 min then failure). The tile pixels are identical to the old crop (proven tile by tile in
tools/import.test.mjs); PNG bytes can differ because the encoder now sees raw samples.

New gate: devbox run import-scale (16384/64 under ulimit -n 256) writes results/import-scale.log;
devbox run test keeps the fast fixtures and skips it.
This commit is contained in:
2026-11-21 20:48:45 +01:00
parent b56d645b3a
commit c8b5289a3d
6 changed files with 477 additions and 23 deletions
+10 -2
View File
@@ -118,11 +118,18 @@ cleanup. Any problem exits non-zero and writes no tiles: a missing/unreadable so
unsupported or multi-frame image, a scene other than 1, a tile size that is not a multiple of the
cell, a malformed state DB, or a server still holding the state-dir lock (exit 3).
Slicing streams: one ImageMagick decode of the source supplies raw row bands, and a small pool of
`magick` children cuts one tile row each (live caches and file descriptors are bounded by the worker
count, never by the tile count). A long import reports where it is on stderr, e.g.
`rows 128/256, tiles 32768/65536`; a failure prints the image, the row band and ImageMagick's own
message.
**A 16384 px scene with 50 tokens (the phone-playtest target).**
1. Prepare a 16384×16384 PNG or WebP. `--tile-size=64` is the designed fallback and gives 256×256 =
65,536 tiles (32 px would give 262,144 files); ImageMagick keeps the source in a disk-backed
pixel cache, so allow temp space and a few minutes rather than gigabytes of RAM.
65,536 tiles (32 px would give 262,144 files). On this 16-core box such an import takes about
20 s and peaks near 400 MB RSS (the old one-shot crop died at 4-8 GB and 65k fds, #83); the
decode still uses ImageMagick's disk-backed pixel cache, so allow a few GB of temp space.
2. Start with the import in one step: `devbox run serve -- --image=run/map16384.png --tile-size=64`
(or stop the server, run `fff-vtt import` as above, then restart it).
3. Create the 50 tokens with the seeding tool, not 50 panel submits (a rerun does not double up):
@@ -318,6 +325,7 @@ panel); it needs USB debugging, the overlay does not.
| `devbox run mutation` | mutation suite on a temp copy of `server/`, `tests/`, `tools/`: every seeded mutation must fail at least one test |
| `devbox run bench` | release-build bench of the vision and egress paths, writing `results/bench.log` |
| `devbox run fogbench` | the 50-token / 16384 px server measurement, writing `results/m4-fog-bench.log`; the numbers live in `results/m4-fog-bench.md` |
| `devbox run import-scale` | the map import at target scale: 16384×16384 at 64 px (65,536 tiles) under a 256-fd limit, asserting bounded RSS/fds and progress, writing `results/import-scale.log`; ~25 s, too slow for the pre-push gate (`test` skips it) |
| `devbox run -- nimble install -d -y` | install/refresh the pinned Nim dependencies |
| `devbox run -- bash -c 'cd client && npm ci && npm run dev'` | Vite dev server on :5173 — the UI only, no server API behind it |