Files
carbon-lang/toolchain/lex/string_literal_benchmark.cpp
T
Jon Ross-Perkins acbe6530c3 Move diagnostics into a namespace (#5173)
What this really does is avoids shadowing names, so that we can
comfortable have things like `Check::DiagnosticEmitter` or
`Check::DiagnosticLoc` without shadowing being a concern.

Note, down this path I'm also thinking about:

- Renaming misc DiagnosticConsumer/DiagnosticEmitter classes, possibly
just to DiagnosticConsumer/DiagnosticEmitter (so
`Check::DiagnosticEmitter` instead of `SemIRLocDiagnosticEmitter`).
- Dropping `Diagnostic` from `Emitter::DiagnosticBuilder`.
- But not for `Check::DiagnosticBuilder`, because `Check::Builder` would
be ambiguous.
- Renaming diagnostics/diagnostic_* to drop "diagnostic".

[Discussion about SemIRLoc ->
DiagnosticLoc](https://discord.com/channels/655572317891461132/655578254970716160/1353771570463768698)
reminded me of this (in particular the older [Check::DiagnosticBuilder
discussion](https://discord.com/channels/655572317891461132/655578254970716160/1344363562608627763)),
but I'd only do that rename if there's matching consensus about a path
forward where we keep SemIRLoc, and in a way that it's only ever used
for diagnostics (the divergence from which is at the root of current
LocId discussion).

I'm trying to keep that separate from a namespace addition for clarity.
2025-03-26 19:12:10 +00:00

143 lines
4.5 KiB
C++

// Part of the Carbon Language project, under the Apache License v2.0 with LLVM
// Exceptions. See /LICENSE for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
#include <benchmark/benchmark.h>
#include "toolchain/diagnostics/null_diagnostics.h"
#include "toolchain/lex/string_literal.h"
namespace Carbon::Lex {
namespace {
static void BM_ValidString(benchmark::State& state, std::string_view introducer,
std::string_view terminator) {
std::string x(introducer);
x.append(100000, 'a');
x.append(terminator);
for (auto _ : state) {
StringLiteral::Lex(x);
}
}
static void BM_ValidString_Simple(benchmark::State& state) {
BM_ValidString(state, "\"", "\"");
}
static void BM_ValidString_Multiline(benchmark::State& state) {
BM_ValidString(state, "'''\n", "\n'''");
}
static void BM_ValidString_MultilineDoubleQuote(benchmark::State& state) {
BM_ValidString(state, "\"\"\"\n", "\n\"\"\"");
}
static void BM_ValidString_Raw(benchmark::State& state) {
BM_ValidString(state, "#\"", "\"#");
}
BENCHMARK(BM_ValidString_Simple);
BENCHMARK(BM_ValidString_Multiline);
BENCHMARK(BM_ValidString_MultilineDoubleQuote);
BENCHMARK(BM_ValidString_Raw);
static void BM_IncompleteWithRepeatedEscapes(benchmark::State& state,
std::string_view introducer,
std::string_view escape) {
std::string x(introducer);
// Aim for about 100k to emphasize escape parsing issues.
while (x.size() < 100000) {
x.append("key: ");
x.append(escape);
x.append("\"");
x.append(escape);
x.append("\"");
x.append(escape);
x.append("n ");
}
for (auto _ : state) {
StringLiteral::Lex(x);
}
}
static void BM_IncompleteWithEscapes_Simple(benchmark::State& state) {
BM_IncompleteWithRepeatedEscapes(state, "\"", "\\");
}
static void BM_IncompleteWithEscapes_Multiline(benchmark::State& state) {
BM_IncompleteWithRepeatedEscapes(state, "'''\n", "\\");
}
static void BM_IncompleteWithEscapes_MultilineDoubleQuote(
benchmark::State& state) {
BM_IncompleteWithRepeatedEscapes(state, "\"\"\"\n", "\\");
}
static void BM_IncompleteWithEscapes_Raw(benchmark::State& state) {
BM_IncompleteWithRepeatedEscapes(state, "#\"", "\\#");
}
BENCHMARK(BM_IncompleteWithEscapes_Simple);
BENCHMARK(BM_IncompleteWithEscapes_Multiline);
BENCHMARK(BM_IncompleteWithEscapes_MultilineDoubleQuote);
BENCHMARK(BM_IncompleteWithEscapes_Raw);
static void BM_SimpleStringValue(benchmark::State& state, int size,
std::string_view introducer, bool add_escape,
std::string_view terminator) {
llvm::BumpPtrAllocator allocator;
std::string x(introducer);
x.append(size, 'a');
if (add_escape) {
// Adds a basic escape that forces ComputeValue to generate a new string.
x.append("\\\\");
}
x.append(terminator);
for (auto _ : state) {
StringLiteral::Lex(x)->ComputeValue(
allocator, Diagnostics::NullEmitter<const char*>());
}
}
static void BM_ComputeValue_NoGenerate_Short(benchmark::State& state) {
BM_SimpleStringValue(state, 10, "\"", /*add_escape=*/false, "\"");
}
static void BM_ComputeValue_NoGenerate_Long(benchmark::State& state) {
BM_SimpleStringValue(state, 10000, "\"", /*add_escape=*/false, "\"");
}
static void BM_ComputeValue_WillGenerate_Short(benchmark::State& state) {
BM_SimpleStringValue(state, 10, "\"", /*add_escape=*/true, "\"");
}
static void BM_ComputeValue_WillGenerate_Long(benchmark::State& state) {
BM_SimpleStringValue(state, 10000, "\"", /*add_escape=*/true, "\"");
}
static void BM_ComputeValue_WillGenerate_Multiline(benchmark::State& state) {
BM_SimpleStringValue(state, 10000, "'''\n", /*add_escape=*/true, "\n'''");
}
static void BM_ComputeValue_WillGenerate_MultilineDoubleQuote(
benchmark::State& state) {
BM_SimpleStringValue(state, 10000, "\"\"\"\n", /*add_escape=*/true,
"\n\"\"\"");
}
static void BM_ComputeValue_WillGenerate_Raw(benchmark::State& state) {
BM_SimpleStringValue(state, 10000, "#\"", /*add_escape=*/true, "\"#");
}
BENCHMARK(BM_ComputeValue_NoGenerate_Short);
BENCHMARK(BM_ComputeValue_NoGenerate_Long);
BENCHMARK(BM_ComputeValue_WillGenerate_Short);
BENCHMARK(BM_ComputeValue_WillGenerate_Long);
BENCHMARK(BM_ComputeValue_WillGenerate_Multiline);
BENCHMARK(BM_ComputeValue_WillGenerate_MultilineDoubleQuote);
BENCHMARK(BM_ComputeValue_WillGenerate_Raw);
} // namespace
} // namespace Carbon::Lex