The 100 MB numbers replace the 10 MB ones, because 10 MB was measuring
process startup as much as the decoder. Characters in dense CJK now beat
the clone by 2.5x; words and longest line still lose to it by a quarter,
which the divergences section says out loud instead of leaving the reader
to find it. The -c and -m rows join the head-to-head table: no stream is
opened for a count that is a file size.
Dense CJK was the last place the Rust clone beat us: every character
was decoded one at a time, in a loop whose branches always went the same
way. A 64-byte block of ASCII and three-byte characters is now counted
from masks instead - characters as bytes minus continuations, lines from
the newline mask, words from a byte-level white-space mask in which
every byte of a white-space character is marked, so a byte transition is
a character transition. A block is only accepted when it validates as
exactly that shape (continuations one and two bytes after their lead, no
overlong, no surrogate, no character running off either end), and a
refused block falls back to the character loop.
100 MB of CJK under en_US.UTF-8: characters 46.2 ms to 7.6 ms (the
clone's 18.7), words 55.8 ms to 8.6 ms (the clone's 6.8), longest line
59.1 ms to 43.1 ms. Lightly multibyte text is at or ahead of the clone
on every flag.
Also: -c and single-byte -m answer from the size the column width was
computed from, so a thousand small files cost the stat pass and nothing
else. 1050 x 1 MiB: -c 4.4 ms to 2.1 ms (the clone's 3.7), -m 2.7 ms
against the clone's 30.8.
Two fixes found on the way: the multibyte fallback restored its counts
after a chunked pass bailed and then decoded on top of the partial
sums, and the SIMD width kernels were disabled outside the C locale even
though a UTF-8 locale prints exactly the same ASCII.
-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.
-L was a serial byte-at-a-time walk, the multibyte path decoded every
byte of a file serially after wasting a probe pass on it, and a long
file list was counted one file at a time.
- Width now has its own kernel family: scalar, AVX-2, AVX-512, and an
AVX-512+VBMI2 prefix-sum walk that computes a whole block's line
widths at once. Slices start after a line end, so they fold by
maximum and -L runs across cores.
- UTF-8 chunks decode independently (no shift state), ASCII stretches
go to the byte kernels, and only the characters around a high byte
are decoded. A first-KiB sample skips the wasted probe pass, and
malformed input falls back to the streaming decoder's GNU-exact
error handling.
- Thread count follows a cost model (a thread per half megabyte, a
quarter for -L) over sched_getaffinity narrowed by the cgroup CPU
quota instead of a machine-wide sysconf count.
- File lists of eight or more are counted on a worker pool, one file
per worker, with rows printed in argument order and errors serialized.
- -m in a single-byte locale uses the file size like -c already did.
1060 MB of 1050 files: 45 ms against GNU's 1.5 s and the Rust clone's
145 ms. -L on 1.1 GB: 62 ms against 1.5 s and 142 ms.
Busybox wc is back in every suite, for shits and giggles. checkwc now
walks PATH and identifies each wc by its --version answer (coreutils and
fastwc respond; busybox names itself in the error it prints), so a wc
symlinked to fastwc is detected and skipped instead of silently racing
us against ourselves, and a missing coreutils is a clear configure
error. Every run now ends with the average speedup of fastwc against
coreutils and busybox, computed from the per-case ratios.
The fail-fast verdict now requires a measurable (>0ms) reference time,
so sub-millisecond cases stop flaking on startup noise, and the 100M
line monster is pinned back to the coreutils oracle instead of whatever
oracle was raced last.
Adds -L/--max-line-length (display width, tab stops every 8), the long
options --lines/--words/--chars/--bytes/--max-line-length, --files0-from
(NUL-delimited name lists), --total=auto|always|only|never, --debug
(acceleration diagnostics), and POSIXLY_CORRECT handling. Ports GNU's
column width rule (digits of the sum of regular file sizes, floor 7 for
non-regular inputs), its multibyte word/line-length semantics (-w/-m/-L
decode per character under a multibyte locale), the -c sizing heuristic
(page-aligned st_size reads a tail block), and its error texts, quoting
and exit codes.
Verified byte-identical stdout, exit codes and (normalized) stderr
against coreutils wc 9.11: 125-case parity suite and 552-case fuzz
across C, C.UTF-8, en_US.UTF-8 and POSIX locales. The benchmark contract
holds: words, lines (incl. the 100M monster) and stdin all pass with
fastwc never slower than GNU wc.
Sub-millisecond runs showed as a flat 0ms, hiding real differences. The
suite now times fastwc with a time_us helper, keeps the minimum reading
in µs, derives ms from it, and prints both (fastwc: 0ms (767µs)). GNU wc
stays at ms precision; solo case shows µs too.
count_sliced computed the slice size from the requested thread count and
only then clamped nt to MAX_THREADS, so the last slice ended before the
file tail: raising the cap (as pick_threads now does) silently dropped
the tail from every count. Cap nt first, then derive per.
Monsters (>=256 MiB) now get one thread per core (capped at 24) instead
of a hard 16, which measures ~1.17x on the 1B-line case and is flat on
warm files. check_sliced now sweeps the cap boundary (16, 24, 25).
Five distro recipes in packaging/, all following the same recipe:
unpack the Gitea release archive (which extracts to fastwc/), run
autoreconf -fi (the generated configure/Makefile.in are not committed),
configure, make, make install - the binary needs only glibc.
Each recipe notes the release prerequisite: the archive URL requires a
v0.1.0 tag on the remote, and Arch/Nix/Void carry an integrity
placeholder to fill in once the tag exists. The PKGBUILD was validated
with makepkg --printsrcinfo, and the full build recipe was run
end-to-end from a Gitea-layout tarball (autoreconf, configure, make,
make install DESTDIR) producing a working /usr/bin/fastwc.
The scalar AVX-512 mirror built its whitespace mask from ws_tab lookups,
so a bug in the kernel's actual predicate - the (x - 9) < 5 unsigned
range trick plus newline/space/NBSP compares - would pass the selftest
on hosts without AVX-512 and only surface on real hardware. The mirror
now computes the mask with the kernel's exact expression, and
check_kernel gains an exhaustive 256-byte ramp (forward and reversed)
so any divergence from the reference table changes a count. Verified by
mutation: shifting the range constant to (x - 8) < 5 makes the selftest
fail.
Review (5-lane) found one MAJOR: count_sliced ignored pthread_create's
return value - on EAGAIN it joined an indeterminate pthread_t and summed
an uninitialized slice, UB plus a silently wrong count. Threads are now
initialized to 0, a failed create counts its slice inline, and nt is
hard-capped at a named MAX_THREADS (the fixed jobs[8]/th[8] arrays
smash the stack past 8 threads - reproduced by QA's thread sweep).
Shipped from the measured optimization hunt:
- -w-only mode skips the newline compare/popcount entirely (the
(x-9)<5 range already covers '\n' in the whitespace mask) - a
need_lines gate threads through every kernel, the scalar reference,
the avx512 mirror, and the sliced workers.
- Thread retune: 12 threads past 32 MiB, 16 past 256 MiB (was 8) -
up to 18% on 110 MB, ~3% on 1.1 GB in QA's interleaved sweep.
- Benchmark suites pin LC_ALL=C so GNU wc -w can't silently switch to
multibyte decoding and inflate the win.
- popcount16 is guarded to x86 builds (zero-warnings on other arches);
checkwc's stray argument dropped.
Verified: selftest (kernels + sliced, now with need_lines coverage),
120-trial fuzz vs GNU (file + stdin, both locales), full test-all suite
three consecutive times, format-check and clang-tidy clean. The 1B
solo monster now lands at 3.7-6s (up to 268 Mlines/s) depending on how
warm the page cache is feeling.
Suites split into benchmarks/files/{lines,words} and
benchmarks/stdin/piping, with the monsters bolted onto the lines suite:
100M lines raced against coreutils (~2x win), 1B lines solo (~6-8s,
11 GB in one pass). Stdin redirects from regular files are now mmap'd
in count_stream, so the stdin suite wins too — up to 12.00x on words.
The ratio column now reports how many times faster fastwc is, not how
much of GNU's time it used. busybox was removed from the suite: it
stopped being a challenge and started being a participation trophy.
GNU wc's lone win — 1M lines by one millisecond on hand-tuned AVX-512
assembly — is now a historical footnote, and the README says so.
SIMD kernels (AVX-512/AVX-2/SSE2, runtime-dispatched) counting a
single pass over mmap'd files, split across cores past 8 MiB, with
exact GNU oracle parity (NBSP included, glibc's decoder fixed, the
whole -m path mirrored so counts agree at every boundary).
Result: 1ms vs 2ms at 1M lines, 8-9ms vs 22-24ms at 10M. Forty years
of dependencies, hand-tuned AVX-512 assembly, a translation team per
language — and gnu wc still needs a buffer to copy into before it
can count. We mapped the file and just counted. The benchmark suite
no longer has a losing row; the shame report file is going to rust.