| 94 | } |
| 95 | |
| 96 | void BM_ReplaceNew(benchmark::State& state) { |
| 97 | const ReplaceCase& c = Cases()[state.range(0)]; |
| 98 | auto flen = static_cast<int>(strlen(c.from)); |
| 99 | auto olen = static_cast<int>(strlen(c.to)); |
| 100 | std::string text = MakeText(c.text_len, c.stride, c.from[0], 'x'); |
| 101 | auto tlen = static_cast<int32_t>(text.size()); |
| 102 | ExecutionContext ctx; |
| 103 | auto ctx_ptr = reinterpret_cast<int64_t>(&ctx); |
| 104 | |
| 105 | // One warm-up call doubling as a correctness guard. |
| 106 | int32_t out_len = 0; |
| 107 | replace_utf8_utf8_utf8(ctx_ptr, text.data(), tlen, c.from, flen, c.to, olen, &out_len); |
| 108 | if (ctx.has_error()) { |
| 109 | state.SkipWithError(ctx.get_error().c_str()); |
| 110 | return; |
| 111 | } |
| 112 | |
| 113 | for (auto _ : state) { |
| 114 | ctx.Reset(); |
| 115 | const char* out = replace_utf8_utf8_utf8(ctx_ptr, text.data(), tlen, c.from, flen, |
| 116 | c.to, olen, &out_len); |
| 117 | benchmark::DoNotOptimize(out); |
| 118 | benchmark::DoNotOptimize(out_len); |
| 119 | } |
| 120 | state.SetBytesProcessed(state.iterations() * tlen); |
| 121 | state.SetLabel(c.name); |
| 122 | } |
| 123 | |
| 124 | void BM_ReplaceOld(benchmark::State& state) { |
| 125 | const ReplaceCase& c = Cases()[state.range(0)]; |