Race the UTF-8 decoder against coreutils

Every case in the suite pinned LC_ALL=C, so the multibyte path - the one
whose counts go wrong quietly rather than crashing - had no contract
behind it at all. genfile grew a --utf8 mode that writes mixed CJK,
ASCII words, a tab, an ideographic space, a no-break space and a
combining accent, and the words suite now races -w, -m and -L over 300k
lines of it under en_US.UTF-8 (C.UTF-8 as a fallback), against coreutils.

The case runs the locale on both sides rather than inheriting the C pin,
because the oracle's own decoder is the reference. It prints SKIP rather
than passing when the host has no UTF-8 locale: a multibyte count cannot
be checked in a single-byte one.
This commit is contained in:
2026-11-15 14:00:07 -05:00
parent 9b68ad4c6c
commit c45de29b0c
5 changed files with 164 additions and 5 deletions
+72
View File
@@ -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 <lines> — 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 <mode> <lines> <flag> — 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 <words|lines> <-w|-l|...> <size...> — run run_stdin_case
# for every size, stopping at the first failure.
run_stdin_cases() {