mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-11-24 15:12:43 +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>