mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-11-24 15:02:44 +00:00
Previously, small-enough integer literals went through a code path that still built an `APInt`, even though we don't store that in the `IntStore` and instead embed the integer into its ID. The APInt construction as well as the API design ended up costing _significant_ performance in lexing for common cases of decimal integer literals. This PR makes three fundamental changes: 1. It redesigns the API to minimize how many object addresses are escaped to memory across out-of-line boundaries, and to allow bypassing any `APInt` construction by switching from computing and returning the value from the literal lexer to having the literal lexer add the value to the relevant value store if needed and return the ID instead. This allows the fast path of an integer embedded into the ID to actually be fast. It also allows easily making that fast path itself be inlinable while the complex general cases are kept out-of-line to manage register pressure and make inlining profitable across the board. 2. It adds an inlinable _very_ fast path for single-digit integer literals that helps handle particularly common cases. We can do this without creating problematic register pressure for the rest of the lexer which is why it is left inlinable. 3. It adds an out-of-line fast path for simple decimal integer literals. We actually had a fast path of parsing these already, but we still built `APInt`s around them. This makes parallel fast paths to both identify simple integer literals as well as turning them directly into an integer-embedded ID when they fit. Beyond these changes, the PR makes a number of minor changes to help cause them to all play together effectively: refactorings to support all of the above larger changes, and force inlining to remove calls from hot paths. Lex-phase benchmark time, cycles, and instructions per file, by number of lines, comparing this change against trunk: | Benchmark | Metric | 4K | 16K | 64K | 256K | | ---------------------- | ------------ | -----: | -----: | -----: | -----: | | ApiFileDenseDecls | CPU time | -2.1% | -3.1% | -3.3% | ~ | | | Cycles | -2.8% | -3.5% | -3.6% | ~ | | | Instructions | -2.9% | -3.9% | -4.3% | -4.4% | | ImplFile | CPU time | -9.8% | -14.7% | -16.7% | -15.9% | | | Cycles | -11.6% | -15.5% | -16.9% | -15.8% | | | Instructions | -15.8% | -19.1% | -20.3% | -20.4% | | ApiFileRealisticHeader | CPU time | -4.6% | -7.6% | -8.7% | -5.9% | | | Cycles | -5.8% | -7.9% | -9.0% | -6.0% | | | Instructions | -7.8% | -10.0% | -10.7% | -10.9% | Assisted-by: Antigravity with various models --------- Co-authored-by: Dana Jansens <danakj@orodu.net>
130 lines
4.5 KiB
C++
130 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 <string>
|
|
|
|
#include "toolchain/base/shared_value_stores.h"
|
|
#include "toolchain/diagnostics/null_diagnostics.h"
|
|
#include "toolchain/lex/numeric_literal.h"
|
|
|
|
namespace Carbon::Lex {
|
|
namespace {
|
|
|
|
// Returns an integer literal string with `prefix` followed by `num_digits`
|
|
// entries from `digits` (repeating `digits` as necessary).
|
|
static auto MakeIntString(llvm::StringLiteral prefix, int radix, int num_digits)
|
|
-> std::string {
|
|
// Digits are reversed so that we can take `radix` digits from the end, and
|
|
// never have 0 be the first digit.
|
|
static constexpr llvm::StringLiteral Digits = "FEDCBA9876543210";
|
|
|
|
std::string s;
|
|
s.reserve(prefix.size() + num_digits);
|
|
s.append(prefix);
|
|
for (int i = 0; i < num_digits; i += radix) {
|
|
s.append(Digits.take_back(std::min(radix, num_digits - i)));
|
|
}
|
|
return s;
|
|
}
|
|
|
|
static void BM_Lex_Float(benchmark::State& state) {
|
|
for (auto _ : state) {
|
|
benchmark::DoNotOptimize(
|
|
NumericLiteral::Lex("0.000001", [] { return true; }));
|
|
}
|
|
}
|
|
|
|
static void BM_Lex_Int(benchmark::State& state) {
|
|
for (auto _ : state) {
|
|
benchmark::DoNotOptimize(
|
|
NumericLiteral::Lex("1_234_567_890", [] { return true; }));
|
|
}
|
|
}
|
|
|
|
static void BM_Lex_IntDecimalN(benchmark::State& state) {
|
|
std::string s = MakeIntString("", 10, state.range(0));
|
|
for (auto _ : state) {
|
|
benchmark::DoNotOptimize(NumericLiteral::Lex(s, [] { return true; }));
|
|
}
|
|
}
|
|
|
|
static void BM_AddValue_Float(benchmark::State& state) {
|
|
auto val = NumericLiteral::Lex("0.000001", [] { return true; });
|
|
auto& emitter = Diagnostics::NullEmitter<Lex::SourceLoc>();
|
|
// Real values aren't canonicalized, so each one grows the value store. Use a
|
|
// fresh store for each batch of values to bound its size.
|
|
constexpr int BatchSize = 1000;
|
|
while (state.KeepRunningBatch(BatchSize)) {
|
|
SharedValueStores value_stores;
|
|
for (int i = 0; i < BatchSize; ++i) {
|
|
benchmark::DoNotOptimize(val.AddValue(value_stores, emitter));
|
|
}
|
|
}
|
|
}
|
|
|
|
static void BM_AddValue_Int(benchmark::State& state) {
|
|
auto val = NumericLiteral::Lex("1_234_567_890", [] { return true; });
|
|
auto& emitter = Diagnostics::NullEmitter<Lex::SourceLoc>();
|
|
SharedValueStores value_stores;
|
|
for (auto _ : state) {
|
|
benchmark::DoNotOptimize(val.AddValue(value_stores, emitter));
|
|
}
|
|
}
|
|
|
|
static void BM_AddValue_IntDecimalN(benchmark::State& state) {
|
|
std::string s = MakeIntString("", 10, state.range(0));
|
|
auto val = NumericLiteral::Lex(s, [] { return true; });
|
|
auto& emitter = Diagnostics::NullEmitter<Lex::SourceLoc>();
|
|
SharedValueStores value_stores;
|
|
for (auto _ : state) {
|
|
benchmark::DoNotOptimize(val.AddValue(value_stores, emitter));
|
|
}
|
|
}
|
|
|
|
static void BM_AddValue_IntBinaryN(benchmark::State& state) {
|
|
std::string s = MakeIntString("0b", 2, state.range(0));
|
|
auto val = NumericLiteral::Lex(s, [] { return true; });
|
|
auto& emitter = Diagnostics::NullEmitter<Lex::SourceLoc>();
|
|
SharedValueStores value_stores;
|
|
for (auto _ : state) {
|
|
benchmark::DoNotOptimize(val.AddValue(value_stores, emitter));
|
|
}
|
|
}
|
|
|
|
static void BM_AddValue_IntOctalN(benchmark::State& state) {
|
|
std::string s = MakeIntString("0o", 8, state.range(0));
|
|
auto val = NumericLiteral::Lex(s, [] { return true; });
|
|
auto& emitter = Diagnostics::NullEmitter<Lex::SourceLoc>();
|
|
SharedValueStores value_stores;
|
|
for (auto _ : state) {
|
|
benchmark::DoNotOptimize(val.AddValue(value_stores, emitter));
|
|
}
|
|
}
|
|
|
|
static void BM_AddValue_IntHexN(benchmark::State& state) {
|
|
// 0 is in the middle so that it isn't truncated in parse.
|
|
std::string s = MakeIntString("0x", 16, state.range(0));
|
|
auto val = NumericLiteral::Lex(s, [] { return true; });
|
|
auto& emitter = Diagnostics::NullEmitter<Lex::SourceLoc>();
|
|
SharedValueStores value_stores;
|
|
for (auto _ : state) {
|
|
benchmark::DoNotOptimize(val.AddValue(value_stores, emitter));
|
|
}
|
|
}
|
|
|
|
BENCHMARK(BM_Lex_Float);
|
|
BENCHMARK(BM_Lex_Int);
|
|
BENCHMARK(BM_Lex_IntDecimalN)->RangeMultiplier(10)->Range(1, 10000);
|
|
BENCHMARK(BM_AddValue_Float);
|
|
BENCHMARK(BM_AddValue_Int);
|
|
BENCHMARK(BM_AddValue_IntDecimalN)->RangeMultiplier(10)->Range(1, 10000);
|
|
BENCHMARK(BM_AddValue_IntBinaryN)->RangeMultiplier(10)->Range(1, 10000);
|
|
BENCHMARK(BM_AddValue_IntOctalN)->RangeMultiplier(10)->Range(1, 10000);
|
|
BENCHMARK(BM_AddValue_IntHexN)->RangeMultiplier(10)->Range(1, 10000);
|
|
|
|
} // namespace
|
|
} // namespace Carbon::Lex
|