Debian Codegraph @

utf-8-validate/deps/is_utf8/benchmarks/bench.cpp

raw
1 #include "is_utf8.h"
2 #include "simdutf.h"
3 #include <assert.h>
4 #include <chrono>
5 #include <stdbool.h>
6 #include <stddef.h>
7 #include <stdint.h>
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include <string.h>
11
12 uint64_t nano() {
13 return std::chrono::duration_cast<::std::chrono::nanoseconds>(
14 std::chrono::steady_clock::now().time_since_epoch())
15 .count();
16 }
17
18 #ifdef _MSC_VER
19 #define never_inline __declspec(noinline)
20 #else
21 #define never_inline __attribute__((noinline))
22 #endif
23
24 // generate a string having at least length N
25 // can exceed by up to 3 chars, returns the actual length
26 size_t populate_utf8(char *data, size_t N) {
27 size_t i = 0;
28 for (; i < N;) {
29 int w = rand() & 0xFF;
30 if (w < 0x80) {
31 data[i++] = 0x20; // w;
32 } else if (w < 0xE0) {
33 data[i++] = 0xC2 + rand() % (0xDF - 0xC2 + 1);
34 data[i++] = 0x80 + rand() % (0xBF - 0x80 + 1);
35 } else if (w == 0xE0) {
36 data[i++] = w;
37 data[i++] = 0xA0 + rand() % (0xBF - 0xA0 + 1);
38 data[i++] = 0x80 + rand() % (0xBF - 0x80 + 1);
39 } else if (w <= 0xEC) {
40 data[i++] = w;
41 data[i++] = 0x80 + rand() % (0xBF - 0x80 + 1);
42 data[i++] = 0x80 + rand() % (0xBF - 0x80 + 1);
43 } else if (w == 0xED) {
44 data[i++] = w;
45 data[i++] = 0x80 + rand() % (0x9F - 0x80 + 1);
46 data[i++] = 0x80 + rand() % (0xBF - 0x80 + 1);
47 } else if (w <= 0xEF) {
48 data[i++] = w;
49 data[i++] = 0x80 + rand() % (0xBF - 0x80 + 1);
50 data[i++] = 0x80 + rand() % (0xBF - 0x80 + 1);
51 } else if (w < 0xF0) {
52 data[i++] = 0xF1 + rand() % (0xF3 - 0xF1 + 1);
53 data[i++] = 0x80 + rand() % (0xBF - 0x80 + 1);
54 data[i++] = 0x80 + rand() % (0xBF - 0x80 + 1);
55 data[i++] = 0x80 + rand() % (0xBF - 0x80 + 1);
56 } else if (w == 0xF0) {
57 data[i++] = w;
58 data[i++] = 0x90 + rand() % (0xBF - 0x90 + 1);
59 data[i++] = 0x80 + rand() % (0xBF - 0x80 + 1);
60 data[i++] = 0x80 + rand() % (0xBF - 0x80 + 1);
61 } else if (w <= 0xF3) {
62 data[i++] = 0xF1 + rand() % (0xF3 - 0xF1 + 1);
63 data[i++] = 0x80 + rand() % (0xBF - 0x80 + 1);
64 data[i++] = 0x80 + rand() % (0xBF - 0x80 + 1);
65 data[i++] = 0x80 + rand() % (0xBF - 0x80 + 1);
66 } else if (w == 0xF4) {
67 data[i++] = w;
68 data[i++] = 0x80 + rand() % (0x8F - 0x80 + 1);
69 data[i++] = 0x80 + rand() % (0xBF - 0x80 + 1);
70 data[i++] = 0x80 + rand() % (0xBF - 0x80 + 1);
71 }
72 }
73 data[i] = '\0';
74 return i;
75 }
76
77 // copied in part from Guava
78 static never_inline bool basic_validate_utf8(const char *b, size_t length) {
79 const unsigned char *bytes = (const unsigned char *)b;
80 for (size_t index = 0;;) {
81 unsigned char byte1;
82
83 do { // fast ASCII Path
84 if (index >= length) {
85 return true;
86 }
87 byte1 = bytes[index++];
88 } while (byte1 < 0x80);
89 if (byte1 < 0xE0) {
90 // Two-byte form.
91 if (index == length) {
92 return false;
93 }
94 if (byte1 < 0xC2 || bytes[index++] > 0xBF) {
95 return false;
96 }
97 } else if (byte1 < 0xF0) {
98 // Three-byte form.
99 if (index + 1 >= length) {
100 return false;
101 }
102 unsigned char byte2 = bytes[index++];
103 if (byte2 > 0xBF
104 // Overlong? 5 most significant bits must not all be zero.
105 || (byte1 == 0xE0 && byte2 < 0xA0)
106 // Check for illegal surrogate codepoints.
107 || (byte1 == 0xED && 0xA0 <= byte2)
108 // Third byte trailing-byte test.
109 || bytes[index++] > 0xBF) {
110 return false;
111 }
112 } else {
113
114 // Four-byte form.
115 if (index + 2 >= length) {
116 return false;
117 }
118 int byte2 = bytes[index++];
119 if (byte2 > 0xBF
120 // Check that 1 <= plane <= 16. Tricky optimized form of:
121 // if (byte1 > (byte) 0xF4
122 // || byte1 == (byte) 0xF0 && byte2 < (byte) 0x90
123 // || byte1 == (byte) 0xF4 && byte2 > (byte) 0x8F)
124 || (((byte1 << 28) + (byte2 - 0x90)) >> 30) != 0
125 // Third byte trailing-byte test
126 || bytes[index++] > 0xBF
127 // Fourth byte trailing-byte test
128 || bytes[index++] > 0xBF) {
129 return false;
130 }
131 }
132 }
133 }
134
135
136 bool zerobuffer_bench(size_t N) {
137 printf("zero buffer \n");
138 printf("string size = %zu \n", N);
139 char *input = new char[N]{};
140 volatile bool isgood{true};
141
142 {
143 uint64_t start = nano();
144 uint64_t finish = start;
145 size_t count{0};
146 uint64_t threshold = 500000000;
147 for (; finish - start < threshold;) {
148 count++;
149 isgood &= basic_validate_utf8(input, N);
150 finish = nano();
151 }
152 double t = (N * count) / double(finish - start);
153
154 printf("basic_validate_utf8 %f GB/s\n", t);
155 }
156
157 {
158 uint64_t start = nano();
159 uint64_t finish = start;
160 size_t count{0};
161 uint64_t threshold = 500000000;
162 for (; finish - start < threshold;) {
163 count++;
164 isgood &= simdutf::validate_utf8(input, N);
165 finish = nano();
166 }
167 double t = (N * count) / double(finish - start);
168
169 printf("simdutf %f GB/s\n", t);
170 }
171
172 {
173 uint64_t start = nano();
174 uint64_t finish = start;
175 size_t count{0};
176 uint64_t threshold = 500000000;
177 for (; finish - start < threshold;) {
178 count++;
179 isgood &= is_utf8(input, N);
180 finish = nano();
181 }
182 double t = (N * count) / double(finish - start);
183
184 printf("is_utf8 %f GB/s\n", t);
185 }
186 delete[] input;
187 printf("\n");
188 return isgood;
189 }
190
191 bool bench(size_t N) {
192 printf("random UTF-8\n");
193 printf("string size = %zu \n", N);
194 char *input = new char[N];
195 populate_utf8(input, N);
196 volatile bool isgood{true};
197
198 {
199 uint64_t start = nano();
200 uint64_t finish = start;
201 size_t count{0};
202 uint64_t threshold = 500000000;
203 for (; finish - start < threshold;) {
204 count++;
205 isgood &= basic_validate_utf8(input, N);
206 finish = nano();
207 }
208 double t = (N * count) / double(finish - start);
209
210 printf("basic_validate_utf8 %f GB/s\n", t);
211 }
212
213 {
214 uint64_t start = nano();
215 uint64_t finish = start;
216 size_t count{0};
217 uint64_t threshold = 500000000;
218 for (; finish - start < threshold;) {
219 count++;
220 isgood &= simdutf::validate_utf8(input, N);
221 finish = nano();
222 }
223 double t = (N * count) / double(finish - start);
224
225 printf("simdutf %f GB/s\n", t);
226 }
227
228 {
229 uint64_t start = nano();
230 uint64_t finish = start;
231 size_t count{0};
232 uint64_t threshold = 500000000;
233 for (; finish - start < threshold;) {
234 count++;
235 isgood &= is_utf8(input, N);
236 finish = nano();
237 }
238 double t = (N * count) / double(finish - start);
239
240 printf("is_utf8 %f GB/s\n", t);
241 }
242 delete[] input;
243 printf("\n");
244 return isgood;
245 }
246
247 int main() {
248 return (bench(40096) & bench(100000) & bench(50000))
249 & (zerobuffer_bench(40096) & zerobuffer_bench(100000) & zerobuffer_bench(50000))
250 ? EXIT_SUCCESS : EXIT_FAILURE;
251 }