Add fast paths for lexing simple decimal integer literals (#8115)

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>
This commit is contained in:
Chandler CarruthandDana Jansens authored and GitHub committed 2026-11-17 08:41:53 +00:00
1 parent 645e784aa0
commit 4e830a085e
8 files changed
+384 -198

No files matched your search

+4 -5
View File
@@ -78,11 +78,10 @@ auto TokenizedBuffer::GetTokenText(TokenIndex token) const -> llvm::StringRef {
// separators the author included.
if (token_info.kind() == TokenKind::IntLiteral ||
token_info.kind() == TokenKind::RealLiteral) {
std::optional<NumericLiteral> relexed_token =
NumericLiteral::Lex(source_->text().substr(token_info.byte_offset()),
token_info.kind() == TokenKind::RealLiteral);
CARBON_CHECK(relexed_token, "Could not reform numeric literal token.");
return relexed_token->text();
return NumericLiteral::Lex(
source_->text().substr(token_info.byte_offset()),
[&] { return token_info.kind() == TokenKind::RealLiteral; })
.text();
}
// Refer back to the source text to find the original spelling, including