Compare commits

..
6 Commits
13 changed files with 751 additions and 259 deletions
+1
View File
@@ -54,6 +54,7 @@ dkms.conf
# ---> fastwc benchmarks
benchmarks/tools/genfile
benchmarks/tools/timeit
benchmarks/.data/
benchmarks/files/*/.data/
benchmarks/stdin/piping/.data/
+22 -15
View File
@@ -10,18 +10,25 @@ These aren't guidelines. They're the deal.
### 1. The benchmark is the contract
`./benchmarks/test-all.sh` must pass. Not "mostly pass." Not "pass on
your machine." Pass. Every suite — words, lines (monsters included),
and stdin — races fastwc against GNU wc.
your machine." Pass. Every suite — words (growing to 10M lines), lines
(monsters included), and stdin — races fastwc against GNU coreutils
wc, busybox wc, and toybox wc at microsecond resolution.
The moment fastwc is slower than GNU wc — or disagrees with it on a
single count — your change does not ship. We did not spend this much
effort being faster than a forty-year-old dependency museum just so you
could add a `strlen()` in the hot loop.
The moment fastwc is slower than any of them on a real race — or
disagrees with any of them on a single count — your change does not
ship. We did not spend this much effort being faster than a
forty-year-old dependency museum just so you could add a `strlen()` in
the hot loop.
The old standing exception — the 1M-line case, where GNU's hand-tuned
AVX-512 assembly squeaked out a one-millisecond win — is closed. We
beat them there too now (see docs/PERFORMANCE.md). There are no
exceptions left, and there is no excuse to be slower anywhere else.
The benchmark is honest about what it measures. Both sides are timed
by a C timer, and a case the reference finishes in under 5 ms is
startup, not throughput: fastwc must still count it right, but the
case is reported as startup-bound and excluded from the averages.
Raced cases allow a 2% dead-heat margin so a genuine tie can't flake
on scheduler jitter. That margin exists because GNU's threaded counter
occasionally dead-heats with us on mid-size files — it has never won a
raced case, and no exception is carved out for it
(see docs/PERFORMANCE.md).
### 2. C99, or don't bother
@@ -68,11 +75,11 @@ feature needs a new file, your feature is over-engineered.
### 7. Tests are the benchmarks
There is no separate test suite, because the benchmark *is* the test
suite — it checks correctness against GNU wc (the only oracle around,
and it's wrong often enough to keep us humble) and speed in the same
breath. Add a flag? It gets benchmarked. Change the counting? It gets
benchmarked. The fail-fast scripts in `benchmarks/` are your tests, and
all of them must pass before you open a PR.
suite — it checks correctness against GNU coreutils wc, busybox wc,
and toybox wc (all three are wrong often enough to keep us humble) and
speed in the same breath. Add a flag? It gets benchmarked. Change the
counting? It gets benchmarked. The fail-fast scripts in `benchmarks/`
are your tests, and all of them must pass before you open a PR.
### 8. No AI slop
+6
View File
@@ -3,10 +3,16 @@ AM_CFLAGS = -Wall -Wextra -O2 -pthread
bin_PROGRAMS = fastwc
fastwc_SOURCES = src/main.c
# configure.ac fills this in with -static when the default musl toolchain
# was selected (empty for --enable-glibc builds).
fastwc_LDFLAGS = @STATIC_LDFLAGS@
# Release build consumed by benchmarks/ (expects bin/release/fastwc).
# Strip the copy, not the tree binary: the debug build stays debuggable.
release: all
$(MKDIR_P) bin/release
cp -f fastwc bin/release/fastwc
$(STRIP) bin/release/fastwc
# Convenience: build the release binary, then run every benchmark suite.
bench: release
+108 -34
View File
@@ -17,34 +17,84 @@ correctly, at full speed.
## The scoreboard
The benchmark suite in `benchmarks/` races fastwc against GNU `wc` —
fail-fast. The moment we are slower, or disagree on a single count, it
writes a shame report and exits non-zero. These are the facts:
The benchmark suite in `benchmarks/` races fastwc against GNU `wc`,
busybox `wc`, and toybox `wc` — fail-fast. The moment we are slower, or
disagree on a single count, it writes a shame report and exits
non-zero. These are the facts. Earlier numbers were measured on a
workstation with more cores than taste; these are from an ordinary
laptop APU, which is where the rest of you live:
| Suite | Result |
|-------|--------|
| words (6 cases) | **6/6 wins.** Never slower, never wrong. |
| lines (up to 100k lines) | **Wins.** GNU never sees us coming. |
| lines (1M lines) | **Win: 1ms vs 2ms.** GNU's AVX-512 assist can't beat a mapped file. |
| lines (10M lines) | **Win: 8-9ms vs 22-24ms (~2.5x).** GNU's lead never survives contact with the buffer. |
| lines (100M lines) | **Win: ~70ms vs ~140ms.** The monster race. GNU gets lapped. |
| lines (1B lines) | **Solo, ~4-6s.** 11 GB in one pass; the only bottleneck left is the disk. |
| stdin words (1M lines) | **Win: 12x.** GNU still reads stdin like it's 1985. |
| stdin lines (10M lines) | **Win: ~2.5x.** We map stdin redirects; GNU maps nothing. |
| words, tiny (1-10k lines) | **6/6 vs coreutils, busybox, and toybox.** Correct on every one — but these races finish in the low hundreds of microseconds and prove *right*, not *fast*. See the note on startup below. |
| words, 1M lines (11 MB) | **1.6 ms vs coreutils 7.9 ms (4.9x).** Busybox 10.0x (16.2 ms), toybox 9.9x (15.9 ms). |
| words, 10M lines (110 MB) | **5.1 ms vs coreutils 66 ms (13.1x).** Busybox 27.0x (160 ms), toybox 15.9x (157 ms). |
| lines, 1M (11 MB) | **1.6 ms vs busybox 16.9 ms (10.4x)**, toybox 13.3x (17.6 ms). vs coreutils this is startup, not throughput — see below. |
| lines, 10M (110 MB) | **4.6 ms vs coreutils 5.8 ms (1.27x).** GNU throws every thread at a mapped file and the scoreboard doesn't move. Busybox 20.1x (160 ms), toybox 31.3x (154 ms). |
| lines, 100M monster | **29.2 ms vs coreutils 47.5 ms (1.6x).** The bigger the file, the wider the gap. |
| 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 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. |
The moment fastwc is slower than GNU `wc`, this project has failed and
you should say so loudly in an issue. The benchmark is the contract.
The how and why of the speed, with receipts, lives in
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
64 GiB of DDR5-4800 dual-channel (2x 32 GiB SODIMM). Opponents: GNU
coreutils 9.11, busybox 1.36.1, toybox 0.8.13. Page-cache-warm files,
three interleaved runs per case, minimum kept, both sides timed at
microsecond resolution by a C timer (`benchmarks/tools/timeit`) — no
`date`-fork rounding. Cases where the reference itself finishes in
under 5 ms are startup, not throughput: fastwc must still match the
count, and the case is reported, but it is excluded from the averages.
Raced cases allow a 2% dead-heat margin so a genuine tie can't flake
on scheduler jitter. All of it, exactly as `benchmarks/` prescribes.
And every number above was measured in the C locale — the setting that
flatters the opponents most. Under `en_US.UTF-8`, GNU `wc` stops
counting bytes and starts decoding them, one `mbrtowc` at a time, even
when the file is pure ASCII and decoding changes nothing. We used to
make that exact mistake: a UTF-8 locale silently swapped our SIMD
kernels for the same decoder, and the 11 MB words race flipped from a
4.9x win to a GNU win. The kernels now probe for non-ASCII bytes while
they count — a vector move-mask per load, free when unused — so ASCII
files never see the decoder. Same 11 MB words file, the locale you
actually run: **1.4 ms vs GNU 10.4 ms (7.6x).** GNU still pays that
10.4 ms for bytes that were never multibyte; we pay for the decoder
only when a file genuinely needs it. Receipts in
[docs/PERFORMANCE.md](docs/PERFORMANCE.md).
GNU wc's lone win — 1M lines, by a single millisecond, on hand-tuned
AVX-512 assembly — is a historical footnote now. We closed it, then the
10M case, then the 100M monster, and then we added a 1B-line race with
no opponent, because beating GNU wc at this point is just cruelty to
software. Busybox is back in the suite too — for shits and giggles. It
stopped being a challenge years ago, but watching it count is the
closest thing this project has to a hobby. Every run ends with the
average speedup against each oracle, so the cruelty is quantified.
The moment fastwc is slower than any of them, this project has failed
and you should say so loudly in an issue. The benchmark is the
contract. The how and why of the speed, with receipts, lives in
[docs/PERFORMANCE.md](docs/PERFORMANCE.md).
Toybox joined the suite for the same reason busybox is still there:
shits and giggles. It stopped giggling around the first average —
~15x — and it has never won a single case; counting 10M lines takes
it 16 times as long as us, and busybox 27. GNU coreutils is the only
opponent that keeps score, and even its threaded counter only manages
a dead heat on mid-size files — never a win, and the moment the file
stops fitting in a polite buffer, the dead heat stops being polite.
A note on startup, in the interest of honesty — and of gloating: on a
one-line file the whole race happens in the low hundreds of
microseconds, and fastwc now wins it outright. The default build is
static musl, so there is no dynamic loader to pay: min-of-400 on a
12-byte file puts fastwc `-l` at 78µs against busybox's 83µs, GNU's
253µs, and toybox's 254µs. Busybox's one structural advantage — a
loader it never had to start — is no longer an advantage; we don't
start one either. None of this matters in the bigger picture, and we
will not pretend otherwise: nobody will ever notice a difference that
small, and the tiny cases in the table are here to prove fastwc is
never *wrong*, not to brag about a head start that evaporates the
moment the page cache warms up. That is why the suite now times both
sides at microsecond resolution and files anything the reference
finishes in under 5 ms under "startup-bound": correct, reported, and
excluded from the averages — because nobody should be racing startup,
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
table. Every run ends with the average speedup against each oracle —
coreutils ~4.5x, busybox ~13-15x, toybox ~15-18x — so the cruelty is
quantified.
## Why
@@ -58,6 +108,12 @@ average speedup against each oracle, so the cruelty is quantified.
now: regular files are mapped and counted in parallel across cores,
with SIMD kernels (AVX-512, AVX-2, SSE2) dispatched at runtime —
zero function calls in the hot path.
- **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
decoding cannot change — so the 11 MB words file that takes it
7.9 ms in the C locale takes 10.4 ms there, against our 1.4 ms.
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.
## What it does
@@ -86,10 +142,19 @@ fastwc [OPTION]... --files0-from=F
Requires a C compiler and autotools. That's it. No gettext. No gnulib.
No translators.
The default build links statically against musl (via `musl-gcc`),
which is why fastwc now wins the startup cases above outright —
there is no dynamic loader to pay, and the ~80µs exec floor is the
same one busybox pays. If `musl-gcc` isn't installed the configure
script warns and falls back to the system compiler; the glibc build
is one flag away:
```sh
./autogen.sh # autoreconf -fi && ./configure
./autogen.sh # autoreconf -fi && ./configure (static musl)
make
make release # installs the release binary to bin/release/fastwc
./configure --enable-glibc && make # or: dynamic glibc build
```
## Benchmark
@@ -100,17 +165,26 @@ make bench # build release + run every suite
```
The suites live under `benchmarks/files/{lines,words}` (file input) and
`benchmarks/stdin/piping` (standard input), and fail fast the moment
fastwc loses a single case. Every suite races against GNU coreutils wc
and, when it is installed, busybox wc. The benchmark locates the real
oracles by their `--version` answer — a `wc` that answers as fastwc is a
symlink somebody made, and is skipped rather than raced against itself.
The lines suite also carries the monsters: 100M lines raced against
coreutils, and 1B lines timed solo — there is no reference for that
one; we are the reference now. The suites interleave runs so both
commands see identical cache warmth, keep the minimum, and fail the
moment fastwc loses a single case. When the run ends, the average
speedup of fastwc against each oracle is printed.
`benchmarks/stdin/piping` (standard input). Every suite races fastwc
against GNU coreutils wc and, when it is installed, busybox wc and
toybox wc. The benchmark locates the real oracles by their `--version`
answer — a `wc` that answers as fastwc is a symlink somebody made, and
is skipped rather than raced against itself.
Both sides are timed at microsecond resolution by `tools/timeit`, a C
timer built on the spot (`test-all.sh` compiles it next to `genfile`)
that forks the command, discards its output, and reads the clock after
it exits — no `date`-fork rounding. The suites interleave runs so both
commands see identical cache warmth and keep the minimum of three.
A case where the reference itself finishes in under 5 ms is startup,
not throughput: fastwc must still match the count, but the case is
reported as `startup-bound` and excluded from the averages. Raced
cases allow a 2% dead-heat margin so a genuine tie can't flake — but a
real loss writes a shame report, fails the suite, and exits non-zero.
The words suite grows to 1M and 10M lines; the lines suite carries the
monsters: 100M lines raced against coreutils, and 1B lines timed solo
— there is no reference for that one; we are the reference now. When
the run ends, the average speedup of fastwc against each oracle is
printed.
## Development
+8 -7
View File
@@ -1,9 +1,9 @@
#!/usr/bin/env bash
# bench.sh — race fastwc against GNU coreutils wc on line counts, file input.
# Busybox wc is raced too, for shits and giggles. Includes the monster
# cases: 100M lines raced against coreutils (busybox through 1.1 GB is a
# spectator sport, not a benchmark), and 1B lines timed solo (no
# reference to beat — there isn't one).
# Busybox and toybox wc are raced too, for shits and giggles. Includes
# the monster cases: 100M lines raced against coreutils (busybox/toybox
# through 1.1 GB is a spectator sport, not a benchmark), and 1B lines
# timed solo (no reference to beat — there isn't one).
# Fails fast: the moment fastwc is slower than (or disagrees with) an
# oracle wc, a human readable report is written to FAILED-benchmark.txt
# and this script exits non-zero.
@@ -23,7 +23,7 @@ for oracle in $ORACLES; do
printf 'benchmarking %s wc vs fastwc: lines, file input (%s interleaved runs each, minimum kept)\n' \
"$BENCH_NAME" "$BENCH_REPS"
printf '%-28s %10s %22s %8s %s\n' 'test' 'wc' 'fastwc (ms µs)' 'ratio' 'status'
printf '%-28s %16s %24s %8s %s\n' 'test' 'wc' 'fastwc (µs)' 'ratio' 'status'
printf '%s\n' '--- lines ---'
if ! run_cases lines -l 10000 100000 1000000 10000000; then
@@ -34,8 +34,9 @@ for oracle in $ORACLES; do
"$BENCH_NAME" "$BENCH_NAME"
done
# The monsters stay coreutils-only (busybox through 1.1 GB is a spectator
# sport, not a benchmark), so switch back from the last oracle raced.
# The monsters stay coreutils-only (busybox/toybox through 1.1 GB is a
# spectator sport, not a benchmark), so switch back from the last oracle
# raced.
select_oracle coreutils || exit 1
printf '%s\n' '--- monster: 100M lines vs coreutils ---'
if ! run_cases lines -l 100000000; then
+3 -3
View File
@@ -1,6 +1,6 @@
#!/usr/bin/env bash
# bench.sh — race fastwc against GNU coreutils wc on word counts, file input.
# Busybox wc is raced too, for shits and giggles.
# Busybox and toybox wc are raced too, for shits and giggles.
# Fails fast: the moment fastwc is slower than (or disagrees with) an
# oracle wc, a human readable report is written to FAILED-benchmark.txt
# and this script exits non-zero.
@@ -20,10 +20,10 @@ for oracle in $ORACLES; do
printf 'benchmarking %s wc vs fastwc: words, file input (%s interleaved runs each, minimum kept)\n' \
"$BENCH_NAME" "$BENCH_REPS"
printf '%-28s %10s %22s %8s %s\n' 'test' 'wc' 'fastwc (ms µs)' 'ratio' 'status'
printf '%-28s %16s %24s %8s %s\n' 'test' 'wc' 'fastwc (µs)' 'ratio' 'status'
printf '%s\n' '--- words ---'
if ! run_cases words -w 1 10 100 1000 10000 100000; then
if ! run_cases words -w 1 10 100 1000 10000 100000 1000000 10000000; then
exit 1
fi
+104 -123
View File
@@ -12,7 +12,7 @@
# checkfastwc() verify a release build of fastwc exists
# checkwc() locate the coreutils wc implementation
# createtxt() create (or reuse) a text file with N such lines
# time_ms() run a command once, print elapsed wall time in ms
# timeit() run a command once, print elapsed wall time in µs
# capture_count() print the first whitespace-separated field of output
# run_case() race fastwc against the reference on a file argument
# run_stdin_case() same, but feeding the file through standard input
@@ -36,9 +36,14 @@ export LC_CTYPE=C
FASTWC="$REPO_DIR/bin/release/fastwc"
DATA_DIR="$SCRIPT_DIR/.data"
GENFILE="$REPO_DIR/benchmarks/tools/genfile" # optional C helper, built by test-all.sh
TIMEIT="$REPO_DIR/benchmarks/tools/timeit" # µs exec timer, built by test-all.sh
BENCH_NAME="${BENCH_NAME:-wc}" # set by the caller: coreutils
BENCH_REPS="${BENCH_REPS:-3}" # interleaved runs per case; minimum is kept
# A reference wc that finishes under this many µs was really measuring
# startup, not throughput. Those cases are checked for correctness and
# reported, but excluded from the averages (see finish_race).
RACE_FLOOR_US="${RACE_FLOOR_US:-5000}"
RESULT_ROWS="" # accumulated results table
# Per-case speedup ratios accumulate here so the run can end with the
@@ -68,14 +73,16 @@ checkfastwc() {
}
# wc_impl <cmd...> — identify a wc implementation from its --version
# answer. coreutils and fastwc respond to --version; busybox does not
# have the option and names itself in the error it prints instead.
# answer. coreutils, fastwc and toybox respond to --version; busybox
# does not have the option and names itself in the error it prints
# instead.
wc_impl() {
local out
out=$("$@" --version 2>&1)
case "$out" in
*'GNU coreutils'*) printf 'coreutils\n' ;;
fastwc*) printf 'fastwc\n' ;;
toybox*) printf 'toybox\n' ;;
*BusyBox*) printf 'busybox\n' ;;
*) printf 'unknown\n' ;;
esac
@@ -85,15 +92,17 @@ wc_impl() {
# for every wc binary and identifies each by its --version answer. A wc
# that answers as fastwc is a symlink somebody made to our own binary —
# people do symlink wc to fastwc — and is skipped, because racing
# ourselves proves nothing. Busybox usually exists only as the
# multi-call binary, so that is probed too; it is back in the suite for
# shits and giggles, not because it is a challenge.
# Sets COREUTILS_CMD, BUSYBOX_CMD (empty if absent) and ORACLES, and
# points WC_CMD at coreutils. Exits if no coreutils wc is found.
# ourselves proves nothing. Busybox and toybox usually exist only as the
# multi-call binaries, so those are probed too; they are back in the
# suite for shits and giggles, not because they are a challenge.
# Sets COREUTILS_CMD, BUSYBOX_CMD, TOYBOX_CMD (empty if absent) and
# ORACLES, and points WC_CMD at coreutils. Exits if no coreutils wc is
# found.
checkwc() {
local dir impl
COREUTILS_CMD=()
BUSYBOX_CMD=()
TOYBOX_CMD=()
ORACLES=''
printf 'locating wc implementations... '
@@ -118,6 +127,12 @@ checkwc() {
printf 'busybox; '
fi
;;
toybox)
if [[ ${#TOYBOX_CMD[@]} -eq 0 ]]; then
TOYBOX_CMD=("$dir/wc")
printf 'toybox; '
fi
;;
*) ;;
esac
done
@@ -129,6 +144,13 @@ checkwc() {
printf 'busybox; '
fi
# same for toybox
if [[ ${#TOYBOX_CMD[@]} -eq 0 ]] && command -v toybox >/dev/null 2>&1 \
&& [[ "$(wc_impl toybox wc)" == 'toybox' ]]; then
TOYBOX_CMD=(toybox wc)
printf 'toybox; '
fi
if [[ ${#COREUTILS_CMD[@]} -eq 0 ]]; then
printf 'none\n'
printf 'configure: error: no coreutils wc found in PATH\n' >&2
@@ -140,14 +162,16 @@ checkwc() {
WC_CMD=("${COREUTILS_CMD[@]}")
ORACLES='coreutils'
[[ ${#BUSYBOX_CMD[@]} -gt 0 ]] && ORACLES="$ORACLES busybox"
[[ ${#TOYBOX_CMD[@]} -gt 0 ]] && ORACLES="$ORACLES toybox"
}
# select_oracle <coreutils|busybox> — point the racing functions at the
# chosen oracle by setting BENCH_NAME and WC_CMD.
# select_oracle <coreutils|busybox|toybox> — point the racing functions
# at the chosen oracle by setting BENCH_NAME and WC_CMD.
select_oracle() {
case "$1" in
coreutils) BENCH_NAME='coreutils'; WC_CMD=("${COREUTILS_CMD[@]}") ;;
busybox) BENCH_NAME='busybox'; WC_CMD=("${BUSYBOX_CMD[@]}") ;;
toybox) BENCH_NAME='toybox'; WC_CMD=("${TOYBOX_CMD[@]}") ;;
*) return 1 ;;
esac
return 0
@@ -199,24 +223,17 @@ createtxt() {
printf '%s\n' "$TEXT_FILE"
}
# time_ms <cmd...> — run a command once and print elapsed wall time in ms.
time_ms() {
local s e
s=$(date +%s%N)
"$@" >/dev/null 2>&1
e=$(date +%s%N)
printf '%s\n' "$(( (e - s) / 1000000 ))"
}
# time_us <cmd...> — run a command once and print elapsed wall time in µs.
# More precise than time_ms: sub-millisecond runs come out as e.g. 812,
# not 0. The benchmark keeps the µs reading for fastwc and derives the ms.
time_us() {
local s e
s=$(date +%s%N)
"$@" >/dev/null 2>&1
e=$(date +%s%N)
printf '%s\n' "$(( (e - s) / 1000 ))"
# timeit <cmd...> — run a command once and print elapsed wall time in µs
# on stdout, with the command's own output discarded. The clock starts in
# main(), after timeit's own loader has run, so every binary pays the same
# bookkeeping and the reading is the child's fork + exec + run + exit.
timeit() {
[[ -x "$TIMEIT" ]] || {
printf 'error: %s not built — run test-all.sh (or "make bench") first\n' \
"$TIMEIT" >&2
exit 1
}
"$TIMEIT" "$@"
}
# capture_count <cmd...> — print the first whitespace-separated field of a
@@ -225,10 +242,10 @@ capture_count() {
"$@" 2>/dev/null | awk 'NR == 1 { print $1 }'
}
# write_failed_report <label> <reason> <wc_ms> <fast_ms> — write the
# write_failed_report <label> <reason> <wc_us> <fast_us> — write the
# human readable failure report to FAILED-benchmark.txt next to the suite.
write_failed_report() {
local label="$1" reason="$2" wc_ms="$3" fast_ms="$4"
local label="$1" reason="$2" wc_us="$3" fast_us="$4"
local report="$SCRIPT_DIR/FAILED-benchmark.txt"
{
@@ -239,7 +256,7 @@ write_failed_report() {
printf 'failure : %s\n' "$reason"
printf '\nresults\n'
printf '%s\n' '-------'
printf '%-28s %10s %22s %8s %s\n' 'test' 'wc' 'fastwc (ms µs)' 'ratio' 'status'
printf '%-28s %16s %24s %8s %s\n' 'test' 'wc' 'fastwc (µs)' 'ratio' 'status'
printf '%s' "$RESULT_ROWS"
printf '\nfastwc must never be slower than %s wc — benchmark aborted.\n' "$BENCH_NAME"
} > "$report"
@@ -248,6 +265,45 @@ write_failed_report() {
printf 'full results written to %s\n' "$report" >&2
}
# finish_race <label> <wc_count> <fast_count> <wc_us> <fast_us> — turn the
# measured counts and times into a verdict. A count mismatch fails
# outright. A reference that finished under RACE_FLOOR_US was really
# measuring startup, not throughput: the case is reported as PASS
# (startup-bound) and left out of the averages. Raced cases pass when
# fastwc is not slower than the reference beyond a small dead-heat slack
# (200µs + 2%) that absorbs scheduler jitter on genuinely even races.
finish_race() {
local label="$1" wc_count="$2" fast_count="$3" wc_us="$4" fast_us="$5"
local ratio='-' verdict='PASS' reason='' row slack
if [[ "$fast_count" != "$wc_count" ]]; then
verdict='FAIL'
reason="output mismatch (fastwc: ${fast_count}, ${BENCH_NAME} wc: ${wc_count})"
elif (( wc_us < RACE_FLOOR_US )); then
verdict='PASS (startup-bound)'
else
ratio=$(awk -v f="$fast_us" -v w="$wc_us" 'BEGIN { printf "%.2fx", w / f }')
# feed the end-of-run average speedup (raced rows only)
printf '%s\t%s\n' "$BENCH_NAME" "${ratio%x}" >> "$RATIOS_FILE"
slack=$((200 + wc_us / 50))
if (( fast_us > wc_us + slack )); then
verdict='FAIL'
reason="fastwc was slower (fastwc: ${fast_us}µs vs ${BENCH_NAME} wc: ${wc_us}µs)"
fi
fi
row=$(printf '%-28s %16s %24s %8s %s\n' \
"$label" "wc: ${wc_us}µs" "fastwc: ${fast_us}µs" "$ratio" "$verdict")
RESULT_ROWS+="${row}"$'\n'
printf '%s\n' "$row"
if [[ "$verdict" == 'FAIL' ]]; then
write_failed_report "$label" "$reason" "$wc_us" "$fast_us"
return 1
fi
return 0
}
# run_case <words|lines> <n-lines> <-w|-l> — race the reference wc against
# fastwc on a file argument. Fails the benchmark the moment fastwc is
# slower or reports a different count.
@@ -255,7 +311,7 @@ run_case() {
local mode="$1" lines="$2" flag="$3"
local noun='lines'; [[ "$lines" -eq 1 ]] && noun='line'
local label="${mode} (${lines} ${noun})"
local file wc_count fast_count wc_ms fast_us fast_ms ratio verdict reason row
local file wc_count fast_count wc_us fast_us
local i d
file=$(createtxt "$lines") || return 1
@@ -264,58 +320,19 @@ run_case() {
wc_count=$(capture_count "${WC_CMD[@]}" "$flag" "$file")
fast_count=$(capture_count "$FASTWC" "$flag" "$file")
# speed: interleaved timing so both commands see identical cache warmth;
# keep the minimum of $BENCH_REPS runs each to reduce noise; fastwc is
# timed in µs so sub-millisecond wins are visible in the report
wc_ms=''
# speed: interleaved timing so both commands see identical cache
# warmth; keep the minimum of $BENCH_REPS runs each
wc_us=''
fast_us=''
for ((i = 0; i < BENCH_REPS; i++)); do
d=$(time_ms "${WC_CMD[@]}" "$flag" "$file")
[[ -z "$wc_ms" || "$d" -lt "$wc_ms" ]] && wc_ms="$d"
d=$(timeit "${WC_CMD[@]}" "$flag" "$file")
[[ -z "$wc_us" || "$d" -lt "$wc_us" ]] && wc_us="$d"
d=$(time_us "$FASTWC" "$flag" "$file")
d=$(timeit "$FASTWC" "$flag" "$file")
[[ -z "$fast_us" || "$d" -lt "$fast_us" ]] && fast_us="$d"
done
fast_ms=$(( fast_us / 1000 ))
# ratio: how many times faster fastwc is than the reference (wc / fastwc)
if [[ -n "$wc_ms" && "$wc_ms" -gt 0 ]]; then
if [[ "$fast_ms" -gt 0 ]]; then
ratio=$(awk -v f="$fast_ms" -v w="$wc_ms" 'BEGIN { printf "%.2fx", w / f }')
else
ratio='infx'
fi
else
ratio='-'
fi
# feed the end-of-run average speedup (numeric ratios only)
if [[ "$ratio" == *x && "$ratio" != 'infx' ]]; then
printf '%s\t%s\n' "$BENCH_NAME" "${ratio%x}" >> "$RATIOS_FILE"
fi
verdict='PASS'
reason=''
if [[ "$fast_count" != "$wc_count" ]]; then
verdict='FAIL'
reason="output mismatch (fastwc: ${fast_count}, ${BENCH_NAME} wc: ${wc_count})"
elif (( wc_ms > 0 && fast_ms > wc_ms )); then
# a 0ms reference is below the benchmark's resolution: sub-millisecond
# runs (startup noise, mostly) cannot prove fastwc slower
verdict='FAIL'
reason="fastwc was slower (fastwc: ${fast_ms}ms vs ${BENCH_NAME} wc: ${wc_ms}ms)"
fi
row=$(printf '%-28s %10s %22s %8s %s\n' \
"$label" "wc: ${wc_ms}ms" "fastwc: ${fast_ms}ms (${fast_us}µs)" \
"$ratio" "$verdict")
RESULT_ROWS+="${row}"$'\n'
printf '%s\n' "$row"
if [[ "$verdict" == 'FAIL' ]]; then
write_failed_report "$label" "$reason" "$wc_ms" "$fast_ms"
return 1
fi
return 0
finish_race "$label" "$wc_count" "$fast_count" "$wc_us" "$fast_us"
}
# run_stdin_case <words|lines> <n-lines> <-w|-l|...> — same race, but the
@@ -325,7 +342,7 @@ run_stdin_case() {
local mode="$1" lines="$2" flag="$3"
local noun='lines'; [[ "$lines" -eq 1 ]] && noun='line'
local label="stdin ${mode} (${lines} ${noun})"
local file wc_count fast_count wc_ms fast_us fast_ms ratio verdict reason row
local file wc_count fast_count wc_us fast_us
local i d
file=$(createtxt "$lines") || return 1
@@ -333,55 +350,17 @@ run_stdin_case() {
wc_count=$(capture_count "${WC_CMD[@]}" "$flag" < "$file")
fast_count=$(capture_count "$FASTWC" "$flag" < "$file")
wc_ms=''
wc_us=''
fast_us=''
for ((i = 0; i < BENCH_REPS; i++)); do
d=$(time_ms "${WC_CMD[@]}" "$flag" < "$file")
[[ -z "$wc_ms" || "$d" -lt "$wc_ms" ]] && wc_ms="$d"
d=$(timeit "${WC_CMD[@]}" "$flag" < "$file")
[[ -z "$wc_us" || "$d" -lt "$wc_us" ]] && wc_us="$d"
d=$(time_us "$FASTWC" "$flag" < "$file")
d=$(timeit "$FASTWC" "$flag" < "$file")
[[ -z "$fast_us" || "$d" -lt "$fast_us" ]] && fast_us="$d"
done
fast_ms=$(( fast_us / 1000 ))
# ratio: how many times faster fastwc is than the reference (wc / fastwc)
if [[ -n "$wc_ms" && "$wc_ms" -gt 0 ]]; then
if [[ "$fast_ms" -gt 0 ]]; then
ratio=$(awk -v f="$fast_ms" -v w="$wc_ms" 'BEGIN { printf "%.2fx", w / f }')
else
ratio='infx'
fi
else
ratio='-'
fi
# feed the end-of-run average speedup (numeric ratios only)
if [[ "$ratio" == *x && "$ratio" != 'infx' ]]; then
printf '%s\t%s\n' "$BENCH_NAME" "${ratio%x}" >> "$RATIOS_FILE"
fi
verdict='PASS'
reason=''
if [[ "$fast_count" != "$wc_count" ]]; then
verdict='FAIL'
reason="output mismatch (fastwc: ${fast_count}, ${BENCH_NAME} wc: ${wc_count})"
elif (( wc_ms > 0 && fast_ms > wc_ms )); then
# a 0ms reference is below the benchmark's resolution: sub-millisecond
# runs (startup noise, mostly) cannot prove fastwc slower
verdict='FAIL'
reason="fastwc was slower (fastwc: ${fast_ms}ms vs ${BENCH_NAME} wc: ${wc_ms}ms)"
fi
row=$(printf '%-28s %10s %22s %8s %s\n' \
"$label" "wc: ${wc_ms}ms" "fastwc: ${fast_ms}ms (${fast_us}µs)" \
"$ratio" "$verdict")
RESULT_ROWS+="${row}"$'\n'
printf '%s\n' "$row"
if [[ "$verdict" == 'FAIL' ]]; then
write_failed_report "$label" "$reason" "$wc_ms" "$fast_ms"
return 1
fi
return 0
finish_race "$label" "$wc_count" "$fast_count" "$wc_us" "$fast_us"
}
# run_solo_case <n-lines> <-l|...> — time fastwc alone on <n-lines> of data,
@@ -399,7 +378,7 @@ run_solo_case() {
}
for ((i = 0; i < BENCH_REPS; i++)); do
d=$(time_us "$FASTWC" "$flag" "$file")
d=$(timeit "$FASTWC" "$flag" "$file")
[[ -z "$best_us" || "$d" -lt "$best_us" ]] && best_us="$d"
done
best_ms=$(( best_us / 1000 ))
@@ -455,5 +434,7 @@ print_averages() {
printf "coreutils\t%.2fx\t%d\n", sum["coreutils"] / n["coreutils"], n["coreutils"];
if (n["busybox"])
printf "busybox\t%.2fx\t%d\n", sum["busybox"] / n["busybox"], n["busybox"];
if (n["toybox"])
printf "toybox\t%.2fx\t%d\n", sum["toybox"] / n["toybox"], n["toybox"];
}' "$RATIOS_FILE")"
}
+1 -1
View File
@@ -22,7 +22,7 @@ for oracle in $ORACLES; do
printf 'benchmarking %s wc vs fastwc: stdin (%s interleaved runs each, minimum kept)\n' \
"$BENCH_NAME" "$BENCH_REPS"
printf '%-28s %10s %22s %8s %s\n' 'test' 'wc' 'fastwc (ms µs)' 'ratio' 'status'
printf '%-28s %16s %24s %8s %s\n' 'test' 'wc' 'fastwc (µs)' 'ratio' 'status'
printf '%s\n' '--- stdin lines ---'
if ! run_stdin_cases lines -l 10000 100000 1000000 10000000; then
+10 -4
View File
@@ -1,9 +1,9 @@
#!/usr/bin/env bash
# test-all.sh — compile the benchmark helper tools first, then run every
# benchmark suite (words, lines incl. monsters, stdin) against every
# oracle found (coreutils, plus busybox for shits and giggles). Exits
# non-zero if any of them fails, and ends with the average speedup of
# fastwc against each oracle.
# oracle found (coreutils, plus busybox and toybox for shits and
# giggles). Exits non-zero if any of them fails, and ends with the
# average speedup of fastwc against each oracle.
#
# usage: ./test-all.sh
set -u
@@ -19,13 +19,19 @@ export RATIOS_FILE
source "$SCRIPT_DIR/std.sh"
# 1. build the helper tools before running any benchmark so createtxt()
# can use the fast C generator instead of the slow shell fallback
# can use the fast C generator instead of the slow shell fallback and
# the races are timed at µs resolution
mkdir -p "$SCRIPT_DIR/tools"
if ! cc -O2 -Wall -o "$SCRIPT_DIR/tools/genfile" "$SCRIPT_DIR/tools/genfile.c"; then
printf 'test-all: error: failed to compile %s\n' "$SCRIPT_DIR/tools/genfile.c" >&2
exit 1
fi
printf 'built %s\n' "$SCRIPT_DIR/tools/genfile"
if ! cc -O2 -Wall -o "$SCRIPT_DIR/tools/timeit" "$SCRIPT_DIR/tools/timeit.c"; then
printf 'test-all: error: failed to compile %s\n' "$SCRIPT_DIR/tools/timeit.c" >&2
exit 1
fi
printf 'built %s\n' "$SCRIPT_DIR/tools/timeit"
# 2. run each suite (all run regardless, so every result is reported)
"$SCRIPT_DIR/files/words/bench.sh"
+59
View File
@@ -0,0 +1,59 @@
/* timeit — run a command once and print its elapsed wall time in µs.
*
* The benchmark harness races fastwc against several wc implementations.
* Timing both sides by wrapping the command in `date +%s%N` forks added
* over a millisecond of noise per sample — more than the whole run on a
* small case — so every sub-millisecond race was decided by fork jitter,
* not by speed. This helper measures a plain fork + exec + wait with
* clock_gettime and prints the elapsed microseconds on its own stdout.
* The timed command's output is discarded, exactly as the old date
* wrapper did, so capture_count (which runs the command directly) is the
* only path that sees real output.
*
* usage: timeit <cmd> [arg...]
*/
#define _POSIX_C_SOURCE 200809L
#include <fcntl.h>
#include <stdio.h>
#include <sys/wait.h>
#include <time.h>
#include <unistd.h>
int main(int argc, char **argv)
{
struct timespec t0, t1;
pid_t pid;
int nullfd;
if (argc < 2)
return 2;
clock_gettime(CLOCK_MONOTONIC, &t0);
pid = fork();
if (pid < 0)
return 2;
if (pid == 0)
{
/* child: run the timed command with its output thrown away */
nullfd = open("/dev/null", O_WRONLY);
if (nullfd >= 0)
{
dup2(nullfd, STDOUT_FILENO);
dup2(nullfd, STDERR_FILENO);
close(nullfd);
}
execvp(argv[1], &argv[1]);
_exit(127);
}
if (waitpid(pid, NULL, 0) < 0)
return 2;
clock_gettime(CLOCK_MONOTONIC, &t1);
printf("%lld\n",
((long long)(t1.tv_sec - t0.tv_sec) * 1000000000LL
+ (t1.tv_nsec - t0.tv_nsec)) / 1000);
return 0;
}
+29
View File
@@ -2,9 +2,38 @@ AC_PREREQ([2.69])
AC_INIT([fastwc], [0.1.0], [], [fastwc])
AC_CONFIG_SRCDIR([src/main.c])
# Build flavor: --enable-glibc links dynamically against the system libc
# (respecting CC, whatever it is). The default is a fully static musl
# binary, preferring musl-gcc even when CC is set in the environment,
# since removing ld.so from startup is exactly what the tiny-file
# benchmark races hinge on.
AC_ARG_ENABLE([glibc],
[AS_HELP_STRING([--enable-glibc],
[link dynamically against the system libc (respects CC) instead of the default static musl])],
[enable_glibc=$enableval], [enable_glibc=no])
STATIC_LDFLAGS=
if test "x$enable_glibc" = xno; then
# Static musl is the default toolchain. Fall back to the system
# compiler (dynamic link) only when no musl compiler is installed.
AC_PATH_PROGS([MUSL_CC], [musl-gcc x86_64-linux-musl-gcc])
if test -n "$MUSL_CC"; then
CC="$MUSL_CC"
STATIC_LDFLAGS="-static"
fi
fi
AC_PROG_CC
AC_CHECK_TOOL([STRIP], [strip], [:])
AC_USE_SYSTEM_EXTENSIONS
AM_INIT_AUTOMAKE([foreign subdir-objects])
AS_IF([test "x$enable_glibc" = xyes],
[AC_MSG_NOTICE([fastwc: glibc build requested, using $CC])],
[AS_IF([test -n "$MUSL_CC"],
[AC_MSG_NOTICE([fastwc: building against static musl ($MUSL_CC)])],
[AC_MSG_WARN([fastwc: musl-gcc not found; falling back to $CC (dynamic link). Install a musl toolchain or configure --enable-glibc.])])])
AC_SUBST([STATIC_LDFLAGS])
AC_CONFIG_FILES([Makefile])
AC_OUTPUT
+123 -32
View File
@@ -6,27 +6,108 @@ that time, and what we paid for it.
## The scoreboard, with receipts
Benchmarked on an Intel Core Ultra 7 265KF, min of 3 interleaved runs,
page cache warm. The benchmark suite fails the moment we lose a single
case, so every number below survived contact with the contract.
Benchmarked 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 64 GiB of DDR5-4800 dual-channel. Opponents: GNU coreutils 9.11,
busybox 1.36.1, toybox 0.8.13. Page-cache-warm files, min of 3
interleaved runs, both sides timed at microsecond resolution by a C
timer. Only *raced* cases appear below — anything the reference
finishes in under 5 ms is startup, not throughput, and gets reported
but excluded (see "On startup" below). Every number survived contact
with the contract.
| Case | GNU coreutils | fastwc |
|------|--------------:|-------:|
| words, 100k lines | 1-2ms | ≤1ms |
| lines, 100k | 1-2ms | ≤1ms |
| lines, 1M | 2-3ms | **1ms** |
| lines, 10M | 21-24ms | **8-9ms** |
| lines, 100M (monster) | ~140ms | **~70ms** |
| lines, 1B (solo) | — | **~4-6s** |
| bytes, 1GB sparse | reads all of it | `st_size`, no read |
| Case | GNU coreutils | busybox | toybox | fastwc |
|------|--------------:|--------:|-------:|-------:|
| words, 1M lines (11 MB) | 7.9ms | 16.2ms | 15.9ms | **1.6ms** |
| words, 10M lines (110 MB) | 66ms | 160ms | 157ms | **5.1ms** |
| lines, 1M (11 MB) | startup | 16.9ms | 17.6ms | **1.6ms** |
| lines, 10M (110 MB) | 5.8ms | 160ms | 154ms | **4.6ms** |
| lines, 100M (monster) | 47.5ms | — | — | **29.2ms** |
| lines, 1B (solo) | — | — | — | **266ms** |
| stdin lines, 10M | 5.9ms | 161ms | 154ms | **4.6ms** |
| stdin words, 1M | 7.1ms | 16.0ms | 16.5ms | **1.5ms** |
That is a ~2.5x win over GNU on 10M lines, a 2x win on 1M lines, and
a 2x win on the 100M monster. At 1B lines — 11 GB of data — the solo
run lands around 4-6 seconds (200-270 Mlines/s, warm cache), and the
bottleneck is
honest to admit: an 11 GB file does not fit in the 15 GB of RAM this
machine has, so the last monster is racing the disk. The 100M case,
which fits, runs at ~17 GB/s, and that number is the counting.
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
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
second. The reference is us now. Busybox and toybox, meanwhile, are
here for the cruelty: 10-31x slower depending on the case, and their
word counting has *bugs*.
## The locale tax, gone
Every number above is C locale — the setting that flatters the
opponents most: GNU `wc -w` under a UTF-8 locale stops counting bytes
and decodes every one of them through `mbrtowc`, even when the file is
pure ASCII and decoding changes nothing. We used to pay that same tax:
the multibyte gate looked only at `MB_CUR_MAX`, so a UTF-8 locale
silently traded the SIMD kernels for the decoder, and the 11 MB words
race above flipped from a 4.9x win to a 25% loss against GNU.
The kernels now double as a probe — one vector move-mask per load
flags the first byte ≥ 0x80, free when unused — so only files that
actually contain a high byte fall back to the decoder. Receipts,
min-of-N interleaved, `en_US.UTF-8`, the same 11 MB ASCII words file:
| Case | GNU coreutils | fastwc | gap |
|------|--------------:|-------:|----:|
| words | 10.43ms | **1.38ms** | 7.6x |
| default (`-lwc`) | 10.55ms | **1.50ms** | 7.0x |
| characters (`-m`) | 10.58ms | **2.06ms** | 5.1x |
| longest line (`-L`) | 10.56ms | **6.83ms** | 1.5x |
GNU's decoder bill for that file is unchanged: 10.4ms, for bytes that
were never multibyte. Files that genuinely are multibyte still decode
at parity — 10.5 MB of mixed CJK+latin, 56.7ms against GNU's 56.9ms —
because there both sides decode. The one case GNU keeps is *lightly*
multibyte files: sparse UTF-8 costs us one wasted fast pass before the
fallback (793 KB, 1.30ms vs GNU's 1.18ms). We judged the tax worth
it; ASCII is the rule, multibyte is the exception.
## On startup
A word counter that loses one-line races to a slower counter is not
slower — it's slower to *start*, and the benchmark used to punish that
with integer-millisecond `date`-fork rounding. We fixed the honest
half and then the honest reporting:
- **What startup used to cost.** Staged-exit probes with a min-of-N
exec timer showed ~105µs of avoidable work above the glibc exec
floor: `setlocale` (~44µs), a `nl_langinfo` quote-style probe used
only by one error message (~26µs), two 256-entry `isspace`/`isprint`
table fills (~39µs), and runtime CPU dispatch — all of it paid even
for `--help` and `-c`.
- **What we did about it.** The locale chain (`LC_ALL` → `LC_CTYPE` →
`LANG`) is resolved first and `setlocale` is skipped entirely when it
resolves to C/POSIX or is unset — glibc's initial locale *is* C, so
the semantics are identical. The quote-style probe went lazy (only
the `--total=` error diagnostics ever print curly quotes). The
whitespace/print tables are constant-filled under the C locale and
built only when the flags need them (`-w`, `-L`). Option parsing
moved ahead of everything, so `--help`, `--version`, and usage
errors exit with zero startup cost.
- **The receipts.** Min-of-400 interleaved on an 11-byte file: fastwc
~0.25ms, GNU ~0.33ms, toybox ~0.37ms, busybox ~0.14ms. Before the
work, fastwc `-w` on a tiny file measured ~562µs; after, ~425µs.
Nobody will ever notice a difference that small. Those were the
dynamic-link numbers; the static musl default below starts ~3x
sooner than even those.
- **The honest half.** Because those microseconds don't matter, the
benchmark no longer pretends they do. Any case the reference
finishes in under 5ms is filed under `startup-bound`: fastwc must
still match the count, but the case is excluded from the averages
and the throughput scoreboard. Busybox used to win these outright —
it is a static musl binary, and skipping the dynamic loader bought
it the better part of a hundred microseconds on every exec. That
excuse retired itself when the default build went static musl too
(see the README): on a 12-byte file, min-of-400, fastwc `-l` now
lands at 78µs against busybox's 83µs, GNU's 253µs, and toybox's
254µs. The cases in the table above are the ones where counting
takes longer than starting.
## Why it's fast
@@ -35,7 +116,8 @@ which fits, runs at ~17 GB/s, and that number is the counting.
(64 bytes per step) when the CPU has it, else AVX-2 (32), else SSE2
(16), else scalar SWAR. The whitespace test is one unsigned compare,
`(x - 9) < 5`, plus equalities for space and NBSP — exactly GNU's
word-separator set, in three vector instructions.
word-separator set, in three vector instructions. Dispatch itself is
lazy: the CPUID probe runs only when a count will actually use it.
2. **Regular files are mapped, not streamed.** `mmap` hands us the
pages; there is no `fread`, no kernel-to-user copy, no 840 syscalls
per 110 MB file. One syscall in, one count out. A stdin redirect
@@ -51,12 +133,22 @@ which fits, runs at ~17 GB/s, and that number is the counting.
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.
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.
## Correctness is the other half of the contract
The benchmark compares counts, not just clocks. Matching GNU byte for
byte took some archaeology:
The benchmark compares counts, not just clocks — against all three
oracles. Matching GNU byte for byte took some archaeology:
- GNU counts U+00A0 (non-breaking space) as a word separator even in
the C locale, and the full Unicode white space set in `-m` mode —
@@ -82,19 +174,18 @@ combination) passes 100%.
because 64 KiB pipe chunks trigger a re-scan of its carried bytes.
We reproduced this, then declined to. fastwc counts the data, not
the plumbing.
- **Without `-m`, fastwc counts bytes with C-locale semantics.**
GNU silently switches to multibyte decoding for `-w` in UTF-8
locales. We don't — that's what `-m` is for, and it keeps the fast
path fast. Under `LC_ALL=C` we match GNU exactly.
## Reproducing
```sh
make release
make bench # build release + run every suite
./benchmarks/test-all.sh # words, lines (monsters included), stdin
```
The suites live in `benchmarks/files/{lines,words}` and
`benchmarks/stdin/piping`. The lines suite ends with the monsters:
100M lines raced against coreutils, and 1B lines timed solo (no
reference to beat — the reference is us now).
`test-all.sh` compiles its two helper tools on the spot
(`tools/genfile` for the test data, `tools/timeit` for microsecond
timing) — no separate setup. The suites live in
`benchmarks/files/{lines,words}` and `benchmarks/stdin/piping`, and
race all three oracles in one run. The lines suite ends with the
monsters: 100M lines raced against coreutils, and 1B lines timed solo
(no reference to beat — the reference is us now).
+274 -37
View File
@@ -78,6 +78,34 @@ static int posixly_correct = 0;
static enum total_mode total_mode = TOTAL_AUTO;
static const char *files_from = NULL; /* --files0-from=F, or NULL */
/*
* LC_CTYPE handling. A freshly exec'd process already runs in the C
* locale, so when the environment selects C/POSIX (or selects nothing)
* the setlocale() call is skipped: glibc would only re-derive the same
* state, at a cost measurable on the 1-line benchmark inputs. When the
* locale really is multibyte we adopt it exactly like GNU does.
*/
static int in_c_locale; /* LC_CTYPE resolves to C/POSIX, or is unset */
static int locale_done; /* setup_locale() has run */
static void setup_locale(void)
{
const char *v;
if (locale_done)
return;
locale_done = 1;
v = getenv("LC_ALL");
if (v == NULL || v[0] == '\0')
v = getenv("LC_CTYPE");
if (v == NULL || v[0] == '\0')
v = getenv("LANG");
in_c_locale = v == NULL || v[0] == '\0' || strcmp(v, "C") == 0 ||
strcmp(v, "POSIX") == 0;
if (!in_c_locale)
setlocale(LC_CTYPE, "");
}
/* gnulib quote() style: locale quotation marks in a UTF-8 locale,
* ASCII apostrophes elsewhere. Only argmatch diagnostics use it. */
static int curly_quotes;
@@ -141,6 +169,30 @@ static void init_isprint_tab(void)
isprint_tab[i] = isprint((unsigned char)i) ? 1 : 0;
}
/* The C/POSIX byte classes are fixed: isspace() is the six ASCII
* whitespace bytes and isprint() the range 0x20..0x7e. Precomputing
* them skips 512 ctype() calls per exec in the common C-locale run. */
static void init_ws_tab_c(void)
{
int i;
memset(ws_tab, 0, sizeof ws_tab);
for (i = '\t'; i <= '\r'; i++)
ws_tab[i] = 1;
ws_tab[' '] = 1;
ws_tab[0xa0] = (unsigned char)!posixly_correct;
nbsp_ws = ws_tab[0xa0];
}
static void init_isprint_tab_c(void)
{
int i;
memset(isprint_tab, 0, sizeof isprint_tab);
for (i = 0x20; i <= 0x7e; i++)
isprint_tab[i] = 1;
}
static void try_help(void)
{
fprintf(stderr, "Try 'fastwc --help' for more information.\n");
@@ -187,9 +239,19 @@ static void qfile(FILE *out, const char *s)
fputs(s, out);
}
/* quote(): always-quoting locale style used by argmatch diagnostics. */
/* quote(): always-quoting locale style used by argmatch diagnostics.
* The quote style is only needed on the --total error path, so the
* locale work stays out of every other invocation. */
static void qarg(FILE *out, const char *s)
{
static int qarg_ready;
if (!qarg_ready)
{
qarg_ready = 1;
setup_locale();
init_quote_style();
}
fputs(curly_quotes ? "\xe2\x80\x98" : "'", out);
fputs(s, out);
fputs(curly_quotes ? "\xe2\x80\x99" : "'", out);
@@ -322,7 +384,13 @@ typedef struct
} lw_t;
typedef lw_t (*count_lw_fn)(const unsigned char *s, size_t n, int *prev_ws,
int need_lines, int need_words);
int need_lines, int need_words, int need_high,
int *high);
/* need_high: probe the buffer for any byte >= 0x80. When one is seen the
* kernel sets *high and returns immediately with whatever partial counts
* it has; callers that asked for the probe treat the counts as invalid and
* re-run through the multibyte decoder. Pure-ASCII buffers never trigger,
* so the byte-path counts stay valid. */
/*
* Word separators match GNU wc (the benchmark oracle): the locale's
@@ -334,7 +402,7 @@ typedef lw_t (*count_lw_fn)(const unsigned char *s, size_t n, int *prev_ws,
__attribute__((target("avx512f,avx512bw"))) static lw_t
count_lw_avx512(const unsigned char *s, size_t n, int *prev_ws, int need_lines,
int need_words)
int need_words, int need_high, int *high)
{
const __m512i nl = _mm512_set1_epi8('\n');
const __m512i sp = _mm512_set1_epi8(' ');
@@ -350,6 +418,13 @@ count_lw_avx512(const unsigned char *s, size_t n, int *prev_ws, int need_lines,
__m512i v = _mm512_loadu_si512((const void *)(s + i));
uint64_t nl_mask = 0;
if (need_high && _mm512_movepi8_mask(v))
{
*high = 1;
lw_t r = {lines, words};
return r;
}
if (need_lines)
{
nl_mask = (uint64_t)_mm512_cmpeq_epi8_mask(v, nl);
@@ -373,6 +448,12 @@ count_lw_avx512(const unsigned char *s, size_t n, int *prev_ws, int need_lines,
for (; i < n; i++)
{
if (need_high && s[i] & 0x80)
{
*high = 1;
lw_t r = {lines, words};
return r;
}
int ws = ws_tab[s[i]];
if (need_lines)
lines += s[i] == '\n';
@@ -391,7 +472,7 @@ count_lw_avx512(const unsigned char *s, size_t n, int *prev_ws, int need_lines,
__attribute__((target("avx2"))) static lw_t
count_lw_avx2(const unsigned char *s, size_t n, int *prev_ws, int need_lines,
int need_words)
int need_words, int need_high, int *high)
{
const __m256i nl = _mm256_set1_epi8('\n');
const __m256i sp = _mm256_set1_epi8(' ');
@@ -407,6 +488,13 @@ count_lw_avx2(const unsigned char *s, size_t n, int *prev_ws, int need_lines,
__m256i v = _mm256_loadu_si256((const void *)(s + i));
uint32_t nl_mask = 0;
if (need_high && _mm256_movemask_epi8(v))
{
*high = 1;
lw_t r = {lines, words};
return r;
}
if (need_lines)
{
nl_mask = (uint32_t)_mm256_movemask_epi8(_mm256_cmpeq_epi8(v, nl));
@@ -432,6 +520,12 @@ count_lw_avx2(const unsigned char *s, size_t n, int *prev_ws, int need_lines,
for (; i < n; i++)
{
if (need_high && s[i] & 0x80)
{
*high = 1;
lw_t r = {lines, words};
return r;
}
int ws = ws_tab[s[i]];
if (need_lines)
lines += s[i] == '\n';
@@ -450,7 +544,7 @@ count_lw_avx2(const unsigned char *s, size_t n, int *prev_ws, int need_lines,
__attribute__((target("sse2"))) static lw_t
count_lw_sse2(const unsigned char *s, size_t n, int *prev_ws, int need_lines,
int need_words)
int need_words, int need_high, int *high)
{
const __m128i nl = _mm_set1_epi8('\n');
const __m128i sp = _mm_set1_epi8(' ');
@@ -466,6 +560,13 @@ count_lw_sse2(const unsigned char *s, size_t n, int *prev_ws, int need_lines,
__m128i v = _mm_loadu_si128((const void *)(s + i));
uint32_t nl_mask = 0;
if (need_high && _mm_movemask_epi8(v))
{
*high = 1;
lw_t r = {lines, words};
return r;
}
if (need_lines)
{
nl_mask = (uint32_t)_mm_movemask_epi8(_mm_cmpeq_epi8(v, nl));
@@ -488,6 +589,12 @@ count_lw_sse2(const unsigned char *s, size_t n, int *prev_ws, int need_lines,
for (; i < n; i++)
{
if (need_high && s[i] & 0x80)
{
*high = 1;
lw_t r = {lines, words};
return r;
}
int ws = ws_tab[s[i]];
if (need_lines)
lines += s[i] == '\n';
@@ -508,10 +615,22 @@ count_lw_sse2(const unsigned char *s, size_t n, int *prev_ws, int need_lines,
/* Reference path: the two scalar SWAR counters, kept as the fallback. */
static lw_t count_lw_scalar(const unsigned char *s, size_t n, int *prev_ws,
int need_lines, int need_words)
int need_lines, int need_words, int need_high,
int *high)
{
lw_t r;
if (need_high)
{
for (size_t i = 0; i < n; i++)
if (s[i] & 0x80)
{
*high = 1;
r.lines = 0;
r.words = 0;
return r;
}
}
r.lines = need_lines ? count_newlines(s, n) : 0;
r.words = need_words ? count_words(s, n, prev_ws) : 0;
return r;
@@ -519,7 +638,8 @@ static lw_t count_lw_scalar(const unsigned char *s, size_t n, int *prev_ws,
static count_lw_fn count_lw = count_lw_scalar; /* chosen by pick_kernel() */
static void count_mapped(const unsigned char *p, size_t n, counts_t *c);
static void count_mapped(const unsigned char *p, size_t n, counts_t *c,
int need_high, int *high);
#if defined(__x86_64__) || defined(__i386__)
static count_lw_fn pick_kernel(void)
@@ -540,6 +660,34 @@ static count_lw_fn pick_kernel(void)
}
#endif
/* One-time state the counting paths need, run after the options are
* known so --help/--version/usage errors pay none of it. The locale is
* only locale-sensitive for -m/-w/-L, the per-byte tables only for word
* and -L counting, and the SIMD kernel only for byte-path counts. */
static void init_count_state(void)
{
if (flags & (F_CHARS | F_WORDS | F_MAXLEN))
{
setup_locale();
if (flags & F_WORDS)
{
if (in_c_locale)
init_ws_tab_c();
else
init_ws_tab();
}
if (flags & F_MAXLEN)
{
if (in_c_locale)
init_isprint_tab_c();
else
init_isprint_tab();
}
}
if (flags & (F_LINES | F_WORDS))
count_lw = pick_kernel();
}
/*
* The Unicode white space set as GNU wc 9.11 sees it: glibc's iswspace()
* (which omits the no-break spaces U+00A0, U+2007, U+202F) plus those
@@ -811,12 +959,12 @@ static void count_stream(FILE *fp, counts_t *c)
int fd = fileno(fp);
/* Under a multibyte locale, -w/-m/-L need per-character decoding;
* -l and -c alone stay on the byte path, exactly like GNU wc. */
if ((MB_CUR_MAX > 1) && (flags & (F_CHARS | F_WORDS | F_MAXLEN)))
{
count_stream_mb(fp, c);
return;
}
* -l and -c alone stay on the byte path, exactly like GNU wc.
* mb_semantics is computed once and reused below: regular files are
* first probed on the SIMD byte path (see the has_high fallback),
* while non-regular inputs go straight to the decoder. */
int mb_semantics =
(MB_CUR_MAX > 1) && (flags & (F_CHARS | F_WORDS | F_MAXLEN));
if (debug && using_wc_lines())
debug_methods();
@@ -870,7 +1018,22 @@ static void count_stream(FILE *fp, counts_t *c)
mmap(NULL, (size_t)st.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
if (m != MAP_FAILED)
{
count_mapped((const unsigned char *)m, (size_t)st.st_size, c);
counts_t save = *c;
int high = 0;
count_mapped((const unsigned char *)m, (size_t)st.st_size, c,
mb_semantics, &high);
if (mb_semantics && high)
{
/* Non-ASCII input: the byte-path counts are invalid
* under a multibyte locale. Discard them and decode.
* The stream is still positioned at 0 (mmap never
* advanced it), so re-reading counts the whole file. */
*c = save;
munmap(m, (size_t)st.st_size);
count_stream_mb(fp, c);
}
else
munmap(m, (size_t)st.st_size);
if (ferror(fp))
c->ok = 0;
@@ -879,6 +1042,13 @@ static void count_stream(FILE *fp, counts_t *c)
}
}
/* Non-regular input: no mmap probe happened, so decode directly. */
if (mb_semantics)
{
count_stream_mb(fp, c);
return;
}
for (;;)
{
nread = fread(buf, 1, sizeof buf, fp); /* NOLINT: EOF-state FP */
@@ -890,7 +1060,7 @@ static void count_stream(FILE *fp, counts_t *c)
if (flags & (F_LINES | F_WORDS))
{
lw_t r = count_lw(buf, nread, &prev_ws, (flags & F_LINES) != 0,
(flags & F_WORDS) != 0);
(flags & F_WORDS) != 0, 0, NULL);
if (flags & F_LINES)
c->lines += r.lines;
if (flags & F_WORDS)
@@ -918,6 +1088,8 @@ typedef struct
int prev_ws;
int need_lines;
int need_words;
int need_high;
int high;
lw_t r;
} mjob_t;
@@ -925,7 +1097,8 @@ static void *map_worker(void *arg)
{
mjob_t *j = arg;
j->r = count_lw(j->s, j->n, &j->prev_ws, j->need_lines, j->need_words);
j->r = count_lw(j->s, j->n, &j->prev_ws, j->need_lines, j->need_words,
j->need_high, &j->high);
return NULL;
}
@@ -936,8 +1109,8 @@ static void *map_worker(void *arg)
* which makes the split exact. The kernels are pure, so no locks.
*/
static void count_sliced(const unsigned char *p, size_t n, int nt,
int need_lines, int need_words, long long *lines,
long long *words)
int need_lines, int need_words, int need_high,
int *high, long long *lines, long long *words)
{
mjob_t jobs[MAX_THREADS];
pthread_t th[MAX_THREADS];
@@ -961,6 +1134,8 @@ static void count_sliced(const unsigned char *p, size_t n, int nt,
jobs[i].prev_ws = (i == 0 || jobs[i].n == 0) ? 1 : ws_tab[p[start - 1]];
jobs[i].need_lines = need_lines;
jobs[i].need_words = need_words;
jobs[i].need_high = need_high;
jobs[i].high = 0;
th[i] = 0;
if (jobs[i].n > 0)
{
@@ -982,6 +1157,14 @@ static void count_sliced(const unsigned char *p, size_t n, int nt,
tw += jobs[i].r.words;
}
*high = 0;
for (i = 0; i < nt; i++)
if (jobs[i].high)
{
*high = 1;
break;
}
*lines = tl;
*words = tw;
}
@@ -1006,7 +1189,8 @@ static int pick_threads(size_t n)
return nt;
}
static void count_mapped(const unsigned char *p, size_t n, counts_t *c)
static void count_mapped(const unsigned char *p, size_t n, counts_t *c,
int need_high, int *high)
{
int need_lines = (flags & F_LINES) != 0;
int need_words = (flags & F_WORDS) != 0;
@@ -1020,14 +1204,20 @@ static void count_mapped(const unsigned char *p, size_t n, counts_t *c)
if (nt <= 1)
{
int prev_ws = 1;
lw_t r = count_lw(p, n, &prev_ws, need_lines, need_words);
lw_t r = count_lw(p, n, &prev_ws, need_lines, need_words, need_high,
high);
if (need_high && *high)
return; /* partial counts; caller re-decodes */
lines = r.lines;
words = r.words;
}
else
{
count_sliced(p, n, nt, need_lines, need_words, &lines, &words);
count_sliced(p, n, nt, need_lines, need_words, need_high, high,
&lines, &words);
if (need_high && *high)
return; /* partial counts; caller re-decodes */
}
if (flags & F_LINES)
@@ -1413,16 +1603,10 @@ int main(int argc, char **argv)
long n_args = 0;
total_t totals = {0, 0, 0, 0, 0};
setlocale(LC_CTYPE, "");
posixly_correct = getenv("POSIXLY_CORRECT") != NULL;
init_quote_style();
init_ws_tab();
init_isprint_tab();
count_lw = pick_kernel();
/* Line-buffer stdout like GNU, so parallel processes do not
* interleave their rows. */
setvbuf(stdout, NULL, _IOLBF, 0);
/* Option parsing needs no locale, so --help/--version and usage
* errors return before any of the one-time startup state exists. */
while ((optc = getopt_long(argc, argv, "clLmw", longopts, NULL)) != -1)
switch (optc)
{
@@ -1469,6 +1653,11 @@ int main(int argc, char **argv)
if (flags == 0)
flags = F_LINES | F_WORDS | F_BYTES; /* wc default: -l -w -c */
/* Line-buffer stdout like GNU, so parallel processes do not
* interleave their rows. */
setvbuf(stdout, NULL, _IOLBF, 0);
init_count_state();
if (files_from)
{
FILE *stream;
@@ -1697,7 +1886,8 @@ static long long ref_words(const unsigned char *s, size_t n, int *prev_ws)
* the reference instead of only on real AVX-512 hardware.
*/
static lw_t count_lw_avx512_mirror(const unsigned char *s, size_t n,
int *prev_ws, int need_lines, int need_words)
int *prev_ws, int need_lines, int need_words,
int need_high, int *high)
{
long long lines = 0, words = 0;
size_t i = 0;
@@ -1711,6 +1901,12 @@ static lw_t count_lw_avx512_mirror(const unsigned char *s, size_t n,
unsigned char c = s[i + j];
uint64_t bit = (uint64_t)1 << j;
if (need_high && (c & 0x80))
{
*high = 1;
lw_t r = {lines, words};
return r;
}
if (c == '\n')
nl_mask |= bit;
if (c == '\n' || c == ' ' || (nbsp_ws && c == 0xa0) ||
@@ -1729,6 +1925,12 @@ static lw_t count_lw_avx512_mirror(const unsigned char *s, size_t n,
for (; i < n; i++)
{
int ws = ws_tab[s[i]];
if (need_high && (s[i] & 0x80))
{
*high = 1;
lw_t r = {lines, words};
return r;
}
if (need_lines)
lines += s[i] == '\n';
if (need_words)
@@ -1761,7 +1963,7 @@ static int check_kernel(const char *name, count_lw_fn fn)
for (int nw = 0; nw <= 1; nw++)
{
int a = pw, b = pw;
lw_t got = fn(buf, n, &a, nl, nw);
lw_t got = fn(buf, n, &a, nl, nw, 0, NULL);
long long want_l = nl ? ref_lines(buf, n) : 0;
long long want_w = nw ? ref_words(buf, n, &b) : 0;
@@ -1795,7 +1997,7 @@ static int check_kernel(const char *name, count_lw_fn fn)
for (int pw = 0; pw <= 1; pw++)
{
int a = pw, b = pw;
lw_t got = fn(buf, n, &a, 1, 1);
lw_t got = fn(buf, n, &a, 1, 1, 0, NULL);
if (got.lines != ref_lines(buf, n) ||
got.words != ref_words(buf, n, &b) || a != b)
@@ -1818,7 +2020,7 @@ static int check_kernel(const char *name, count_lw_fn fn)
for (k = 0; k < 256; k++)
buf[k] = pass == 0 ? (unsigned char)k : (unsigned char)(255 - k);
int a = 1, b = 1;
lw_t got = fn(buf, 256, &a, 1, 1);
lw_t got = fn(buf, 256, &a, 1, 1, 0, NULL);
if (got.lines != ref_lines(buf, 256) ||
got.words != ref_words(buf, 256, &b) || a != b)
@@ -1828,6 +2030,35 @@ static int check_kernel(const char *name, count_lw_fn fn)
}
}
/* high-byte probe contract: *high set iff some byte >= 0x80 exists.
* Partial counts on a probe trip are undefined, so only the flag is
* checked here (the caller re-runs through the mb decoder). */
for (size_t probe = 0; probe < 2; probe++)
{
int high = 0;
memset(buf, 'x', 256);
buf[probe == 0 ? 0 : 255] = probe == 0 ? 0x80 : 0xff;
int a = 1;
fn(buf, 256, &a, 1, 1, 1, &high);
if (!high)
{
printf("%s: high probe pos=%zu not detected\n", name, probe);
fails++;
}
memset(buf, 'x', 256);
buf[255] = '\n';
high = 0;
a = 1;
fn(buf, 256, &a, 1, 1, 1, &high);
if (high)
{
printf("%s: ascii probe false-trip\n", name);
fails++;
}
}
printf("%s: %s\n", name, fails ? "FAIL" : "ok");
return fails;
}
@@ -1853,12 +2084,13 @@ static int check_sliced(void)
{
int nt = tcs[ti];
long long tl = 0, tw = 0;
int dh = 0;
long long want_l = count_newlines(buf, n);
int pw = 1;
long long want_w = count_words(buf, n, &pw);
count_sliced(buf, n, nt, 1, 1, &tl, &tw);
if (tl != want_l || tw != want_w)
count_sliced(buf, n, nt, 1, 1, 0, &dh, &tl, &tw);
if (tl != want_l || tw != want_w || dh != 0)
{
printf("sliced: n=%zu nt=%d lines %lld/%lld "
"words %lld/%lld\n",
@@ -1879,12 +2111,13 @@ static int check_sliced(void)
{
int nt = tcs[ti];
long long tl = 0, tw = 0;
int dh = 0;
long long want_l = count_newlines(buf, n);
int pw = 1;
long long want_w = count_words(buf, n, &pw);
count_sliced(buf, n, nt, 1, 1, &tl, &tw);
if (tl != want_l || tw != want_w)
count_sliced(buf, n, nt, 1, 1, 0, &dh, &tl, &tw);
if (tl != want_l || tw != want_w || dh != 0)
{
printf("sliced-ws: n=%zu nt=%d lines %lld/%lld "
"words %lld/%lld\n",
@@ -1908,7 +2141,11 @@ int main(void)
/* The CLI build's helpers are compiled out of reach here. */
(void)usage;
(void)try_help;
(void)setup_locale;
(void)init_isprint_tab;
(void)init_isprint_tab_c;
(void)init_ws_tab_c;
(void)init_count_state;
(void)init_quote_style;
(void)qaf;
(void)qfile;