diff --git a/README.md b/README.md index ffed653..0225169 100644 --- a/README.md +++ b/README.md @@ -260,7 +260,9 @@ monsters: 100M lines raced against coreutils, and 1B lines timed solo — there is no reference for that one; we are the reference now. The lines suite also races `-L` at 1M and 10M lines, and ends with a 128-file list (12.8M lines) so the worker pool is under the contract -too, not just in some table in this file. When the run ends, the +too, not just in some table in this file. The words suite finishes with +`-w`, `-m` and `-L` over mixed UTF-8 text under a real UTF-8 locale, +because the decoder's counts are the ones that go wrong quietly. When the run ends, the average speedup of fastwc against each oracle is printed. ## Development diff --git a/benchmarks/files/words/bench.sh b/benchmarks/files/words/bench.sh index dc38d44..79ee1d7 100755 --- a/benchmarks/files/words/bench.sh +++ b/benchmarks/files/words/bench.sh @@ -30,3 +30,19 @@ for oracle in $ORACLES; do printf '\nall %s words benchmarks passed — fastwc was never slower than %s wc\n' \ "$BENCH_NAME" "$BENCH_NAME" done + +# The multibyte path, raced against coreutils only: under a UTF-8 locale +# wc decodes, and the case is about our decoder agreeing with its counts +# and beating its speed. 300k mixed lines is about 6 MB, which is enough +# for both sides to be counting rather than starting. +select_oracle coreutils || exit 1 +printf '\n%s\n' '--- multibyte, UTF-8 locale (coreutils) ---' +if ! run_utf8_case words 300000 -w; then + exit 1 +fi +if ! run_utf8_case chars 300000 -m; then + exit 1 +fi +if ! run_utf8_case 'longest line' 300000 -L; then + exit 1 +fi diff --git a/benchmarks/std.sh b/benchmarks/std.sh index 974da98..f28a17b 100755 --- a/benchmarks/std.sh +++ b/benchmarks/std.sh @@ -33,6 +33,17 @@ set -u export LC_ALL=C export LC_CTYPE=C +# One UTF-8 locale for the multibyte cases, or empty when the host has +# none: those cases need real decoding on both sides, so they run under +# it explicitly rather than inheriting the C locale above. +UTF8_LOCALE='' +for cand in en_US.UTF-8 C.UTF-8; do + if LC_ALL="$cand" locale charmap 2>/dev/null | grep -qi 'utf-8'; then + UTF8_LOCALE="$cand" + break + fi +done + 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 @@ -445,6 +456,67 @@ run_multi_case() { finish_race "$label" "$wc_out" "$fast_out" "$wc_us" "$fast_us" } +# createtxt_utf8 — mixed UTF-8 text (CJK, words, spaces, a +# combining accent) in this suite's .data directory. Prints the path and +# sets $TEXT_FILE; the file is reused when its name matches. +createtxt_utf8() { + local lines="$1" + + TEXT_FILE="$DATA_DIR/utf8-$lines.txt" + + if [[ ! -s "$TEXT_FILE" ]]; then + mkdir -p "$DATA_DIR" + if [[ -x "$GENFILE" ]]; then + "$GENFILE" --utf8 "$lines" > "$TEXT_FILE" || { + printf 'createtxt_utf8: error: failed to generate %s\n' \ + "$TEXT_FILE" >&2 + return 1 + } + else + printf 'createtxt_utf8: error: %s not built\n' "$GENFILE" >&2 + return 1 + fi + fi + printf '%s\n' "$TEXT_FILE" +} + +# run_utf8_case — race under a UTF-8 locale, where +# -w/-m/-L go through the decoder instead of the byte kernels. The +# oracle's own multibyte semantics are the reference here, which is why +# the case has to run in the locale rather than pinned to C. +run_utf8_case() { + local mode="$1" lines="$2" flag="$3" + local label="$mode utf8 (${lines} lines)" + local file wc_count fast_count wc_us fast_us i d + + if [[ -z "$UTF8_LOCALE" ]]; then + printf '%-28s %16s %24s %8s %s\n' "$label" '-' '-' '-' \ + 'SKIP (no UTF-8 locale)' + return 0 + fi + + file=$(createtxt_utf8 "$lines") || return 1 + + wc_count=$(capture_count env LC_ALL="$UTF8_LOCALE" LC_CTYPE="$UTF8_LOCALE" \ + "${WC_CMD[@]}" "$flag" "$file") + fast_count=$(capture_count env LC_ALL="$UTF8_LOCALE" \ + LC_CTYPE="$UTF8_LOCALE" "$FASTWC" "$flag" "$file") + + wc_us='' + fast_us='' + for ((i = 0; i < BENCH_REPS; i++)); do + d=$(timeit env LC_ALL="$UTF8_LOCALE" LC_CTYPE="$UTF8_LOCALE" \ + "${WC_CMD[@]}" "$flag" "$file") + [[ -z "$wc_us" || "$d" -lt "$wc_us" ]] && wc_us="$d" + + d=$(timeit env LC_ALL="$UTF8_LOCALE" LC_CTYPE="$UTF8_LOCALE" \ + "$FASTWC" "$flag" "$file") + [[ -z "$fast_us" || "$d" -lt "$fast_us" ]] && fast_us="$d" + done + + finish_race "$label" "$wc_count" "$fast_count" "$wc_us" "$fast_us" +} + # run_stdin_cases <-w|-l|...> — run run_stdin_case # for every size, stopping at the first failure. run_stdin_cases() { diff --git a/benchmarks/tools/genfile.c b/benchmarks/tools/genfile.c index 2681f6a..12415e3 100644 --- a/benchmarks/tools/genfile.c +++ b/benchmarks/tools/genfile.c @@ -4,9 +4,15 @@ * (createtxt in std.sh) to build test data quickly; std.sh falls back to a * slow shell loop when this binary has not been built. * + * With --utf8 the lines are mixed text instead: CJK runs, ASCII words, a + * tab, and the Unicode spaces, which is the data the multibyte path was + * built for. + * * usage: genfile + * genfile --utf8 */ #include +#include #include #include #include @@ -23,6 +29,18 @@ static const char alpha[] = static uint64_t state; +/* One three-byte code point, U+4E00..U+9FFF, which is where dense CJK + * lives and what the block kernel is written for. */ +static size_t emit_cjk(char *out, unsigned r) +{ + unsigned cp = 0x4e00 + r % 0x5200; + + out[0] = (char)(0xe0 | (cp >> 12)); + out[1] = (char)(0x80 | ((cp >> 6) & 0x3f)); + out[2] = (char)(0x80 | (cp & 0x3f)); + return 3; +} + static uint64_t next(void) { state ^= state << 13; @@ -39,8 +57,15 @@ int main(int argc, char **argv) char buf[CHUNK]; size_t used = 0; + int utf8 = 0; + + if (argc == 3 && strcmp(argv[1], "--utf8") == 0) { + utf8 = 1; + argv++; + argc--; + } if (argc != 2) { - fprintf(stderr, "usage: %s \n", argv[0]); + fprintf(stderr, "usage: %s [--utf8] \n", argv[0]); return 2; } n = atol(argv[1]); @@ -53,10 +78,47 @@ int main(int argc, char **argv) state = (uint64_t)time(NULL) ^ ((uint64_t)getpid() << 32) ^ 0x9E3779B97F4A7C15ULL; for (i = 0; i < n; i++) { - for (j = 0; j < LINE_LEN; j++) - buf[used++] = alpha[next() % ALPHA_LEN]; + if (!utf8) { + for (j = 0; j < LINE_LEN; j++) + buf[used++] = alpha[next() % ALPHA_LEN]; + } else { + unsigned pick = (unsigned)(next() % 100); + unsigned k, runs; + + if (pick < 8) { /* a tab indented line */ + buf[used++] = '\t'; + } + if (pick < 55) { /* a run of CJK, the dense case */ + runs = 1 + (unsigned)(next() % 12); + for (k = 0; k < runs; k++) + used += emit_cjk(buf + used, (unsigned)next()); + if (pick < 15) /* ... followed by an ASCII word */ + for (j = 0, k = 3 + (unsigned)(next() % 6); j < (int)k; j++) + buf[used++] = alpha[next() % 26]; + } else if (pick < 85) { /* plain words */ + runs = 1 + (unsigned)(next() % 5); + for (k = 0; k < runs; k++) { + if (k) + buf[used++] = ' '; + for (j = 0; j < 7; j++) + buf[used++] = alpha[next() % 26]; + } + } else if (pick < 92) { /* an ideographic space */ + buf[used++] = '\xe3'; + buf[used++] = '\x80'; + buf[used++] = '\x80'; + } else if (pick < 97) { /* a no-break space */ + buf[used++] = '\xc2'; + buf[used++] = '\xa0'; + } else { /* a combining accent, width zero */ + buf[used++] = 'e'; + buf[used++] = '\xcc'; + buf[used++] = '\x81'; + } + } buf[used++] = '\n'; - if (used + LINE_LEN + 1 > CHUNK) { + /* a UTF-8 line is up to ~50 bytes, an ASCII line exactly 11 */ + if (used + 128 > CHUNK) { if (fwrite(buf, 1, used, stdout) != used) return 1; used = 0; diff --git a/docs/PERFORMANCE.md b/docs/PERFORMANCE.md index 6f876e8..a8f4149 100644 --- a/docs/PERFORMANCE.md +++ b/docs/PERFORMANCE.md @@ -287,6 +287,13 @@ make bench # build release + run every suite ./benchmarks/test-all.sh # words, lines (monsters included), stdin ``` +The words suite ends with `-w`, `-m` and `-L` over 300k lines of mixed +UTF-8 under `en_US.UTF-8` (or `C.UTF-8`), raced against coreutils. The +suite pins the C locale for every other case, so without these the +decoder had no contract at all; the case skips with a printed note on a +host with no UTF-8 locale, because a multibyte count cannot be checked in +a single-byte one. + The lines suite races `-L` as well as `-l`, and ends by counting 128 files of about 1.1 MB each, which is the worker pool's case: both sides finish in well over a second's worth of work, so the row is a race and