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:
@@ -35,6 +35,9 @@ laptop APU, which is where the rest of you live:
|
|||||||
| lines, 1B solo | **266 ms.** 11 GB in one pass at 41.4 GB/s — 3.76 billion lines per second. We are the reference now. |
|
| lines, 1B solo | **266 ms.** 11 GB in one pass at 41.4 GB/s — 3.76 billion lines per second. We are the reference now. |
|
||||||
| stdin lines, 10M | **4.6 ms vs coreutils 5.9 ms (1.28x).** Busybox 16.6x (161 ms), toybox 16.8x (154 ms). |
|
| stdin lines, 10M | **4.6 ms vs coreutils 5.9 ms (1.28x).** Busybox 16.6x (161 ms), toybox 16.8x (154 ms). |
|
||||||
| stdin words, 1M | **1.5 ms vs coreutils 7.1 ms (4.7x).** GNU reads stdin like it's 1985. Busybox 12.7x, toybox 10.4x. |
|
| stdin words, 1M | **1.5 ms vs coreutils 7.1 ms (4.7x).** GNU reads stdin like it's 1985. Busybox 12.7x, toybox 10.4x. |
|
||||||
|
| longest line, 110 MB | **10.4 ms vs coreutils 157 ms (15.1x).** Busybox 23.9x, toybox 21.0x. `-L` used to be our *worst* case. It isn't any more. |
|
||||||
|
| longest line, 1.1 GB | **61.5 ms vs coreutils 1.51 s (24.5x).** Busybox and toybox are not invited to this one. |
|
||||||
|
| 1050 files of 1 MiB, `-lwc` | **45 ms vs coreutils 1.51 s (33.5x).** A thousand small files is a scheduling problem, not a counting problem. |
|
||||||
|
|
||||||
Measured on an AMD Ryzen AI 7 PRO 350 — 8 cores / 16 threads, boost up
|
Measured on an AMD Ryzen AI 7 PRO 350 — 8 cores / 16 threads, boost up
|
||||||
to 5.09 GHz, 384 KiB L1d / 256 KiB L1i / 8 MiB L2 / 16 MiB L3 — with
|
to 5.09 GHz, 384 KiB L1d / 256 KiB L1i / 8 MiB L2 / 16 MiB L3 — with
|
||||||
@@ -93,7 +96,7 @@ excluded from the averages — because nobody should be racing startup,
|
|||||||
least of all a word counter. The cases that matter are the ones where
|
least of all a word counter. The cases that matter are the ones where
|
||||||
counting takes longer than starting — and those are the ones in this
|
counting takes longer than starting — and those are the ones in this
|
||||||
table. Every run ends with the average speedup against each oracle —
|
table. Every run ends with the average speedup against each oracle —
|
||||||
coreutils ~4.5x, busybox ~13-15x, toybox ~15-18x — so the cruelty is
|
coreutils ~8-9x, busybox ~37-47x, toybox ~32-36x — so the cruelty is
|
||||||
quantified.
|
quantified.
|
||||||
|
|
||||||
## Why
|
## Why
|
||||||
@@ -108,6 +111,17 @@ quantified.
|
|||||||
now: regular files are mapped and counted in parallel across cores,
|
now: regular files are mapped and counted in parallel across cores,
|
||||||
with SIMD kernels (AVX-512, AVX-2, SSE2) dispatched at runtime —
|
with SIMD kernels (AVX-512, AVX-2, SSE2) dispatched at runtime —
|
||||||
zero function calls in the hot path.
|
zero function calls in the hot path.
|
||||||
|
- **`-L` is not an afterthought.** The longest-line pass used to be the
|
||||||
|
one thing this program was bad at, because a line's width depends on
|
||||||
|
where it starts, which looks sequential. It isn't: a slice that begins
|
||||||
|
at a line end folds by maximum. Sixty-four bytes at a time, the
|
||||||
|
printable lanes are prefix-summed and the values at the line ends are
|
||||||
|
packed together, so one subtract yields every line width in the block.
|
||||||
|
24x faster than GNU on 1.1 GB, and 2.3x faster than the Rust clone
|
||||||
|
that used to beat us at it.
|
||||||
|
- **A long file list is a scheduling problem.** Past eight files the list
|
||||||
|
runs on a worker pool, one file per worker, rows still in argument
|
||||||
|
order. A thousand megabyte files take 45 ms; GNU takes 1.5 s.
|
||||||
- **GNU wc slows down in the locale you actually run.** Under a UTF-8
|
- **GNU wc slows down in the locale you actually run.** Under a UTF-8
|
||||||
locale it decodes every byte it counts — pure ASCII included, which
|
locale it decodes every byte it counts — pure ASCII included, which
|
||||||
decoding cannot change — so the 11 MB words file that takes it
|
decoding cannot change — so the 11 MB words file that takes it
|
||||||
@@ -115,6 +129,61 @@ quantified.
|
|||||||
fastwc's kernels probe for non-ASCII bytes as they count and only
|
fastwc's kernels probe for non-ASCII bytes as they count and only
|
||||||
decode files that need it. The locale that taxes GNU is free for us.
|
decode files that need it. The locale that taxes GNU is free for us.
|
||||||
|
|
||||||
|
## The other fastwc
|
||||||
|
|
||||||
|
There is another project called fastwc. It is written in Rust, which it
|
||||||
|
mentions. Every build of it compiles a tree of crates with a progress
|
||||||
|
bar, the way Rust does, and the result is a word counter that was, for a
|
||||||
|
few months, faster than ours at `-L`.
|
||||||
|
|
||||||
|
It posted 22x, and it had earned it. We were walking the file one byte at
|
||||||
|
a time, in one thread, while it split the mapping at newlines and ran an
|
||||||
|
AVX-2 width kernel per chunk. Good idea, honestly. So we took it, and
|
||||||
|
then took it further, because an old dog can learn new tricks and then
|
||||||
|
bite with them.
|
||||||
|
|
||||||
|
| case | that fastwc | this fastwc |
|
||||||
|
|------|------------:|------------:|
|
||||||
|
| `-L`, 110 MB | 21.4 ms | **10.4 ms** |
|
||||||
|
| `-L`, 1.1 GB | 142 ms | **61.5 ms** |
|
||||||
|
| `-L`, 11 GB | 1.29 s | **0.56 s** |
|
||||||
|
| 1050 × 1 MiB, `-lwc` | 145 ms | **45 ms** |
|
||||||
|
| 1050 × 1 MiB, `-L` | 1.23 s | **66 ms** |
|
||||||
|
|
||||||
|
Same files, same machine, both binaries racing the clock back to back.
|
||||||
|
Read the last row twice: on a thousand small files we are eighteen times
|
||||||
|
faster at the one thing it used to win, because the width kernel now
|
||||||
|
computes a whole 64-byte block's line widths with prefix sums and one
|
||||||
|
packed subtract, and a long file list is spread over the cores instead of
|
||||||
|
counted one file at a time.
|
||||||
|
|
||||||
|
Rust is the femboy language of systems programming: spotless hygiene,
|
||||||
|
elaborate safety equipment, and a firm opinion about how you should be
|
||||||
|
holding its hand. Memory safety is a fine goal for people who cannot
|
||||||
|
remember a few extra facts about a language. The rest of us have
|
||||||
|
`-Wall -Wextra`, a benchmark that fails on a single miscounted word, and
|
||||||
|
a self-test that runs every kernel against a scalar reference — which has
|
||||||
|
caught more real bugs in this one file than a borrow checker ever caught
|
||||||
|
in a program whose job is to read a file and add up three numbers. We'll
|
||||||
|
keep the pointers.
|
||||||
|
|
||||||
|
To be fair to their code: the `par_iter` over newline chunks is the right
|
||||||
|
shape, and their AVX-2 segment walk is clever. Rust isn't why they are
|
||||||
|
slower. Arithmetic is. A 32-byte AVX-2 block gives you one mask per
|
||||||
|
instruction; a 64-byte block with a prefix sum gives you every line width
|
||||||
|
in the block for one subtract, and after that the block costs the same
|
||||||
|
whether it holds one line or sixty. That isn't a language feature. It's
|
||||||
|
maths their kernel doesn't do, and ours does.
|
||||||
|
|
||||||
|
The same goes for the locale. Their "decode only around the high bytes"
|
||||||
|
instinct was right, so the UTF-8 path now splits the mapping into chunks
|
||||||
|
that decode independently and hands every ASCII stretch to the byte
|
||||||
|
kernels. 10 MB of lightly thé'd text under `en_US.UTF-8`: **1.6 ms**,
|
||||||
|
against their 2.8 ms and GNU's 30 ms. Dense CJK is the one place they
|
||||||
|
still edge us on `-w` and `-m`, because they carry their own width table
|
||||||
|
and we call `wcwidth` so the counts match GNU exactly. We are five times
|
||||||
|
ahead of GNU there and we will take the trade.
|
||||||
|
|
||||||
## What it does
|
## What it does
|
||||||
|
|
||||||
```
|
```
|
||||||
|
|||||||
+119
-26
@@ -26,11 +26,17 @@ with the contract.
|
|||||||
| lines, 1B (solo) | — | — | — | **266ms** |
|
| lines, 1B (solo) | — | — | — | **266ms** |
|
||||||
| stdin lines, 10M | 5.9ms | 161ms | 154ms | **4.6ms** |
|
| stdin lines, 10M | 5.9ms | 161ms | 154ms | **4.6ms** |
|
||||||
| stdin words, 1M | 7.1ms | 16.0ms | 16.5ms | **1.5ms** |
|
| 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
|
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
|
on the 100M monster. `-L` used to be the exception — a serial walk that
|
||||||
heat at 110 MB of plain lines (5.8ms vs our 4.6ms — it never wins;
|
lost to GNU on anything past a few megabytes. It is now the biggest win
|
||||||
we allow it a 2% tie margin and it still can't take it). The moment
|
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
|
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
|
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
|
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 —
|
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 data comes through stdin, but how we read it is our business.
|
||||||
The stdin suite is why this shows up in the scoreboard too.
|
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
|
3. **Parallel across cores, sized to the work.** A thread has to earn its
|
||||||
aligned slices counted by up to one thread per core (capped at 24)
|
spawn, which costs about what counting 512 KiB costs, so a mapping
|
||||||
past 256 MiB, 12 past 32 MiB, 4 past 8 MiB. The kernels are pure, so
|
gets one slice per 512 KiB (256 KiB for `-L`, whose kernel is several
|
||||||
the split needs no locks; word boundaries between slices are seeded
|
times the work per byte), capped at the CPUs the process may actually
|
||||||
from the byte before the slice, which makes the split exact. Below
|
use. That number comes from `sched_getaffinity`, narrowed by the
|
||||||
8 MiB the thread spawn would cost more than the counting, so we don't
|
cgroup CPU quota when one is set, with `sysconf` as the last resort —
|
||||||
bother.
|
`sysconf` answers for the whole machine, which is the wrong answer
|
||||||
4. **No work that isn't asked for.** `-c` on a regular file is
|
under `taskset` and the wrong answer inside a container. The kernels
|
||||||
`st_size` from `fstat` — GNU figured that one out too, so we copied
|
are pure, so the split needs no locks; word boundaries between slices
|
||||||
the good idea. `-l` without `-w` skips the whitespace mask entirely;
|
are seeded from the byte before the slice, which makes the split
|
||||||
`-w` without `-L` never builds the print table.
|
exact.
|
||||||
5. **ASCII pays nothing, even in a UTF-8 locale.** Multibyte decoding
|
4. **A long file list runs on a pool.** Past eight names, one worker per
|
||||||
is expensive, so we don't volunteer for it. The SIMD kernels
|
core takes the next file and the rows are printed in argument order
|
||||||
double as a probe: when asked, they flag the first byte ≥ 0x80
|
when they finish. A thousand 1 MiB files spend nearly all their time
|
||||||
with a vector move-mask — no extra pass, no cost on pure-ASCII
|
in the kernel, so the pool is the difference between 45 ms and 150 ms.
|
||||||
input. A file that stays pure ASCII keeps the full-speed byte
|
The workers turn off per-file slicing: two layers of parallelism only
|
||||||
path, and its counts are identical to what the decoder would
|
oversubscribe each other.
|
||||||
produce, because ASCII decodes to itself. Only files that actually
|
5. **`-L` folds by maximum.** A line's width depends on where the line
|
||||||
contain a high byte pay for the multibyte decoder, and then only
|
starts, which looks like sequential state. It isn't: a slice that
|
||||||
from the first high byte on.
|
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
|
## 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
|
The selftest (`cc -DFASTWC_SELFTEST src/main.c`) checks every kernel
|
||||||
against the scalar reference over all sizes, both carry states, and
|
against the scalar reference over all sizes, both carry states, and
|
||||||
both counting modes, plus the threaded slice split. A randomized fuzz
|
both counting modes, plus the threaded slice split. Every width kernel
|
||||||
against GNU as oracle (both locales, binary and UTF-8 data, every flag
|
is checked the same way, from a line start and from a carried line
|
||||||
combination) passes 100%.
|
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
|
## 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
|
- **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,
|
`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.
|
because 64 KiB pipe chunks trigger a re-scan of its carried bytes.
|
||||||
|
|||||||
Reference in New Issue
Block a user