UTF-8 validation in x86-64: a scalar reference and an AVX2 version that checks 32 bytes per step. Every build fuzzes one against the other.
Benchmark: 64 MiB of ASCII and 64 MiB of mixed text
Browse filesBest of five runs each. The build runs it, so every build log carries the numbers for the machine that built it.
failing
5 steps in 1 ms
2 files changed
+108−0
@@ -19,5 +19,15 @@ on = "windows"
1919 run = ["./build/test.exe"]
2020 expect = "0 wrong"
2121
22+[[step]]
23+name = "build the benchmark"
24+run = ["gcc", "-O2", "-Wall", "-o", "build/bench.exe", "bench/bench.c", "build/utf8_scalar.obj"]
25+
26+[[step]]
27+name = "time it on 64 MiB"
28+on = "windows"
29+run = ["./build/bench.exe"]
30+expect = "MiB of mixed text"
31+
2232 [artifact]
2333 path = "build/utf8_scalar.obj"
@@ -0,0 +1,98 @@
1+// bench.c: times the validator on 64 MiB of ASCII and 64 MiB of mixed
2+// 1- to 4-byte text, best of five runs each.
3+//
4+// build/bench.exe
5+
6+#include <stdint.h>
7+#include <stdio.h>
8+#include <stdlib.h>
9+#include <windows.h>
10+
11+int utf8_valid_scalar(const uint8_t *buf, size_t len);
12+
13+static uint64_t rng = 0x2545F4914F6CDD1Dull;
14+
15+static uint32_t next(void)
16+{
17+ rng ^= rng << 13;
18+ rng ^= rng >> 7;
19+ rng ^= rng << 17;
20+ return (uint32_t)rng;
21+}
22+
23+// One random code point: half of them ASCII, the rest spread over 2, 3 and 4 bytes.
24+static size_t put(uint8_t *p)
25+{
26+ uint32_t r = next(), c;
27+ switch (r % 6) {
28+ case 0: case 1: case 2:
29+ p[0] = 0x20 + (uint8_t)((r >> 8) % 95);
30+ return 1;
31+ case 3:
32+ c = 0x80 + (r >> 8) % 0x780;
33+ p[0] = (uint8_t)(0xC0 | c >> 6);
34+ p[1] = (uint8_t)(0x80 | (c & 63));
35+ return 2;
36+ case 4:
37+ c = 0x800 + (r >> 8) % 0xF000;
38+ if (c >= 0xD800)
39+ c += 0x800;
40+ p[0] = (uint8_t)(0xE0 | c >> 12);
41+ p[1] = (uint8_t)(0x80 | (c >> 6 & 63));
42+ p[2] = (uint8_t)(0x80 | (c & 63));
43+ return 3;
44+ default:
45+ c = 0x10000 + (r >> 8) % 0x100000;
46+ p[0] = (uint8_t)(0xF0 | c >> 18);
47+ p[1] = (uint8_t)(0x80 | (c >> 12 & 63));
48+ p[2] = (uint8_t)(0x80 | (c >> 6 & 63));
49+ p[3] = (uint8_t)(0x80 | (c & 63));
50+ return 4;
51+ }
52+}
53+
54+static double now(void)
55+{
56+ LARGE_INTEGER f, c;
57+ QueryPerformanceFrequency(&f);
58+ QueryPerformanceCounter(&c);
59+ return (double)c.QuadPart / (double)f.QuadPart;
60+}
61+
62+static double best_of_five(int (*fn)(const uint8_t *, size_t), const uint8_t *buf, size_t n)
63+{
64+ double best = 1e30;
65+ for (int i = 0; i < 5; i++) {
66+ double t = now();
67+ if (!fn(buf, n)) {
68+ puts("valid input rejected");
69+ exit(1);
70+ }
71+ t = now() - t;
72+ if (t < best)
73+ best = t;
74+ }
75+ return best;
76+}
77+
78+static void race(const char *name, const uint8_t *buf, size_t n)
79+{
80+ double scalar = best_of_five(utf8_valid_scalar, buf, n);
81+ printf("%.1f MiB of %s: scalar %.2f GB/s\n", n / 1048576.0, name, n / scalar / 1e9);
82+}
83+
84+int main(void)
85+{
86+ size_t size = (size_t)64 << 20, n = 0;
87+ uint8_t *text = malloc(size);
88+ if (!text)
89+ return 1;
90+ for (size_t i = 0; i < size; i++)
91+ text[i] = 0x20 + (uint8_t)(next() % 95);
92+ race("ASCII", text, size);
93+ while (n + 4 <= size)
94+ n += put(text + n);
95+ race("mixed text", text, n);
96+ free(text);
97+ return 0;
98+}