-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathto_ascii.cpp
246 lines (221 loc) · 8.49 KB
/
to_ascii.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
#include "ada/idna/to_ascii.h"
#include <cstdlib>
#include <iostream>
#include <memory>
#if ICU_AVAILABLE
#include <unicode/uidna.h>
#include <unicode/utf8.h>
#include <unicode/utypes.h>
#endif
#include "idna.h"
#include "performancecounters/event_counter.h"
event_collector collector;
size_t N = 1000;
#include <benchmark/benchmark.h>
std::string inputs[] = {
"-x.xn--zca",
"xn--zca.xn--zca",
"xn--mgba3gch31f060k",
"xn--1ch",
"x-.\xc3\x9f",
"me\xc3\x9f\x61\x67\x65\x66\x61\x63\x74\x6f\x72\x79\x2e\x63"
"\x61", // https://lemire.me/blog/2023/01/23/international-domain-names-where-does-https-mesagefactory-ca-lead-you/
};
double inputs_total_byte = []() -> double {
size_t bytes{0};
for (std::string& url_string : inputs) {
bytes += url_string.size();
}
return double(bytes);
}();
static void Ada(benchmark::State& state) {
for (auto _ : state) {
for (std::string& url_string : inputs) {
benchmark::DoNotOptimize(ada::idna::to_ascii(url_string));
}
}
if (collector.has_events()) {
event_aggregate aggregate{};
for (size_t i = 0; i < N; i++) {
std::atomic_thread_fence(std::memory_order_acquire);
collector.start();
for (std::string& url_string : inputs) {
benchmark::DoNotOptimize(ada::idna::to_ascii(url_string));
}
std::atomic_thread_fence(std::memory_order_release);
event_count allocate_count = collector.end();
aggregate << allocate_count;
}
state.counters["cycles/domain"] =
aggregate.best.cycles() / std::size(inputs);
state.counters["instructions/domain"] =
aggregate.best.instructions() / std::size(inputs);
state.counters["instructions/cycle"] =
aggregate.best.instructions() / aggregate.best.cycles();
state.counters["instructions/byte"] =
aggregate.best.instructions() / inputs_total_byte;
state.counters["instructions/ns"] =
aggregate.best.instructions() / aggregate.best.elapsed_ns();
state.counters["GHz"] =
aggregate.best.cycles() / aggregate.best.elapsed_ns();
state.counters["ns/domain"] =
aggregate.best.elapsed_ns() / std::size(inputs);
state.counters["cycle/byte"] = aggregate.best.cycles() / inputs_total_byte;
}
state.counters["time/byte"] = benchmark::Counter(
inputs_total_byte, benchmark::Counter::kIsIterationInvariantRate |
benchmark::Counter::kInvert);
state.counters["time/domain"] = benchmark::Counter(
double(std::size(inputs)), benchmark::Counter::kIsIterationInvariantRate |
benchmark::Counter::kInvert);
state.counters["speed"] = benchmark::Counter(
inputs_total_byte, benchmark::Counter::kIsIterationInvariantRate);
state.counters["url/s"] = benchmark::Counter(
double(std::size(inputs)), benchmark::Counter::kIsIterationInvariantRate);
}
BENCHMARK(Ada);
#if ICU_AVAILABLE
// returns empty string on error
std::string icu_to_ascii(std::string_view input) {
static std::string error = "";
std::string out(255, 0);
constexpr bool be_strict = false;
UErrorCode status = U_ZERO_ERROR;
uint32_t options =
UIDNA_CHECK_BIDI | UIDNA_CHECK_CONTEXTJ | UIDNA_NONTRANSITIONAL_TO_ASCII;
if (be_strict) {
options |= UIDNA_USE_STD3_RULES;
}
UIDNA* uidna = uidna_openUTS46(options, &status);
if (U_FAILURE(status)) {
return error;
}
UIDNAInfo info = UIDNA_INFO_INITIALIZER;
// RFC 1035 section 2.3.4.
// The domain name must be at most 255 octets.
// It cannot contain a label longer than 63 octets.
// Thus we should never need more than 255 octets, if we
// do the domain name is in error.
int32_t length =
uidna_nameToASCII_UTF8(uidna, input.data(), int32_t(input.length()),
out.data(), 255, &info, &status);
if (status == U_BUFFER_OVERFLOW_ERROR) {
status = U_ZERO_ERROR;
out.resize(length);
// When be_strict is true, this should not be allowed!
length =
uidna_nameToASCII_UTF8(uidna, input.data(), int32_t(input.length()),
out.data(), length, &info, &status);
}
// A label contains hyphen-minus ('-') in the third and fourth positions.
info.errors &= ~UIDNA_ERROR_HYPHEN_3_4;
// A label starts with a hyphen-minus ('-').
info.errors &= ~UIDNA_ERROR_LEADING_HYPHEN;
// A label ends with a hyphen-minus ('-').
info.errors &= ~UIDNA_ERROR_TRAILING_HYPHEN;
if (!be_strict) { // This seems to violate RFC 1035 section 2.3.4.
// A non-final domain name label (or the whole domain name) is empty.
info.errors &= ~UIDNA_ERROR_EMPTY_LABEL;
// A domain name label is longer than 63 bytes.
info.errors &= ~UIDNA_ERROR_LABEL_TOO_LONG;
// A domain name is longer than 255 bytes in its storage form.
info.errors &= ~UIDNA_ERROR_DOMAIN_NAME_TOO_LONG;
}
uidna_close(uidna);
if (U_FAILURE(status) || info.errors != 0 || length == 0) {
return error;
}
out.resize(length); // we possibly want to call :shrink_to_fit otherwise we
// use 255 bytes.
return out;
}
static void Icu(benchmark::State& state) {
for (auto _ : state) {
for (std::string& url_string : inputs) {
benchmark::DoNotOptimize(icu_to_ascii(url_string));
}
}
if (collector.has_events()) {
event_aggregate aggregate{};
for (size_t i = 0; i < N; i++) {
std::atomic_thread_fence(std::memory_order_acquire);
collector.start();
for (std::string& url_string : inputs) {
benchmark::DoNotOptimize(icu_to_ascii(url_string));
}
std::atomic_thread_fence(std::memory_order_release);
event_count allocate_count = collector.end();
aggregate << allocate_count;
}
state.counters["cycles/url"] = aggregate.best.cycles() / std::size(inputs);
state.counters["instructions/url"] =
aggregate.best.instructions() / std::size(inputs);
state.counters["instructions/cycle"] =
aggregate.best.instructions() / aggregate.best.cycles();
state.counters["instructions/byte"] =
aggregate.best.instructions() / inputs_total_byte;
state.counters["instructions/ns"] =
aggregate.best.instructions() / aggregate.best.elapsed_ns();
state.counters["GHz"] =
aggregate.best.cycles() / aggregate.best.elapsed_ns();
state.counters["ns/url"] = aggregate.best.elapsed_ns() / std::size(inputs);
state.counters["cycle/byte"] = aggregate.best.cycles() / inputs_total_byte;
}
state.counters["time/byte"] = benchmark::Counter(
inputs_total_byte, benchmark::Counter::kIsIterationInvariantRate |
benchmark::Counter::kInvert);
state.counters["time/url"] = benchmark::Counter(
double(std::size(inputs)), benchmark::Counter::kIsIterationInvariantRate |
benchmark::Counter::kInvert);
state.counters["speed"] = benchmark::Counter(
inputs_total_byte, benchmark::Counter::kIsIterationInvariantRate);
state.counters["url/s"] = benchmark::Counter(
double(std::size(inputs)), benchmark::Counter::kIsIterationInvariantRate);
}
BENCHMARK(Icu);
bool verify() {
bool is_ok = true;
for (std::string& url_string : inputs) {
std::string icu_answer = icu_to_ascii(url_string);
std::string ada_answer = ada::idna::to_ascii(url_string);
if (icu_answer != ada_answer) {
std::cerr << " ada/icu mismatch " << ada_answer << " vs. " << icu_answer
<< std::endl;
is_ok = false;
}
}
if (!is_ok) {
std::cout << "\n\n\nWarning: errors found.\n\n\n\n";
} else {
std::cout << "ICU and ada/idna agree on all test inputs.\n";
}
return is_ok;
}
#endif
int main(int argc, char** argv) {
#if ICU_AVAILABLE
verify();
#endif
#if (__APPLE__ && __aarch64__) || defined(__linux__)
if (!collector.has_events()) {
benchmark::AddCustomContext("performance counters",
"No privileged access (sudo may help).");
}
#else
if (!collector.has_events()) {
benchmark::AddCustomContext("performance counters", "Unsupported system.");
}
#endif
benchmark::AddCustomContext("input bytes",
std::to_string(size_t(inputs_total_byte)));
benchmark::AddCustomContext("number of domains",
std::to_string(std::size(inputs)));
benchmark::AddCustomContext(
"bytes/domains", std::to_string(inputs_total_byte / std::size(inputs)));
if (collector.has_events()) {
benchmark::AddCustomContext("performance counters", "Enabled");
}
benchmark::Initialize(&argc, argv);
benchmark::RunSpecifiedBenchmarks();
benchmark::Shutdown();
}