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:
@@ -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
|
||||
|
||||
@@ -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() {
|
||||
|
||||
@@ -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 <lines>
|
||||
* genfile --utf8 <lines>
|
||||
*/
|
||||
#include <stdint.h>
|
||||
#include <string.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <time.h>
|
||||
@@ -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 <lines>\n", argv[0]);
|
||||
fprintf(stderr, "usage: %s [--utf8] <lines>\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;
|
||||
|
||||
Reference in New Issue
Block a user