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