2026-11-11 · Export names, and what writing a large job’s files costs

2026-11-11 · Export names, and what writing a large job’s files costs

Context

Issue #143 asks that a folder job of several thousand pages export as quickly, file for file, as a small one. The third round left two things: output_base compared each source’s output name with every earlier source’s on every write, so writing a job’s files grew with the square of its sources; and the benchmark had 2,000 sources, not the issue’s 5,000.

On a quiet Windows ARM64 machine, before this round, large_job (2,000 sources × 2 set-ups) gave 27.3 s for export_finished over the job and 9.28 ms for exporting one source. At 9.28 ms a source, 2,000 sources make 18.6 s, so about 8.7 s was unexplained. The first guess was the name comparison. This round measured before changing anything.

The split measured

large.rs now times each step of writing a source’s files, for one source in ten. It repeats write_files step by step in the test, with no timing in product code. It also times the names alone over the whole job, a second export_finished over files that already exist, outputs, and what one file costs on this disk. Each source writes four files under each set-up, eight in all: .txt (2.3 KB), .json (8.5 KB), .searchable.pdf (22 KB) and .provenance.json (5.5 KB). Each file is written to name.ext.pending and renamed. Nothing is flushed with sync_all, no folder is listed, and nothing is probed on disk before writing. create_dir_all ran once per write, for every set-up.

Per source (two set-ups), before the change, 5,000 sources, busy machine:

Step ms a source Share
Writing the four .pending files ×2 3.0 19 %
Renaming them into place ×2 6.8 44 %
Making the folder (create_dir_all) ×2 0.4 3 %
Names (output_base), at this size 1.5 10 %
Reading (export_document) ×2 2.0 13 %
Rendering: searchable PDF / JSON / provenance / text 1.21 / 0.09 / 0.03 / 0.01 8 %
Store: the source’s pieces, the job’s out-of-date marks 0.46 3 %
All 15.5  

At 2,000 sources the shares were the same but for the names: 0.75 ms a source, 5 %. Computed alone over the whole job, the names took 1,326 ms at 2,000 sources and 7,706 ms at 5,000: 5.8 times as long for 2.5 times the sources, the square as expected. On a quiet machine they explain part of the 8.7 s gap above, not all of it. On a busy one, export_finished over the job ran at twice the split’s time per source before the change, but at less than it after; and writing every file again took longer than writing them new. So the rest of the gap is mostly the machine’s load, not something in the code that grows with the job. Nothing else in the write path depends on the number of sources.

One file on this disk (in %TEMP%, scanned by the antivirus), 500 times each, ms a file:

  Busy, before Busy, after
1 KB, new, written straight 0.26–0.41 0.29–0.77
1 KB, new, temporary file renamed 0.55–0.72 0.62–0.93
1 KB, replaced, temporary file renamed 0.64–0.68 0.49–0.59
100 KB, new, written straight 0.36–0.52 0.42
100 KB, new, temporary file renamed 0.95–1.69 1.07–1.15
is_file on a file that exists 0.04–0.05 0.17–0.35

What a file costs barely depends on its size. It is the creating, closing and renaming: NTFS metadata, and the antivirus, which scans every file as it is closed. That second cause is inferred, not measured: Kalamu cannot turn the scanner off to compare. The second round measured the same job’s writes at 3.6 s on Linux.

Decisions

Before and after

cargo test -p kalamu-engine --release large_job -- --ignored --nocapture, Windows ARM64. Every number below was taken on a busy machine (three other agents building), so it is rough. The quiet numbers from before this round are in Context.

  2,000 before 2,000 after 5,000 before 5,000 after
export_finished over the job 59,883 ms 30,197 ms 158,272 ms 54,886 ms
… a source 29.9 ms 15.1 ms 31.7 ms 11.0 ms
… of which the names, over the job 1,326 ms 1.7 ms 7,706 ms 7.2 ms
export_finished again, every file replaced — 32,749 ms — 70,868 ms
A source’s files, split, all steps 15.8 ms 15.9 ms 15.5 ms 13.9 ms
Export of one source (export) 10.18 ms 10.04 ms 14.71 ms 13.91 ms
… of which reading the job’s record 3.28 ms 3.46 ms 8.80 ms 7.78 ms
export_document of one source 0.23 ms 0.27 ms 0.62 ms 0.25 ms
outputs (the job’s files listed) — 1,030 ms (16,000 files) — 2,137 ms (40,000 files)
list_jobs 19.2 ms 17.4 ms 74.2 ms 65.4 ms
spent 3.41 ms 10.32 ms 14.43 ms 19.19 ms
Job page, counts of 200 files 2.85 ms 2.31 ms 2.92 ms 3.03 ms
save_correction 0.45 ms 0.12 ms 0.60 ms 0.22 ms

The names went from growing with the square of the sources to under 8 ms for 5,000. They are no longer to be seen in the split (0.004 ms a source). After the change, the time a source takes no longer grows with the job: 15 ms a source at 2,000 and 11 at 5,000, both within the noise of a shared machine. The gap between before and after is larger than the names and the folder alone explain (7.7 s + 2 s at 5,000), so most of it is the machine’s load moving between runs. To be rerun alone, on a quiet machine, from the repository’s root in Git Bash:

cargo test -p kalamu-engine --release large_job -- --ignored --nocapture
KALAMU_LARGE_SOURCES=2000 cargo test -p kalamu-engine --release large_job -- --ignored --nocapture

Rerun that way the same day, alone, after the change (once each, on the branch that gathers this round’s work):

  2,000 5,000
export_finished over the job 33,512 ms 59,935 ms
… a source 16.8 ms 12.0 ms
… of which the names, over the job 4.3 ms 5.8 ms
export_finished again, every file replaced 34,519 ms 87,298 ms
A source’s files, split, all steps 15.7 ms 15.5 ms
… of which writing and renaming its 8 files 12.5 ms 11.8 ms
outputs (the job’s files listed) 1,008 ms 1,985 ms
list_jobs 9.8 ms 37.8 ms
Job page, counts of 200 files 6.2 ms 2.8 ms

Alone, before the change, 2,000 sources took 27,308 ms: the quiet machine gives no faster a total after it, although the names fell from over a second to 4 ms. What decides the total on this computer is the disk, at 12 to 17 ms a source from one run to the next, and that varies by more than the names ever cost at this size. The change is therefore shown by the names’ own line and by a source costing no more at 5,000 than at 2,000, not by the total.

Left

Consequences