Charge the scoreboard for -L and the other fastwc

-L is now the widest gap in the table rather than the one case we lost,
so the README stops pretending that "never slower" covered it and says
where the numbers came from. The Rust clone that held the record gets
its own section: what it did right, what it still wins (dense CJK, by
carrying a width table where we call wcwidth), and what it lost.

PERFORMANCE.md gets the receipts: the -L kernel ladder with one-core
throughput per kernel, the UTF-8 numbers before and after the split
decode, the thread-count rules, and the tab and CJK cases stated as
divergences instead of hidden.
This commit is contained in:
2026-11-15 13:38:32 -05:00
parent 5d2aee2f1d
commit 35d121e6ea
2 changed files with 189 additions and 27 deletions
+119 -26
View File
@@ -26,11 +26,17 @@ with the contract.
| lines, 1B (solo) | — | — | — | **266ms** |
| stdin lines, 10M | 5.9ms | 161ms | 154ms | **4.6ms** |
| stdin words, 1M | 7.1ms | 16.0ms | 16.5ms | **1.5ms** |
| longest line, 110MB | 157ms | — | — | **10.4ms** |
| longest line, 1.1GB | 1506ms | — | — | **61.5ms** |
| 1050 files of 1MiB, `-lwc` | 1509ms | — | — | **45ms** |
That is a 13x win over GNU on 10M lines of words, 4.9x on 1M, and 1.6x
on the 100M monster. GNU's threaded counter manages a genuine dead
heat at 110 MB of plain lines (5.8ms vs our 4.6ms — it never wins;
we allow it a 2% tie margin and it still can't take it). The moment
on the 100M monster. `-L` used to be the exception — a serial walk that
lost to GNU on anything past a few megabytes. It is now the biggest win
in the table: 15x at 110 MB, 24x at 1.1 GB, and 2.3x faster than the
Rust clone that used to own the case. GNU's threaded counter manages a
genuine dead heat at 110 MB of plain lines (5.8ms vs our 4.6ms — it never
wins; we allow it a 2% tie margin and it still can't take it). The moment
the file stops fitting in a polite buffer, the dead heat stops being
polite. At 1B lines — 11 GB, which this machine's 64 GiB actually
fits — the solo run lands at 266ms: 41.4 GB/s, 3.76 billion lines per
@@ -124,26 +130,100 @@ half and then the honest reporting:
from a regular file (`fastwc -l < file`) gets the same treatment —
the data comes through stdin, but how we read it is our business.
The stdin suite is why this shows up in the scoreboard too.
3. **Parallel across cores.** Files over 8 MiB are split into 64-byte
aligned slices counted by up to one thread per core (capped at 24)
past 256 MiB, 12 past 32 MiB, 4 past 8 MiB. The kernels are pure, so
the split needs no locks; word boundaries between slices are seeded
from the byte before the slice, which makes the split exact. Below
8 MiB the thread spawn would cost more than the counting, so we don't
bother.
4. **No work that isn't asked for.** `-c` on a regular file is
`st_size` from `fstat` — GNU figured that one out too, so we copied
the good idea. `-l` without `-w` skips the whitespace mask entirely;
`-w` without `-L` never builds the print table.
5. **ASCII pays nothing, even in a UTF-8 locale.** Multibyte decoding
is expensive, so we don't volunteer for it. The SIMD kernels
double as a probe: when asked, they flag the first byte ≥ 0x80
with a vector move-mask — no extra pass, no cost on pure-ASCII
input. A file that stays pure ASCII keeps the full-speed byte
path, and its counts are identical to what the decoder would
produce, because ASCII decodes to itself. Only files that actually
contain a high byte pay for the multibyte decoder, and then only
from the first high byte on.
3. **Parallel across cores, sized to the work.** A thread has to earn its
spawn, which costs about what counting 512 KiB costs, so a mapping
gets one slice per 512 KiB (256 KiB for `-L`, whose kernel is several
times the work per byte), capped at the CPUs the process may actually
use. That number comes from `sched_getaffinity`, narrowed by the
cgroup CPU quota when one is set, with `sysconf` as the last resort —
`sysconf` answers for the whole machine, which is the wrong answer
under `taskset` and the wrong answer inside a container. The kernels
are pure, so the split needs no locks; word boundaries between slices
are seeded from the byte before the slice, which makes the split
exact.
4. **A long file list runs on a pool.** Past eight names, one worker per
core takes the next file and the rows are printed in argument order
when they finish. A thousand 1 MiB files spend nearly all their time
in the kernel, so the pool is the difference between 45 ms and 150 ms.
The workers turn off per-file slicing: two layers of parallelism only
oversubscribe each other.
5. **`-L` folds by maximum.** A line's width depends on where the line
starts, which looks like sequential state. It isn't: a slice that
begins at a line end needs nothing from its neighbours, and the fold
is a max. Inside a block, the printable lanes are prefix-summed, the
values at the line ends are packed together with one compress, and one
subtract turns them into every line width the block ends. Two or
sixty lines per block cost the same. Blocks with a tab walk their runs
instead, because a tab stop depends on where in the line it sits.
6. **No work that isn't asked for.** `-c` on a regular file is `st_size`
from `fstat` — GNU figured that one out too, so we copied the good
idea, and extended it to `-m` in a single-byte locale, where a
character is a byte. `-l` without `-w` skips the whitespace mask
entirely; `-w` without `-L` never builds the print table.
7. **ASCII pays nothing, even in a UTF-8 locale.** Multibyte decoding is
expensive, so we don't volunteer for it. A sample of the first few
KiB and two points in the middle decides whether a file is worth
decoding at all, which saves the wasted probe pass on files that are
multibyte everywhere. Files the sample calls ASCII still run the SIMD
probe, so a late high byte costs one wasted pass and nothing else.
When a file really is UTF-8, the mapping is split into chunks that
start at line ends and decode independently (UTF-8 has no shift
state), ASCII stretches inside each chunk go to the byte kernels, and
only the characters around a high byte are decoded one at a time.
Malformed bytes fall back to the streaming decoder, whose error
handling is GNU's, byte for byte.
## The `-L` hole, closed
`-L` was the one thing this program was bad at: a serial walk of the
mapped file at 0.7 GB/s. Against the Rust clone it lost 6-8x, and it lost
to GNU on the big files. Three kernels later it doesn't:
| case | before | after | Rust clone | GNU |
|------|-------:|------:|-----------:|----:|
| `-L`, 11 MB | 13.2 ms | **1.9 ms** | 4.3 ms | 16.6 ms |
| `-L`, 110 MB | 136 ms | **10.4 ms** | 21.4 ms | 157 ms |
| `-L`, 1.1 GB | 1204 ms | **61.5 ms** | 142 ms | 1506 ms |
| `-L`, 11 GB | 11.9 s | **0.56 s** | 1.29 s | 15.3 s |
The kernel ladder: scalar for anything the CPU can't do better,
AVX-2 (32-byte masks, run walk), AVX-512 (64-byte masks, same walk), and
AVX-512 with VBMI2 (prefix-summed printable lanes, compressed line-end
values, one packed subtract). Measured on one core over 110 MB of
11-byte lines: 0.67 GB/s scalar, 1.27 AVX-2, 1.55 AVX-512, 3.39 for the
prefix-sum walk. The last one is not a wider vector; it's less work per
line.
## The multibyte path, split
The UTF-8 decoder used to be the same story: correct, GNU-exact, and
serial. It paid for every byte of a file, after a probe pass had already
read the whole thing once. Now a UTF-8 file is split at line ends, each
chunk decodes without inheriting state, ASCII stretches go to the byte
kernels, and only the characters around a high byte are decoded. 10 MB
under `en_US.UTF-8`:
| case | before | after | Rust clone | GNU |
|------|-------:|------:|-----------:|----:|
| words, lightly multibyte | 26.7 ms | **1.6 ms** | 2.8 ms | 30.2 ms |
| words, dense CJK | 33.8 ms | **7.8 ms** | 2.7 ms | 38.9 ms |
| longest line, dense CJK | 43.0 ms | **11.5 ms** | 6.0 ms | 46.8 ms |
Dense CJK is the one case the clone still wins, because it carries a
width table and we call `wcwidth` so the counts agree with GNU on every
code point. We are 5x ahead of GNU there; the parity we care about is
with the oracle, not with the clone.
## Threads you actually have
`sysconf(_SC_NPROCESSORS_ONLN)` reports the machine. A process under
`taskset -c 0-3` has four CPUs, a container with `cpu.max = 200000
100000` has two, and a 128-core server whose cgroup gives you eight has
eight. Oversubscribing a quota buys wall-clock noise, not throughput.
`cpu_count()` takes the affinity mask, narrows it by the cgroup v2 quota
(or the v1 pair), and only falls back to `sysconf` when neither answers.
It runs lazily, on the first mapping big enough to be sliced, so tiny
files never pay for it.
## Correctness is the other half of the contract
@@ -163,12 +243,25 @@ oracles. Matching GNU byte for byte took some archaeology:
The selftest (`cc -DFASTWC_SELFTEST src/main.c`) checks every kernel
against the scalar reference over all sizes, both carry states, and
both counting modes, plus the threaded slice split. A randomized fuzz
against GNU as oracle (both locales, binary and UTF-8 data, every flag
combination) passes 100%.
both counting modes, plus the threaded slice split. Every width kernel
is checked the same way, from a line start and from a carried line
position, and the split width pass is checked against one serial pass for
every thread count. A randomized fuzz against GNU as oracle (both
locales, binary and UTF-8 data, every flag combination) passes 100%.
## Known divergences, stated plainly
- **Dense CJK is not where we win.** `-w` and `-m` over text that is
multibyte end to end decode a character at a time, because a correct
count needs `wcwidth`/`iswspace` per code point. The Rust clone carries
its own table and is 2-3x faster there. Both of us are 5x faster than
GNU, which is the number the contract cares about, and ASCII — the case
that actually shows up in a shell — takes the byte kernels.
- **A tab-heavy block walks its runs.** The prefix-sum width kernel
assumes a printable byte is worth one column. A tab is worth between
one and eight depending on where in the line it sits, so any block
containing one falls back to the per-run walk: still vectorized, about
half the speed of the prefix-sum path. Text without tabs is the norm.
- **GNU wc's count is transport-dependent.** The same bytes piped into
`wc -mw` can count differently than the same bytes read from a file,
because 64 KiB pipe chunks trigger a re-scan of its carried bytes.