Compare commits
10
Commits
f217551448
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
f8361c31a3
|
||
|
|
e52e97a825
|
||
|
|
e5c6c8d3f0
|
||
|
|
0bb53e9c15
|
||
|
|
3cde946a68
|
||
|
|
7b3a535173
|
||
|
|
7a8b763416 | ||
|
|
7ab7efc7b9 | ||
|
|
55aab093e7 | ||
|
|
af61660c42 |
@@ -54,6 +54,7 @@ dkms.conf
|
|||||||
|
|
||||||
# ---> fastwc benchmarks
|
# ---> fastwc benchmarks
|
||||||
benchmarks/tools/genfile
|
benchmarks/tools/genfile
|
||||||
|
benchmarks/tools/timeit
|
||||||
benchmarks/.data/
|
benchmarks/.data/
|
||||||
benchmarks/files/*/.data/
|
benchmarks/files/*/.data/
|
||||||
benchmarks/stdin/piping/.data/
|
benchmarks/stdin/piping/.data/
|
||||||
|
|||||||
+22
-15
@@ -10,18 +10,25 @@ These aren't guidelines. They're the deal.
|
|||||||
### 1. The benchmark is the contract
|
### 1. The benchmark is the contract
|
||||||
|
|
||||||
`./benchmarks/test-all.sh` must pass. Not "mostly pass." Not "pass on
|
`./benchmarks/test-all.sh` must pass. Not "mostly pass." Not "pass on
|
||||||
your machine." Pass. Every suite — words, lines (monsters included),
|
your machine." Pass. Every suite — words (growing to 10M lines), lines
|
||||||
and stdin — races fastwc against GNU wc.
|
(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
|
The moment fastwc is slower than any of them on a real race — or
|
||||||
single count — your change does not ship. We did not spend this much
|
disagrees with any of them on a single count — your change does not
|
||||||
effort being faster than a forty-year-old dependency museum just so you
|
ship. We did not spend this much effort being faster than a
|
||||||
could add a `strlen()` in the hot loop.
|
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
|
The benchmark is honest about what it measures. Both sides are timed
|
||||||
AVX-512 assembly squeaked out a one-millisecond win — is closed. We
|
by a C timer, and a case the reference finishes in under 5 ms is
|
||||||
beat them there too now (see docs/PERFORMANCE.md). There are no
|
startup, not throughput: fastwc must still count it right, but the
|
||||||
exceptions left, and there is no excuse to be slower anywhere else.
|
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
|
### 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
|
### 7. Tests are the benchmarks
|
||||||
|
|
||||||
There is no separate test suite, because the benchmark *is* the test
|
There is no separate test suite, because the benchmark *is* the test
|
||||||
suite — it checks correctness against GNU wc (the only oracle around,
|
suite — it checks correctness against GNU coreutils wc, busybox wc,
|
||||||
and it's wrong often enough to keep us humble) and speed in the same
|
and toybox wc (all three are wrong often enough to keep us humble) and
|
||||||
breath. Add a flag? It gets benchmarked. Change the counting? It gets
|
speed in the same breath. Add a flag? It gets benchmarked. Change the
|
||||||
benchmarked. The fail-fast scripts in `benchmarks/` are your tests, and
|
counting? It gets benchmarked. The fail-fast scripts in `benchmarks/`
|
||||||
all of them must pass before you open a PR.
|
are your tests, and all of them must pass before you open a PR.
|
||||||
|
|
||||||
### 8. No AI slop
|
### 8. No AI slop
|
||||||
|
|
||||||
|
|||||||
@@ -3,10 +3,16 @@ AM_CFLAGS = -Wall -Wextra -O2 -pthread
|
|||||||
bin_PROGRAMS = fastwc
|
bin_PROGRAMS = fastwc
|
||||||
fastwc_SOURCES = src/main.c
|
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).
|
# Release build consumed by benchmarks/ (expects bin/release/fastwc).
|
||||||
|
# Strip the copy, not the tree binary: the debug build stays debuggable.
|
||||||
release: all
|
release: all
|
||||||
$(MKDIR_P) bin/release
|
$(MKDIR_P) bin/release
|
||||||
cp -f fastwc bin/release/fastwc
|
cp -f fastwc bin/release/fastwc
|
||||||
|
$(STRIP) bin/release/fastwc
|
||||||
|
|
||||||
# Convenience: build the release binary, then run every benchmark suite.
|
# Convenience: build the release binary, then run every benchmark suite.
|
||||||
bench: release
|
bench: release
|
||||||
|
|||||||
@@ -17,32 +17,84 @@ correctly, at full speed.
|
|||||||
|
|
||||||
## The scoreboard
|
## The scoreboard
|
||||||
|
|
||||||
The benchmark suite in `benchmarks/` races fastwc against GNU `wc` —
|
The benchmark suite in `benchmarks/` races fastwc against GNU `wc`,
|
||||||
fail-fast. The moment we are slower, or disagree on a single count, it
|
busybox `wc`, and toybox `wc` — fail-fast. The moment we are slower, or
|
||||||
writes a shame report and exits non-zero. These are the facts:
|
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 |
|
| Suite | Result |
|
||||||
|-------|--------|
|
|-------|--------|
|
||||||
| words (6 cases) | **6/6 wins.** Never slower, never wrong. |
|
| 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. |
|
||||||
| lines (up to 100k lines) | **Wins.** GNU never sees us coming. |
|
| 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). |
|
||||||
| lines (1M lines) | **Win: 1ms vs 2ms.** GNU's AVX-512 assist can't beat a mapped file. |
|
| 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 (10M lines) | **Win: 8-9ms vs 22-24ms (~2.5x).** GNU's lead never survives contact with the buffer. |
|
| 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 (100M lines) | **Win: ~70ms vs ~140ms.** The monster race. GNU gets lapped. |
|
| 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 (1B lines) | **Solo, ~4-6s.** 11 GB in one pass; the only bottleneck left is the disk. |
|
| lines, 100M monster | **29.2 ms vs coreutils 47.5 ms (1.6x).** The bigger the file, the wider the gap. |
|
||||||
| stdin words (1M lines) | **Win: 12x.** GNU still reads stdin like it's 1985. |
|
| 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 lines) | **Win: ~2.5x.** We map stdin redirects; GNU maps nothing. |
|
| 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
|
Measured on an AMD Ryzen AI 7 PRO 350 — 8 cores / 16 threads, boost up
|
||||||
you should say so loudly in an issue. The benchmark is the contract.
|
to 5.09 GHz, 384 KiB L1d / 256 KiB L1i / 8 MiB L2 / 16 MiB L3 — with
|
||||||
The how and why of the speed, with receipts, lives in
|
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).
|
[docs/PERFORMANCE.md](docs/PERFORMANCE.md).
|
||||||
|
|
||||||
GNU wc's lone win — 1M lines, by a single millisecond, on hand-tuned
|
The moment fastwc is slower than any of them, this project has failed
|
||||||
AVX-512 assembly — is a historical footnote now. We closed it, then the
|
and you should say so loudly in an issue. The benchmark is the
|
||||||
10M case, then the 100M monster, and then we added a 1B-line race with
|
contract. The how and why of the speed, with receipts, lives in
|
||||||
no opponent, because beating GNU wc at this point is just cruelty to
|
[docs/PERFORMANCE.md](docs/PERFORMANCE.md).
|
||||||
software. Busybox was removed from the suite for the same reason: it
|
|
||||||
stopped being a challenge and started being a participation trophy.
|
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
|
## Why
|
||||||
|
|
||||||
@@ -56,15 +108,32 @@ stopped being a challenge and started being a participation trophy.
|
|||||||
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.
|
||||||
|
- **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
|
## What it does
|
||||||
|
|
||||||
```
|
```
|
||||||
fastwc [-lwc] [-m] [file...]
|
fastwc [OPTION]... [FILE]...
|
||||||
|
fastwc [OPTION]... --files0-from=F
|
||||||
```
|
```
|
||||||
|
|
||||||
- `-l` lines, `-w` words, `-c` bytes, `-m` characters (multibyte)
|
- `-l` lines, `-w` words, `-c` bytes, `-m` characters (multibyte),
|
||||||
|
`-L` maximum line length (display width, tab stops every 8)
|
||||||
|
- `--files0-from=F` NUL-terminated file lists from F (or stdin when F is `-`)
|
||||||
|
- `--total=auto|always|only|never` control over the total row
|
||||||
|
- `--debug` report which line-counting acceleration is in use
|
||||||
- stdin, `-`, multiple files, `total` rows, GNU-compatible counts
|
- stdin, `-`, multiple files, `total` rows, GNU-compatible counts
|
||||||
|
- A CLI surface modeled on GNU coreutils `wc` 9.11: same options, same
|
||||||
|
column alignment rule (counts are right-justified to the widest sum
|
||||||
|
of regular file sizes), same multibyte word semantics, same error
|
||||||
|
text. Swap it in and scripts keep working.
|
||||||
|
- POSIXLY_CORRECT is honored: the no-break space stops being a word
|
||||||
|
separator, exactly like GNU wc.
|
||||||
- no `--help` in fourteen languages. One `--help`, in English, the
|
- no `--help` in fourteen languages. One `--help`, in English, the
|
||||||
language of people who ship software
|
language of people who ship software
|
||||||
|
|
||||||
@@ -73,10 +142,19 @@ fastwc [-lwc] [-m] [file...]
|
|||||||
Requires a C compiler and autotools. That's it. No gettext. No gnulib.
|
Requires a C compiler and autotools. That's it. No gettext. No gnulib.
|
||||||
No translators.
|
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
|
```sh
|
||||||
./autogen.sh # autoreconf -fi && ./configure
|
./autogen.sh # autoreconf -fi && ./configure (static musl)
|
||||||
make
|
make
|
||||||
make release # installs the release binary to bin/release/fastwc
|
make release # installs the release binary to bin/release/fastwc
|
||||||
|
|
||||||
|
./configure --enable-glibc && make # or: dynamic glibc build
|
||||||
```
|
```
|
||||||
|
|
||||||
## Benchmark
|
## Benchmark
|
||||||
@@ -87,14 +165,26 @@ make bench # build release + run every suite
|
|||||||
```
|
```
|
||||||
|
|
||||||
The suites live under `benchmarks/files/{lines,words}` (file input) and
|
The suites live under `benchmarks/files/{lines,words}` (file input) and
|
||||||
`benchmarks/stdin/piping` (standard input), and fail fast the moment
|
`benchmarks/stdin/piping` (standard input). Every suite races fastwc
|
||||||
fastwc loses a single case. The lines suite also carries the monsters:
|
against GNU coreutils wc and, when it is installed, busybox wc and
|
||||||
100M lines raced against coreutils, and 1B lines timed solo — there is
|
toybox wc. The benchmark locates the real oracles by their `--version`
|
||||||
no reference for that one; we are the reference now. The suites
|
answer — a `wc` that answers as fastwc is a symlink somebody made, and
|
||||||
interleave runs so both commands see identical cache warmth, keep the
|
is skipped rather than raced against itself.
|
||||||
minimum, and fail the moment fastwc loses a single case. GNU `wc` is
|
Both sides are timed at microsecond resolution by `tools/timeit`, a C
|
||||||
used as an oracle the same way you'd use a broken clock: occasionally
|
timer built on the spot (`test-all.sh` compiles it next to `genfile`)
|
||||||
it's right, and it's the only one around.
|
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
|
## Development
|
||||||
|
|
||||||
|
|||||||
@@ -1,10 +1,12 @@
|
|||||||
#!/usr/bin/env bash
|
#!/usr/bin/env bash
|
||||||
# bench.sh — race fastwc against GNU coreutils wc on line counts, file input.
|
# bench.sh — race fastwc against GNU coreutils wc on line counts, file input.
|
||||||
# Includes the monster cases: 100M lines raced against coreutils, and 1B
|
# Busybox and toybox wc are raced too, for shits and giggles. Includes
|
||||||
# lines timed solo (no reference to beat — there isn't one).
|
# the monster cases: 100M lines raced against coreutils (busybox/toybox
|
||||||
# Fails fast: the moment fastwc is slower than (or disagrees with) coreutils
|
# through 1.1 GB is a spectator sport, not a benchmark), and 1B lines
|
||||||
# wc, a human readable report is written to FAILED-benchmark.txt and this
|
# timed solo (no reference to beat — there isn't one).
|
||||||
# script exits non-zero.
|
# 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.
|
||||||
#
|
#
|
||||||
# usage: ./bench.sh
|
# usage: ./bench.sh
|
||||||
set -u
|
set -u
|
||||||
@@ -15,17 +17,27 @@ source "$REPO_DIR/benchmarks/std.sh"
|
|||||||
|
|
||||||
checkfastwc
|
checkfastwc
|
||||||
checkwc
|
checkwc
|
||||||
BENCH_NAME="coreutils"
|
|
||||||
|
|
||||||
printf 'benchmarking %s wc vs fastwc: lines, file input (%s interleaved runs each, minimum kept)\n' \
|
for oracle in $ORACLES; do
|
||||||
|
select_oracle "$oracle" || exit 1
|
||||||
|
|
||||||
|
printf 'benchmarking %s wc vs fastwc: lines, file input (%s interleaved runs each, minimum kept)\n' \
|
||||||
"$BENCH_NAME" "$BENCH_REPS"
|
"$BENCH_NAME" "$BENCH_REPS"
|
||||||
printf '%-28s %10s %10s %8s %s\n' 'test' 'wc' 'fastwc' 'ratio' 'status'
|
printf '%-28s %16s %24s %8s %s\n' 'test' 'wc' 'fastwc (µs)' 'ratio' 'status'
|
||||||
|
|
||||||
printf '%s\n' '--- lines ---'
|
printf '%s\n' '--- lines ---'
|
||||||
if ! run_cases lines -l 10000 100000 1000000 10000000; then
|
if ! run_cases lines -l 10000 100000 1000000 10000000; then
|
||||||
exit 1
|
exit 1
|
||||||
fi
|
fi
|
||||||
|
|
||||||
|
printf '\nall %s lines benchmarks passed — fastwc was never slower than %s wc\n' \
|
||||||
|
"$BENCH_NAME" "$BENCH_NAME"
|
||||||
|
done
|
||||||
|
|
||||||
|
# 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 ---'
|
printf '%s\n' '--- monster: 100M lines vs coreutils ---'
|
||||||
if ! run_cases lines -l 100000000; then
|
if ! run_cases lines -l 100000000; then
|
||||||
exit 1
|
exit 1
|
||||||
@@ -33,6 +45,3 @@ fi
|
|||||||
|
|
||||||
printf '%s\n' '--- monster: 1B lines, solo ---'
|
printf '%s\n' '--- monster: 1B lines, solo ---'
|
||||||
run_solo_case 1000000000 -l
|
run_solo_case 1000000000 -l
|
||||||
|
|
||||||
printf '\nall %s lines benchmarks passed — fastwc was never slower than %s wc\n' \
|
|
||||||
"$BENCH_NAME" "$BENCH_NAME"
|
|
||||||
|
|||||||
@@ -1,8 +1,9 @@
|
|||||||
#!/usr/bin/env bash
|
#!/usr/bin/env bash
|
||||||
# bench.sh — race fastwc against GNU coreutils wc on word counts, file input.
|
# bench.sh — race fastwc against GNU coreutils wc on word counts, file input.
|
||||||
# Fails fast: the moment fastwc is slower than (or disagrees with) coreutils
|
# Busybox and toybox wc are raced too, for shits and giggles.
|
||||||
# wc, a human readable report is written to FAILED-benchmark.txt and this
|
# Fails fast: the moment fastwc is slower than (or disagrees with) an
|
||||||
# script exits non-zero.
|
# oracle wc, a human readable report is written to FAILED-benchmark.txt
|
||||||
|
# and this script exits non-zero.
|
||||||
#
|
#
|
||||||
# usage: ./bench.sh
|
# usage: ./bench.sh
|
||||||
set -u
|
set -u
|
||||||
@@ -13,16 +14,19 @@ source "$REPO_DIR/benchmarks/std.sh"
|
|||||||
|
|
||||||
checkfastwc
|
checkfastwc
|
||||||
checkwc
|
checkwc
|
||||||
BENCH_NAME="coreutils"
|
|
||||||
|
|
||||||
printf 'benchmarking %s wc vs fastwc: words, file input (%s interleaved runs each, minimum kept)\n' \
|
for oracle in $ORACLES; do
|
||||||
|
select_oracle "$oracle" || exit 1
|
||||||
|
|
||||||
|
printf 'benchmarking %s wc vs fastwc: words, file input (%s interleaved runs each, minimum kept)\n' \
|
||||||
"$BENCH_NAME" "$BENCH_REPS"
|
"$BENCH_NAME" "$BENCH_REPS"
|
||||||
printf '%-28s %10s %10s %8s %s\n' 'test' 'wc' 'fastwc' 'ratio' 'status'
|
printf '%-28s %16s %24s %8s %s\n' 'test' 'wc' 'fastwc (µs)' 'ratio' 'status'
|
||||||
|
|
||||||
printf '%s\n' '--- words ---'
|
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
|
exit 1
|
||||||
fi
|
fi
|
||||||
|
|
||||||
printf '\nall %s words benchmarks passed — fastwc was never slower than %s wc\n' \
|
printf '\nall %s words benchmarks passed — fastwc was never slower than %s wc\n' \
|
||||||
"$BENCH_NAME" "$BENCH_NAME"
|
"$BENCH_NAME" "$BENCH_NAME"
|
||||||
|
done
|
||||||
|
|||||||
+216
-107
@@ -12,7 +12,7 @@
|
|||||||
# checkfastwc() verify a release build of fastwc exists
|
# checkfastwc() verify a release build of fastwc exists
|
||||||
# checkwc() locate the coreutils wc implementation
|
# checkwc() locate the coreutils wc implementation
|
||||||
# createtxt() create (or reuse) a text file with N such lines
|
# 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
|
# capture_count() print the first whitespace-separated field of output
|
||||||
# run_case() race fastwc against the reference on a file argument
|
# run_case() race fastwc against the reference on a file argument
|
||||||
# run_stdin_case() same, but feeding the file through standard input
|
# run_stdin_case() same, but feeding the file through standard input
|
||||||
@@ -36,12 +36,23 @@ export LC_CTYPE=C
|
|||||||
FASTWC="$REPO_DIR/bin/release/fastwc"
|
FASTWC="$REPO_DIR/bin/release/fastwc"
|
||||||
DATA_DIR="$SCRIPT_DIR/.data"
|
DATA_DIR="$SCRIPT_DIR/.data"
|
||||||
GENFILE="$REPO_DIR/benchmarks/tools/genfile" # optional C helper, built by test-all.sh
|
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_NAME="${BENCH_NAME:-wc}" # set by the caller: coreutils
|
||||||
BENCH_REPS="${BENCH_REPS:-3}" # interleaved runs per case; minimum is kept
|
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
|
RESULT_ROWS="" # accumulated results table
|
||||||
|
|
||||||
WC_CMD=() # filled by checkwc()
|
# Per-case speedup ratios accumulate here so the run can end with the
|
||||||
|
# average speedup per oracle. test-all.sh overrides this with a shared
|
||||||
|
# temp file so every suite feeds the same average; a standalone suite
|
||||||
|
# run gets its own file ($$ differs per process).
|
||||||
|
: "${RATIOS_FILE:=/tmp/fastwc-ratios-$$.tsv}"
|
||||||
|
|
||||||
|
WC_CMD=() # filled by checkwc/select_oracle
|
||||||
TEXT_FILE="" # filled by createtxt()
|
TEXT_FILE="" # filled by createtxt()
|
||||||
|
|
||||||
if [[ -z "$BENCH_REPS" || "$BENCH_REPS" -lt 1 ]]; then
|
if [[ -z "$BENCH_REPS" || "$BENCH_REPS" -lt 1 ]]; then
|
||||||
@@ -61,19 +72,109 @@ checkfastwc() {
|
|||||||
exit 1
|
exit 1
|
||||||
}
|
}
|
||||||
|
|
||||||
# checkwc — locate the coreutils wc implementation and store its invocation
|
# wc_impl <cmd...> — identify a wc implementation from its --version
|
||||||
# in $WC_CMD. Exits on failure.
|
# 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
|
||||||
|
}
|
||||||
|
|
||||||
|
# checkwc — locate the wc implementations to race against. Walks PATH
|
||||||
|
# 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 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() {
|
checkwc() {
|
||||||
printf 'checking for coreutils wc... '
|
local dir impl
|
||||||
if command -v wc >/dev/null 2>&1 \
|
COREUTILS_CMD=()
|
||||||
&& wc --version 2>/dev/null | head -n1 | grep -qi 'GNU coreutils'; then
|
BUSYBOX_CMD=()
|
||||||
printf 'yes\n'
|
TOYBOX_CMD=()
|
||||||
WC_CMD=(wc)
|
ORACLES=''
|
||||||
else
|
|
||||||
printf 'no\n'
|
printf 'locating wc implementations... '
|
||||||
printf 'configure: error: GNU Coreutils wc not found in PATH\n' >&2
|
for dir in ${PATH//:/ }; do
|
||||||
|
[[ -n "$dir" && -x "$dir/wc" ]] || continue
|
||||||
|
impl=$(wc_impl "$dir/wc")
|
||||||
|
case "$impl" in
|
||||||
|
coreutils)
|
||||||
|
if [[ ${#COREUTILS_CMD[@]} -eq 0 ]]; then
|
||||||
|
COREUTILS_CMD=("$dir/wc")
|
||||||
|
printf 'coreutils %s; ' \
|
||||||
|
"$("$dir/wc" --version | head -n1 | sed 's/^wc (GNU coreutils) //')"
|
||||||
|
fi
|
||||||
|
;;
|
||||||
|
fastwc)
|
||||||
|
printf 'warning: %s is a fastwc symlink; skipping as oracle\n' \
|
||||||
|
"$dir/wc" >&2
|
||||||
|
;;
|
||||||
|
busybox)
|
||||||
|
if [[ ${#BUSYBOX_CMD[@]} -eq 0 ]]; then
|
||||||
|
BUSYBOX_CMD=("$dir/wc")
|
||||||
|
printf 'busybox; '
|
||||||
|
fi
|
||||||
|
;;
|
||||||
|
toybox)
|
||||||
|
if [[ ${#TOYBOX_CMD[@]} -eq 0 ]]; then
|
||||||
|
TOYBOX_CMD=("$dir/wc")
|
||||||
|
printf 'toybox; '
|
||||||
|
fi
|
||||||
|
;;
|
||||||
|
*) ;;
|
||||||
|
esac
|
||||||
|
done
|
||||||
|
|
||||||
|
# busybox is commonly installed as the multi-call binary only
|
||||||
|
if [[ ${#BUSYBOX_CMD[@]} -eq 0 ]] && command -v busybox >/dev/null 2>&1 \
|
||||||
|
&& [[ "$(wc_impl busybox wc)" == 'busybox' ]]; then
|
||||||
|
BUSYBOX_CMD=(busybox wc)
|
||||||
|
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
|
||||||
|
printf 'configure: error: if you symlinked wc to fastwc, point PATH at a real coreutils first\n' >&2
|
||||||
exit 1
|
exit 1
|
||||||
fi
|
fi
|
||||||
|
printf '\n'
|
||||||
|
|
||||||
|
WC_CMD=("${COREUTILS_CMD[@]}")
|
||||||
|
ORACLES='coreutils'
|
||||||
|
[[ ${#BUSYBOX_CMD[@]} -gt 0 ]] && ORACLES="$ORACLES busybox"
|
||||||
|
[[ ${#TOYBOX_CMD[@]} -gt 0 ]] && ORACLES="$ORACLES toybox"
|
||||||
|
}
|
||||||
|
|
||||||
|
# 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
|
||||||
}
|
}
|
||||||
|
|
||||||
# createrandstr — print one random 10-character alphanumeric string.
|
# createrandstr — print one random 10-character alphanumeric string.
|
||||||
@@ -122,13 +223,17 @@ createtxt() {
|
|||||||
printf '%s\n' "$TEXT_FILE"
|
printf '%s\n' "$TEXT_FILE"
|
||||||
}
|
}
|
||||||
|
|
||||||
# time_ms <cmd...> — run a command once and print elapsed wall time in ms.
|
# timeit <cmd...> — run a command once and print elapsed wall time in µs
|
||||||
time_ms() {
|
# on stdout, with the command's own output discarded. The clock starts in
|
||||||
local s e
|
# main(), after timeit's own loader has run, so every binary pays the same
|
||||||
s=$(date +%s%N)
|
# bookkeeping and the reading is the child's fork + exec + run + exit.
|
||||||
"$@" >/dev/null 2>&1
|
timeit() {
|
||||||
e=$(date +%s%N)
|
[[ -x "$TIMEIT" ]] || {
|
||||||
printf '%s\n' "$(( (e - s) / 1000000 ))"
|
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
|
# capture_count <cmd...> — print the first whitespace-separated field of a
|
||||||
@@ -137,10 +242,10 @@ capture_count() {
|
|||||||
"$@" 2>/dev/null | awk 'NR == 1 { print $1 }'
|
"$@" 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.
|
# human readable failure report to FAILED-benchmark.txt next to the suite.
|
||||||
write_failed_report() {
|
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"
|
local report="$SCRIPT_DIR/FAILED-benchmark.txt"
|
||||||
|
|
||||||
{
|
{
|
||||||
@@ -151,7 +256,7 @@ write_failed_report() {
|
|||||||
printf 'failure : %s\n' "$reason"
|
printf 'failure : %s\n' "$reason"
|
||||||
printf '\nresults\n'
|
printf '\nresults\n'
|
||||||
printf '%s\n' '-------'
|
printf '%s\n' '-------'
|
||||||
printf '%-28s %10s %10s %8s %s\n' 'test' 'wc' 'fastwc' 'ratio' 'status'
|
printf '%-28s %16s %24s %8s %s\n' 'test' 'wc' 'fastwc (µs)' 'ratio' 'status'
|
||||||
printf '%s' "$RESULT_ROWS"
|
printf '%s' "$RESULT_ROWS"
|
||||||
printf '\nfastwc must never be slower than %s wc — benchmark aborted.\n' "$BENCH_NAME"
|
printf '\nfastwc must never be slower than %s wc — benchmark aborted.\n' "$BENCH_NAME"
|
||||||
} > "$report"
|
} > "$report"
|
||||||
@@ -160,6 +265,45 @@ write_failed_report() {
|
|||||||
printf 'full results written to %s\n' "$report" >&2
|
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
|
# 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
|
# fastwc on a file argument. Fails the benchmark the moment fastwc is
|
||||||
# slower or reports a different count.
|
# slower or reports a different count.
|
||||||
@@ -167,7 +311,7 @@ run_case() {
|
|||||||
local mode="$1" lines="$2" flag="$3"
|
local mode="$1" lines="$2" flag="$3"
|
||||||
local noun='lines'; [[ "$lines" -eq 1 ]] && noun='line'
|
local noun='lines'; [[ "$lines" -eq 1 ]] && noun='line'
|
||||||
local label="${mode} (${lines} ${noun})"
|
local label="${mode} (${lines} ${noun})"
|
||||||
local file wc_count fast_count wc_ms fast_ms ratio verdict reason row
|
local file wc_count fast_count wc_us fast_us
|
||||||
local i d
|
local i d
|
||||||
|
|
||||||
file=$(createtxt "$lines") || return 1
|
file=$(createtxt "$lines") || return 1
|
||||||
@@ -176,49 +320,19 @@ run_case() {
|
|||||||
wc_count=$(capture_count "${WC_CMD[@]}" "$flag" "$file")
|
wc_count=$(capture_count "${WC_CMD[@]}" "$flag" "$file")
|
||||||
fast_count=$(capture_count "$FASTWC" "$flag" "$file")
|
fast_count=$(capture_count "$FASTWC" "$flag" "$file")
|
||||||
|
|
||||||
# speed: interleaved timing so both commands see identical cache warmth;
|
# speed: interleaved timing so both commands see identical cache
|
||||||
# keep the minimum of $BENCH_REPS runs each to reduce noise
|
# warmth; keep the minimum of $BENCH_REPS runs each
|
||||||
wc_ms=''
|
wc_us=''
|
||||||
fast_ms=''
|
fast_us=''
|
||||||
for ((i = 0; i < BENCH_REPS; i++)); do
|
for ((i = 0; i < BENCH_REPS; i++)); do
|
||||||
d=$(time_ms "${WC_CMD[@]}" "$flag" "$file")
|
d=$(timeit "${WC_CMD[@]}" "$flag" "$file")
|
||||||
[[ -z "$wc_ms" || "$d" -lt "$wc_ms" ]] && wc_ms="$d"
|
[[ -z "$wc_us" || "$d" -lt "$wc_us" ]] && wc_us="$d"
|
||||||
|
|
||||||
d=$(time_ms "$FASTWC" "$flag" "$file")
|
d=$(timeit "$FASTWC" "$flag" "$file")
|
||||||
[[ -z "$fast_ms" || "$d" -lt "$fast_ms" ]] && fast_ms="$d"
|
[[ -z "$fast_us" || "$d" -lt "$fast_us" ]] && fast_us="$d"
|
||||||
done
|
done
|
||||||
|
|
||||||
# ratio: how many times faster fastwc is than the reference (wc / fastwc)
|
finish_race "$label" "$wc_count" "$fast_count" "$wc_us" "$fast_us"
|
||||||
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
|
|
||||||
|
|
||||||
verdict='PASS'
|
|
||||||
reason=''
|
|
||||||
if [[ "$fast_count" != "$wc_count" ]]; then
|
|
||||||
verdict='FAIL'
|
|
||||||
reason="output mismatch (fastwc: ${fast_count}, ${BENCH_NAME} wc: ${wc_count})"
|
|
||||||
elif (( fast_ms > wc_ms )); then
|
|
||||||
verdict='FAIL'
|
|
||||||
reason="fastwc was slower (fastwc: ${fast_ms}ms vs ${BENCH_NAME} wc: ${wc_ms}ms)"
|
|
||||||
fi
|
|
||||||
|
|
||||||
row=$(printf '%-28s %10s %10s %8s %s\n' \
|
|
||||||
"$label" "wc: ${wc_ms}ms" "fastwc: ${fast_ms}ms" "$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
|
|
||||||
}
|
}
|
||||||
|
|
||||||
# run_stdin_case <words|lines> <n-lines> <-w|-l|...> — same race, but the
|
# run_stdin_case <words|lines> <n-lines> <-w|-l|...> — same race, but the
|
||||||
@@ -228,7 +342,7 @@ run_stdin_case() {
|
|||||||
local mode="$1" lines="$2" flag="$3"
|
local mode="$1" lines="$2" flag="$3"
|
||||||
local noun='lines'; [[ "$lines" -eq 1 ]] && noun='line'
|
local noun='lines'; [[ "$lines" -eq 1 ]] && noun='line'
|
||||||
local label="stdin ${mode} (${lines} ${noun})"
|
local label="stdin ${mode} (${lines} ${noun})"
|
||||||
local file wc_count fast_count wc_ms fast_ms ratio verdict reason row
|
local file wc_count fast_count wc_us fast_us
|
||||||
local i d
|
local i d
|
||||||
|
|
||||||
file=$(createtxt "$lines") || return 1
|
file=$(createtxt "$lines") || return 1
|
||||||
@@ -236,47 +350,17 @@ run_stdin_case() {
|
|||||||
wc_count=$(capture_count "${WC_CMD[@]}" "$flag" < "$file")
|
wc_count=$(capture_count "${WC_CMD[@]}" "$flag" < "$file")
|
||||||
fast_count=$(capture_count "$FASTWC" "$flag" < "$file")
|
fast_count=$(capture_count "$FASTWC" "$flag" < "$file")
|
||||||
|
|
||||||
wc_ms=''
|
wc_us=''
|
||||||
fast_ms=''
|
fast_us=''
|
||||||
for ((i = 0; i < BENCH_REPS; i++)); do
|
for ((i = 0; i < BENCH_REPS; i++)); do
|
||||||
d=$(time_ms "${WC_CMD[@]}" "$flag" < "$file")
|
d=$(timeit "${WC_CMD[@]}" "$flag" < "$file")
|
||||||
[[ -z "$wc_ms" || "$d" -lt "$wc_ms" ]] && wc_ms="$d"
|
[[ -z "$wc_us" || "$d" -lt "$wc_us" ]] && wc_us="$d"
|
||||||
|
|
||||||
d=$(time_ms "$FASTWC" "$flag" < "$file")
|
d=$(timeit "$FASTWC" "$flag" < "$file")
|
||||||
[[ -z "$fast_ms" || "$d" -lt "$fast_ms" ]] && fast_ms="$d"
|
[[ -z "$fast_us" || "$d" -lt "$fast_us" ]] && fast_us="$d"
|
||||||
done
|
done
|
||||||
|
|
||||||
# ratio: how many times faster fastwc is than the reference (wc / fastwc)
|
finish_race "$label" "$wc_count" "$fast_count" "$wc_us" "$fast_us"
|
||||||
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
|
|
||||||
|
|
||||||
verdict='PASS'
|
|
||||||
reason=''
|
|
||||||
if [[ "$fast_count" != "$wc_count" ]]; then
|
|
||||||
verdict='FAIL'
|
|
||||||
reason="output mismatch (fastwc: ${fast_count}, ${BENCH_NAME} wc: ${wc_count})"
|
|
||||||
elif (( fast_ms > wc_ms )); then
|
|
||||||
verdict='FAIL'
|
|
||||||
reason="fastwc was slower (fastwc: ${fast_ms}ms vs ${BENCH_NAME} wc: ${wc_ms}ms)"
|
|
||||||
fi
|
|
||||||
|
|
||||||
row=$(printf '%-28s %10s %10s %8s %s\n' \
|
|
||||||
"$label" "wc: ${wc_ms}ms" "fastwc: ${fast_ms}ms" "$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
|
|
||||||
}
|
}
|
||||||
|
|
||||||
# run_solo_case <n-lines> <-l|...> — time fastwc alone on <n-lines> of data,
|
# run_solo_case <n-lines> <-l|...> — time fastwc alone on <n-lines> of data,
|
||||||
@@ -286,7 +370,7 @@ run_solo_case() {
|
|||||||
local lines="$1" flag="$2"
|
local lines="$1" flag="$2"
|
||||||
local noun='lines'; [[ "$lines" -eq 1 ]] && noun='line'
|
local noun='lines'; [[ "$lines" -eq 1 ]] && noun='line'
|
||||||
local label="solo ${lines} ${noun}"
|
local label="solo ${lines} ${noun}"
|
||||||
local file ms best='' bytes gbps mlps i d
|
local file best_us='' best_ms bytes gbps mlps i d
|
||||||
|
|
||||||
file=$(createtxt "$lines") || {
|
file=$(createtxt "$lines") || {
|
||||||
printf '%-28s %s\n' "$label" 'SKIP (could not create test data)'
|
printf '%-28s %s\n' "$label" 'SKIP (could not create test data)'
|
||||||
@@ -294,16 +378,17 @@ run_solo_case() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
for ((i = 0; i < BENCH_REPS; i++)); do
|
for ((i = 0; i < BENCH_REPS; i++)); do
|
||||||
d=$(time_ms "$FASTWC" "$flag" "$file")
|
d=$(timeit "$FASTWC" "$flag" "$file")
|
||||||
[[ -z "$best" || "$d" -lt "$best" ]] && best="$d"
|
[[ -z "$best_us" || "$d" -lt "$best_us" ]] && best_us="$d"
|
||||||
done
|
done
|
||||||
|
best_ms=$(( best_us / 1000 ))
|
||||||
|
|
||||||
bytes=$((lines * 11))
|
bytes=$((lines * 11))
|
||||||
gbps=$(awk -v b="$bytes" -v ms="$best" 'BEGIN { printf "%.2f", b / ms / 1e6 }')
|
gbps=$(awk -v b="$bytes" -v ms="$best_ms" 'BEGIN { if (ms < 1) ms = 1; printf "%.2f", b / ms / 1e6 }')
|
||||||
mlps=$(awk -v l="$lines" -v ms="$best" 'BEGIN { printf "%.1f", l / ms / 1e3 }')
|
mlps=$(awk -v l="$lines" -v ms="$best_ms" 'BEGIN { if (ms < 1) ms = 1; printf "%.1f", l / ms / 1e3 }')
|
||||||
|
|
||||||
printf '%-28s %12s %12s %14s\n' \
|
printf '%-28s %25s %12s %14s\n' \
|
||||||
"$label" "fastwc: ${best}ms" "${gbps} GB/s" "${mlps} Mlines/s"
|
"$label" "fastwc: ${best_ms}ms (${best_us}µs)" "${gbps} GB/s" "${mlps} Mlines/s"
|
||||||
}
|
}
|
||||||
|
|
||||||
# run_cases <words|lines> <-w|-l> <size...> — run run_case for every size,
|
# run_cases <words|lines> <-w|-l> <size...> — run run_case for every size,
|
||||||
@@ -329,3 +414,27 @@ run_stdin_cases() {
|
|||||||
done
|
done
|
||||||
return $rc
|
return $rc
|
||||||
}
|
}
|
||||||
|
|
||||||
|
# print_averages — average the accumulated speedup ratios (reference time
|
||||||
|
# over fastwc time) per oracle and print them. Reads $RATIOS_FILE, which
|
||||||
|
# test-all.sh points at a shared temp file across all suites.
|
||||||
|
print_averages() {
|
||||||
|
local oracle avg n
|
||||||
|
|
||||||
|
[[ -s "$RATIOS_FILE" ]] || return 0
|
||||||
|
|
||||||
|
while IFS=$'\t' read -r oracle avg n; do
|
||||||
|
[[ -n "$oracle" ]] &&
|
||||||
|
printf 'average speedup vs %s wc: %s (%s cases)\n' \
|
||||||
|
"$oracle" "$avg" "$n"
|
||||||
|
done <<< "$(awk -F'\t' '
|
||||||
|
{ sum[$1] += $2; n[$1]++ }
|
||||||
|
END {
|
||||||
|
if (n["coreutils"])
|
||||||
|
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")"
|
||||||
|
}
|
||||||
|
|||||||
@@ -2,9 +2,10 @@
|
|||||||
# bench.sh — race fastwc against GNU coreutils wc on standard input, fed by
|
# bench.sh — race fastwc against GNU coreutils wc on standard input, fed by
|
||||||
# shell redirect (the same path every real pipeline uses). Counts must agree
|
# shell redirect (the same path every real pipeline uses). Counts must agree
|
||||||
# with coreutils' stdin behavior, and fastwc must not be slower.
|
# with coreutils' stdin behavior, and fastwc must not be slower.
|
||||||
# Fails fast: the moment fastwc is slower than (or disagrees with) coreutils
|
# Busybox wc is raced too, for shits and giggles.
|
||||||
# wc, a human readable report is written to FAILED-benchmark.txt and this
|
# Fails fast: the moment fastwc is slower than (or disagrees with) an
|
||||||
# script exits non-zero.
|
# oracle wc, a human readable report is written to FAILED-benchmark.txt
|
||||||
|
# and this script exits non-zero.
|
||||||
#
|
#
|
||||||
# usage: ./bench.sh
|
# usage: ./bench.sh
|
||||||
set -u
|
set -u
|
||||||
@@ -15,26 +16,29 @@ source "$REPO_DIR/benchmarks/std.sh"
|
|||||||
|
|
||||||
checkfastwc
|
checkfastwc
|
||||||
checkwc
|
checkwc
|
||||||
BENCH_NAME="coreutils"
|
|
||||||
|
|
||||||
printf 'benchmarking %s wc vs fastwc: stdin (%s interleaved runs each, minimum kept)\n' \
|
for oracle in $ORACLES; do
|
||||||
|
select_oracle "$oracle" || exit 1
|
||||||
|
|
||||||
|
printf 'benchmarking %s wc vs fastwc: stdin (%s interleaved runs each, minimum kept)\n' \
|
||||||
"$BENCH_NAME" "$BENCH_REPS"
|
"$BENCH_NAME" "$BENCH_REPS"
|
||||||
printf '%-28s %10s %10s %8s %s\n' 'test' 'wc' 'fastwc' 'ratio' 'status'
|
printf '%-28s %16s %24s %8s %s\n' 'test' 'wc' 'fastwc (µs)' 'ratio' 'status'
|
||||||
|
|
||||||
printf '%s\n' '--- stdin lines ---'
|
printf '%s\n' '--- stdin lines ---'
|
||||||
if ! run_stdin_cases lines -l 10000 100000 1000000 10000000; then
|
if ! run_stdin_cases lines -l 10000 100000 1000000 10000000; then
|
||||||
exit 1
|
exit 1
|
||||||
fi
|
fi
|
||||||
|
|
||||||
printf '%s\n' '--- stdin words ---'
|
printf '%s\n' '--- stdin words ---'
|
||||||
if ! run_stdin_cases words -w 1000 100000 1000000; then
|
if ! run_stdin_cases words -w 1000 100000 1000000; then
|
||||||
exit 1
|
exit 1
|
||||||
fi
|
fi
|
||||||
|
|
||||||
printf '%s\n' '--- stdin lines+words+bytes ---'
|
printf '%s\n' '--- stdin lines+words+bytes ---'
|
||||||
if ! run_stdin_cases lines -lwc 1000000; then
|
if ! run_stdin_cases lines -lwc 1000000; then
|
||||||
exit 1
|
exit 1
|
||||||
fi
|
fi
|
||||||
|
|
||||||
printf '\nall %s stdin benchmarks passed — fastwc was never slower than %s wc\n' \
|
printf '\nall %s stdin benchmarks passed — fastwc was never slower than %s wc\n' \
|
||||||
"$BENCH_NAME" "$BENCH_NAME"
|
"$BENCH_NAME" "$BENCH_NAME"
|
||||||
|
done
|
||||||
|
|||||||
+23
-3
@@ -1,21 +1,37 @@
|
|||||||
#!/usr/bin/env bash
|
#!/usr/bin/env bash
|
||||||
# test-all.sh — compile the benchmark helper tools first, then run every
|
# test-all.sh — compile the benchmark helper tools first, then run every
|
||||||
# benchmark suite (words, lines incl. monsters, stdin). Exits non-zero if
|
# benchmark suite (words, lines incl. monsters, stdin) against every
|
||||||
# any of them fails.
|
# 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
|
# usage: ./test-all.sh
|
||||||
set -u
|
set -u
|
||||||
|
|
||||||
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
|
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
|
||||||
|
REPO_DIR="$(cd "$SCRIPT_DIR/.." && pwd)"
|
||||||
|
|
||||||
|
# Share one ratios file across all suites so the final average covers
|
||||||
|
# every case; each suite still races through its own process.
|
||||||
|
RATIOS_FILE="$(mktemp /tmp/fastwc-ratios.XXXXXX)"
|
||||||
|
export RATIOS_FILE
|
||||||
|
|
||||||
|
source "$SCRIPT_DIR/std.sh"
|
||||||
|
|
||||||
# 1. build the helper tools before running any benchmark so createtxt()
|
# 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"
|
mkdir -p "$SCRIPT_DIR/tools"
|
||||||
if ! cc -O2 -Wall -o "$SCRIPT_DIR/tools/genfile" "$SCRIPT_DIR/tools/genfile.c"; then
|
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
|
printf 'test-all: error: failed to compile %s\n' "$SCRIPT_DIR/tools/genfile.c" >&2
|
||||||
exit 1
|
exit 1
|
||||||
fi
|
fi
|
||||||
printf 'built %s\n' "$SCRIPT_DIR/tools/genfile"
|
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)
|
# 2. run each suite (all run regardless, so every result is reported)
|
||||||
"$SCRIPT_DIR/files/words/bench.sh"
|
"$SCRIPT_DIR/files/words/bench.sh"
|
||||||
@@ -29,6 +45,10 @@ rc_stdin=$?
|
|||||||
if [[ $rc_words -ne 0 || $rc_lines -ne 0 || $rc_stdin -ne 0 ]]; then
|
if [[ $rc_words -ne 0 || $rc_lines -ne 0 || $rc_stdin -ne 0 ]]; then
|
||||||
printf '\ntest-all: FAILED (words=%s, lines=%s, stdin=%s)\n' \
|
printf '\ntest-all: FAILED (words=%s, lines=%s, stdin=%s)\n' \
|
||||||
"$rc_words" "$rc_lines" "$rc_stdin" >&2
|
"$rc_words" "$rc_lines" "$rc_stdin" >&2
|
||||||
|
print_averages
|
||||||
|
rm -f "$RATIOS_FILE"
|
||||||
exit 1
|
exit 1
|
||||||
fi
|
fi
|
||||||
printf '\ntest-all: all benchmarks passed\n'
|
printf '\ntest-all: all benchmarks passed\n'
|
||||||
|
print_averages
|
||||||
|
rm -f "$RATIOS_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;
|
||||||
|
}
|
||||||
@@ -2,9 +2,38 @@ AC_PREREQ([2.69])
|
|||||||
AC_INIT([fastwc], [0.1.0], [], [fastwc])
|
AC_INIT([fastwc], [0.1.0], [], [fastwc])
|
||||||
AC_CONFIG_SRCDIR([src/main.c])
|
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_PROG_CC
|
||||||
|
AC_CHECK_TOOL([STRIP], [strip], [:])
|
||||||
AC_USE_SYSTEM_EXTENSIONS
|
AC_USE_SYSTEM_EXTENSIONS
|
||||||
AM_INIT_AUTOMAKE([foreign subdir-objects])
|
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_CONFIG_FILES([Makefile])
|
||||||
AC_OUTPUT
|
AC_OUTPUT
|
||||||
|
|||||||
+129
-37
@@ -6,27 +6,108 @@ that time, and what we paid for it.
|
|||||||
|
|
||||||
## The scoreboard, with receipts
|
## The scoreboard, with receipts
|
||||||
|
|
||||||
Benchmarked on an Intel Core Ultra 7 265KF, min of 3 interleaved runs,
|
Benchmarked on an AMD Ryzen AI 7 PRO 350 — 8 cores / 16 threads, boost
|
||||||
page cache warm. The benchmark suite fails the moment we lose a single
|
up to 5.09 GHz, 384 KiB L1d / 256 KiB L1i / 8 MiB L2 / 16 MiB L3 —
|
||||||
case, so every number below survived contact with the contract.
|
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 |
|
| Case | GNU coreutils | busybox | toybox | fastwc |
|
||||||
|------|--------------:|-------:|
|
|------|--------------:|--------:|-------:|-------:|
|
||||||
| words, 100k lines | 1-2ms | ≤1ms |
|
| words, 1M lines (11 MB) | 7.9ms | 16.2ms | 15.9ms | **1.6ms** |
|
||||||
| lines, 100k | 1-2ms | ≤1ms |
|
| words, 10M lines (110 MB) | 66ms | 160ms | 157ms | **5.1ms** |
|
||||||
| lines, 1M | 2-3ms | **1ms** |
|
| lines, 1M (11 MB) | startup | 16.9ms | 17.6ms | **1.6ms** |
|
||||||
| lines, 10M | 21-24ms | **8-9ms** |
|
| lines, 10M (110 MB) | 5.8ms | 160ms | 154ms | **4.6ms** |
|
||||||
| lines, 100M (monster) | ~140ms | **~70ms** |
|
| lines, 100M (monster) | 47.5ms | — | — | **29.2ms** |
|
||||||
| lines, 1B (solo) | — | **~4-6s** |
|
| lines, 1B (solo) | — | — | — | **266ms** |
|
||||||
| bytes, 1GB sparse | reads all of it | `st_size`, no read |
|
| 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
|
That is a 13x win over GNU on 10M lines of words, 4.9x on 1M, and 1.6x
|
||||||
a 2x win on the 100M monster. At 1B lines — 11 GB of data — the solo
|
on the 100M monster. GNU's threaded counter manages a genuine dead
|
||||||
run lands around 4-6 seconds (200-270 Mlines/s, warm cache), and the
|
heat at 110 MB of plain lines (5.8ms vs our 4.6ms — it never wins;
|
||||||
bottleneck is
|
we allow it a 2% tie margin and it still can't take it). The moment
|
||||||
honest to admit: an 11 GB file does not fit in the 15 GB of RAM this
|
the file stops fitting in a polite buffer, the dead heat stops being
|
||||||
machine has, so the last monster is racing the disk. The 100M case,
|
polite. At 1B lines — 11 GB, which this machine's 64 GiB actually
|
||||||
which fits, runs at ~17 GB/s, and that number is the counting.
|
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
|
## 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
|
(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,
|
(16), else scalar SWAR. The whitespace test is one unsigned compare,
|
||||||
`(x - 9) < 5`, plus equalities for space and NBSP — exactly GNU's
|
`(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
|
2. **Regular files are mapped, not streamed.** `mmap` hands us the
|
||||||
pages; there is no `fread`, no kernel-to-user copy, no 840 syscalls
|
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
|
per 110 MB file. One syscall in, one count out. A stdin redirect
|
||||||
@@ -43,19 +125,30 @@ which fits, runs at ~17 GB/s, and that number is the counting.
|
|||||||
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.** Files over 8 MiB are split into 64-byte
|
||||||
aligned slices counted by up to 16 threads (12 past 32 MiB, 16 past
|
aligned slices counted by up to one thread per core (capped at 24)
|
||||||
256 MiB). The kernels are pure, so the split needs no locks; word
|
past 256 MiB, 12 past 32 MiB, 4 past 8 MiB. The kernels are pure, so
|
||||||
boundaries between slices are seeded from the byte before the slice,
|
the split needs no locks; word boundaries between slices are seeded
|
||||||
which makes the split exact. Below 8 MiB the thread spawn would cost
|
from the byte before the slice, which makes the split exact. Below
|
||||||
more than the counting, so we don't bother.
|
8 MiB the thread spawn would cost more than the counting, so we don't
|
||||||
|
bother.
|
||||||
4. **No work that isn't asked for.** `-c` on a regular file is
|
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
|
`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
|
## Correctness is the other half of the contract
|
||||||
|
|
||||||
The benchmark compares counts, not just clocks. Matching GNU byte for
|
The benchmark compares counts, not just clocks — against all three
|
||||||
byte took some archaeology:
|
oracles. Matching GNU byte for byte took some archaeology:
|
||||||
|
|
||||||
- GNU counts U+00A0 (non-breaking space) as a word separator even in
|
- 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 —
|
the C locale, and the full Unicode white space set in `-m` mode —
|
||||||
@@ -81,19 +174,18 @@ combination) passes 100%.
|
|||||||
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.
|
||||||
We reproduced this, then declined to. fastwc counts the data, not
|
We reproduced this, then declined to. fastwc counts the data, not
|
||||||
the plumbing.
|
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
|
## Reproducing
|
||||||
|
|
||||||
```sh
|
```sh
|
||||||
make release
|
make bench # build release + run every suite
|
||||||
./benchmarks/test-all.sh # words, lines (monsters included), stdin
|
./benchmarks/test-all.sh # words, lines (monsters included), stdin
|
||||||
```
|
```
|
||||||
|
|
||||||
The suites live in `benchmarks/files/{lines,words}` and
|
`test-all.sh` compiles its two helper tools on the spot
|
||||||
`benchmarks/stdin/piping`. The lines suite ends with the monsters:
|
(`tools/genfile` for the test data, `tools/timeit` for microsecond
|
||||||
100M lines raced against coreutils, and 1B lines timed solo (no
|
timing) — no separate setup. The suites live in
|
||||||
reference to beat — the reference is us now).
|
`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).
|
||||||
|
|||||||
+1251
-252
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user