Commit Graph
30 Commits
Author SHA1 Message Date
Jon Ross-Perkins 6c458ffe7e Add import context for locations. (#3807)
As discussed around #3792, identify the import a diagnostic message came
from prior to the diagnostic message itself. This occurs during location
translation so that the logic can be central.

I'd considered associating the parse node with ImportRef instructions,
but I realized about halfway through that because I need to store the
ImportDirectiveId on the ImportIR for cross-package imports, it's there
for use in location translation without extra work. That saves a fair
amount of stringing it through declarations, as well as an oddity where
ImportRef instructions would have a node that didn't really represent
them.
2024-03-27 22:22:15 +00:00
Jon Ross-Perkins 0bd45f0d6b Rename DiagnosticLocationTranslator -> DiagnosticConverter (#3804)
Since the addition of TranslateArg, I don't think this type is going to
go away (cutting a TODO). Refactoring names slightly to fit the current
role, and adding const to ConvertLocation.
2024-03-21 23:35:04 +00:00
Jon Ross-Perkins 4421a75c36 file_name -> filename (#3791)
I wanted to choose one or the other. I think some code has been using
each from early on. We're predominately using `filename`, so
consolidating on that. This conveniently matches the [Google dev doc
style guide](https://developers.google.com/style/word-list#filename)
(which we use for docs) which says "filename: Not file name".

In toolchain:

```
╚╡git grep file_name . | wc -l
35
╚╡git grep filename . | wc -l
489
```
2024-03-18 17:26:24 +00:00
cui fliter fea2651e7c chore: fix typos (#3738)
Signed-off-by: cui fliter <imcusg@gmail.com>
2024-03-04 16:27:31 +00:00
Richard Smith 0a06fceb5f Improve diagnosis of mismatched brackets. (#3282)
Move handling of mismatched brackets out of the main lexing loop into a
separate pass that is only run if there are mismatched brackets This is
done in preparation for using both lookahead and lookbehind to work out
how to match brackets, and to get this code far away from the hot lexing
loop.

Fix bracket insertion location to be immediately after the token that
we're inserting the bracket after, rather than potentially at the end of
a comment. When there are open brackets at the end of the file, say that
there are open brackets, not that there's a closing bracket without a
matching opening bracket.
2023-12-21 08:49:37 +00:00
Richard Smith d87fe8b532 Rename Carbon::StringLiteralId -> Carbon::StringLiteralValueId. (#3522)
We have `StringLiteral`s in multiple other `Carbon` sub-namespaces.
Rename to a more specific name to avoid collisions.

We should likely also rename `Carbon::IntId` -> `Carbon::IntValueId` and
`Carbon::RealId` -> `Carbon::RealValueId`, but this collision is
prioritized because it was blocking work on typed parse nodes which
introduces a `Carbon::Parse::StringLiteralId`.
2023-12-18 23:27:33 +00:00
Jon Ross-Perkins 0db63ff17a Abbreviate Integer and FloatingPoint (#3435)
I was suggesting this because `FloatingPoint` is pretty long. `int` and
`float` should be familiar abbreviations. `unsigned` should be familiar
to developers too, but `UnsignedInt` still feels usefully clearer for
the additional chars.
2023-11-29 23:29:48 +00:00
Richard Smith eae630a3db Rename Lex::{Token,Line} -> Lex::{Token,Line}Index. (#3433)
As discussed [on
discord](https://discord.com/channels/655572317891461132/655578254970716160/1178878128714678282)
and today's toolchain discussion.
2023-11-29 20:33:58 +00:00
Jon Ross-Perkins 3f208e27f9 Align on FileStart/FileEnd for naming. (#3428)
The lexer has been using EndOfFile form (stemming from EOF), parser went
to FileEnd form. This consolidates on FileEnd form.
2023-11-29 16:36:57 +00:00
Jacob Schneider 1d443a3617 Underline the entire token when producing diagnostic messages. (#3413)
This is an incremental improvement on our diagnostic messages that
simply underlines an entire token if the token is larger than 1 char
(else it points to the single char with a caret like it used to).
2023-11-27 16:36:26 +00:00
Jon Ross-PerkinsandChandler Carruth cafcd88882 Split lexing logic and storage to separate files. (#3365)
Just reorganizing logic a little, trying to mirror the direction we've
gone with check, lower, etc. That is, lex.h contains a function `Lex`
that is used directly.

Note, I'm avoiding making meaningful changes here. It could in theory
still affect inlining in benchmarks, but I'm not seeing an impact.

Before:

```
------------------------------------------------------------------------------------------------------
Benchmark                                            Time             CPU   Iterations UserCounters...
------------------------------------------------------------------------------------------------------
BM_ValidKeywords                               2784949 ns      2784867 ns          249 bytes_per_second=214.452M/s tokens_per_second=35.9084M/s
BM_ValidKeywordsAsRawIdentifiers               3222597 ns      3222551 ns          210 bytes_per_second=244.513M/s tokens_per_second=31.0313M/s
BM_RawIdentifierFocus                          5907836 ns      5907518 ns          103 bytes_per_second=264.873M/s tokens_per_second=16.9276M/s
BM_ValidIdentifiers<1, 64, false>              6255128 ns      6254297 ns          105 bytes_per_second=235.488M/s tokens_per_second=15.989M/s
BM_ValidIdentifiers<1, 1, true>                3677630 ns      3677398 ns          192 bytes_per_second=77.7999M/s tokens_per_second=27.1931M/s
BM_ValidIdentifiers<3, 5, true>                5427693 ns      5427116 ns          110 bytes_per_second=105.434M/s tokens_per_second=18.426M/s
BM_ValidIdentifiers<3, 16, true>               5063246 ns      5062761 ns          115 bytes_per_second=216.623M/s tokens_per_second=19.7521M/s
BM_ValidIdentifiers<12, 64, true>              5518589 ns      5518118 ns          100 bytes_per_second=691.264M/s tokens_per_second=18.1221M/s
BM_ValidIdentifiers<16, 16, true>              4890776 ns      4890782 ns          112 bytes_per_second=350.989M/s tokens_per_second=20.4466M/s
BM_ValidIdentifiers<24, 24, true>              4974729 ns      4974582 ns          112 bytes_per_second=498.444M/s tokens_per_second=20.1022M/s
BM_ValidIdentifiers<32, 32, true>              5517583 ns      5517085 ns           99 bytes_per_second=587.718M/s tokens_per_second=18.1255M/s
BM_ValidIdentifiers<48, 48, true>              5914759 ns      5914222 ns           94 bytes_per_second=806.255M/s tokens_per_second=16.9084M/s
BM_ValidIdentifiers<64, 64, true>              7556040 ns      7556036 ns           77 bytes_per_second=833.009M/s tokens_per_second=13.2345M/s
BM_ValidIdentifiers<80, 80, true>              7739113 ns      7737696 ns           76 bytes_per_second=1010.65M/s tokens_per_second=12.9237M/s
BM_HorizontalWhitespace/1                      5015062 ns      5014443 ns          108 bytes_per_second=114.111M/s tokens_per_second=19.9424M/s
BM_HorizontalWhitespace/4                      5165496 ns      5165425 ns          111 bytes_per_second=166.163M/s tokens_per_second=19.3595M/s
BM_HorizontalWhitespace/16                     5616796 ns      5616447 ns          102 bytes_per_second=356.578M/s tokens_per_second=17.8049M/s
BM_HorizontalWhitespace/64                     7912904 ns      7912346 ns           78 bytes_per_second=831.648M/s tokens_per_second=12.6385M/s
BM_HorizontalWhitespace/128                   11086218 ns     11083155 ns           57 bytes_per_second=1.11759G/s tokens_per_second=9.0227M/s
BM_RandomSource                                4796549 ns      4795733 ns          145 bytes_per_second=216.783M/s lines_per_second=6.61943M/s tokens_per_second=20.8519M/s
BM_GroupingSymbols/1/0/0                       3937151 ns      3936581 ns          176 bytes_per_second=216.914M/s lines_per_second=19.0521M/s tokens_per_second=25.4028M/s
BM_GroupingSymbols/2/0/0                       3000029 ns      2999506 ns          239 bytes_per_second=243.125M/s lines_per_second=27.7812M/s tokens_per_second=33.3388M/s
BM_GroupingSymbols/3/0/0                       2729059 ns      2728834 ns          251 bytes_per_second=261.26M/s lines_per_second=32.065M/s tokens_per_second=36.6457M/s
BM_GroupingSymbols/4/0/0                       2432363 ns      2432209 ns          291 bytes_per_second=304.738M/s lines_per_second=37.0034M/s tokens_per_second=41.1149M/s
BM_GroupingSymbols/8/0/0                       2144080 ns      2143967 ns          326 bytes_per_second=468.547M/s lines_per_second=44.0468M/s tokens_per_second=46.6425M/s
BM_GroupingSymbols/16/0/0                      2290055 ns      2289711 ns          308 bytes_per_second=741.709M/s lines_per_second=42.3866M/s tokens_per_second=43.6736M/s
BM_GroupingSymbols/32/0/0                      3102790 ns      3102186 ns          220 bytes_per_second=1027.1M/s lines_per_second=31.7437M/s tokens_per_second=32.2353M/s
BM_GroupingSymbols/0/1/0                       3406964 ns      3406421 ns          207 bytes_per_second=222.677M/s lines_per_second=7.33908M/s tokens_per_second=29.3563M/s
BM_GroupingSymbols/0/2/0                       2244101 ns      2244006 ns          312 bytes_per_second=239.985M/s lines_per_second=7.42689M/s tokens_per_second=44.5632M/s
BM_GroupingSymbols/0/3/0                       1976449 ns      1976080 ns          347 bytes_per_second=216M/s lines_per_second=6.32566M/s tokens_per_second=50.6052M/s
BM_GroupingSymbols/0/4/0                       1600539 ns      1600325 ns          429 bytes_per_second=224.777M/s lines_per_second=6.24873M/s tokens_per_second=62.4873M/s
BM_GroupingSymbols/0/8/0                       1085044 ns      1084941 ns          646 bytes_per_second=222.765M/s lines_per_second=5.12009M/s tokens_per_second=92.1709M/s
BM_GroupingSymbols/0/16/0                       824804 ns       824756 ns          817 bytes_per_second=209.166M/s lines_per_second=3.5659M/s tokens_per_second=121.248M/s
BM_GroupingSymbols/0/32/0                       685741 ns       685741 ns         1004 bytes_per_second=196.576M/s lines_per_second=2.20929M/s tokens_per_second=145.828M/s
BM_GroupingSymbols/0/0/1                       3467507 ns      3467077 ns          206 bytes_per_second=218.781M/s lines_per_second=7.21069M/s tokens_per_second=28.8427M/s
BM_GroupingSymbols/0/0/2                       2284154 ns      2283937 ns          309 bytes_per_second=235.79M/s lines_per_second=7.29705M/s tokens_per_second=43.784M/s
BM_GroupingSymbols/0/0/3                       1965548 ns      1965225 ns          356 bytes_per_second=217.193M/s lines_per_second=6.36059M/s tokens_per_second=50.8848M/s
BM_GroupingSymbols/0/0/4                       1623965 ns      1623725 ns          440 bytes_per_second=221.538M/s lines_per_second=6.15868M/s tokens_per_second=61.5868M/s
BM_GroupingSymbols/0/0/8                       1080601 ns      1080452 ns          650 bytes_per_second=223.691M/s lines_per_second=5.14137M/s tokens_per_second=92.5539M/s
BM_GroupingSymbols/0/0/16                       840820 ns       840677 ns          828 bytes_per_second=205.205M/s lines_per_second=3.49837M/s tokens_per_second=118.952M/s
BM_GroupingSymbols/0/0/32                       707793 ns       707734 ns          991 bytes_per_second=190.467M/s lines_per_second=2.14063M/s tokens_per_second=141.296M/s
BM_GroupingSymbols/32/1/0                      2804159 ns      2803869 ns          245 bytes_per_second=1103.7M/s lines_per_second=34.0779M/s tokens_per_second=35.665M/s
BM_GroupingSymbols/32/2/0                      2676229 ns      2675855 ns          261 bytes_per_second=1123.78M/s lines_per_second=34.688M/s tokens_per_second=37.3712M/s
BM_GroupingSymbols/32/3/0                      2652102 ns      2651836 ns          262 bytes_per_second=1103.24M/s lines_per_second=34.0217M/s tokens_per_second=37.7097M/s
BM_GroupingSymbols/32/4/0                      2600677 ns      2600552 ns          268 bytes_per_second=1096.17M/s lines_per_second=33.7678M/s tokens_per_second=38.4534M/s
BM_GroupingSymbols/32/8/0                      2382017 ns      2381869 ns          294 bytes_per_second=1083.42M/s lines_per_second=33.2659M/s tokens_per_second=41.9838M/s
BM_GroupingSymbols/32/16/0                     2102340 ns      2102243 ns          325 bytes_per_second=1034.45M/s lines_per_second=31.5377M/s tokens_per_second=47.5682M/s
BM_GroupingSymbols/32/32/0                     1745079 ns      1744914 ns          403 bytes_per_second=952.64M/s lines_per_second=28.6461M/s tokens_per_second=57.3094M/s
BM_GroupingSymbols/32/32/1                     1701414 ns      1701255 ns          415 bytes_per_second=962.73M/s lines_per_second=28.9228M/s tokens_per_second=58.7802M/s
BM_GroupingSymbols/32/32/2                     1688503 ns      1688121 ns          415 bytes_per_second=957.019M/s lines_per_second=28.7242M/s tokens_per_second=59.2375M/s
BM_GroupingSymbols/32/32/3                     1679701 ns      1679499 ns          419 bytes_per_second=948.651M/s lines_per_second=28.446M/s tokens_per_second=59.5416M/s
BM_GroupingSymbols/32/32/4                     1647815 ns      1647816 ns          427 bytes_per_second=953.342M/s lines_per_second=28.559M/s tokens_per_second=60.6864M/s
BM_GroupingSymbols/32/32/8                     1581283 ns      1581075 ns          450 bytes_per_second=942.39M/s lines_per_second=28.1201M/s tokens_per_second=63.2481M/s
BM_GroupingSymbols/32/32/16                    1445591 ns      1445526 ns          477 bytes_per_second=936.105M/s lines_per_second=27.7442M/s tokens_per_second=69.179M/s
BM_GroupingSymbols/32/32/32                    1296335 ns      1296094 ns          544 bytes_per_second=882.943M/s lines_per_second=25.8276M/s tokens_per_second=77.1549M/s
BM_BlankLines/1                                5774442 ns      5774454 ns          107 bytes_per_second=99.0921M/s lines_per_second=17.3175M/s tokens_per_second=17.3177M/s
BM_BlankLines/4                                8712135 ns      8711977 ns           75 bytes_per_second=98.5198M/s lines_per_second=45.9133M/s tokens_per_second=11.4785M/s
BM_BlankLines/16                              32313782 ns     32307655 ns           22 bytes_per_second=61.9884M/s lines_per_second=49.5234M/s tokens_per_second=3.09524M/s
BM_BlankLines/64                              87562163 ns     87543476 ns            7 bytes_per_second=75.166M/s lines_per_second=73.1058M/s tokens_per_second=1.14229M/s
BM_BlankLines/128                            160336428 ns    160298644 ns            4 bytes_per_second=79.1257M/s lines_per_second=79.8502M/s tokens_per_second=623.836k/s
BM_CommentLines/1/0/0                          7298959 ns      7298427 ns           89 bytes_per_second=117.601M/s lines_per_second=27.4029M/s tokens_per_second=13.7016M/s
BM_CommentLines/4/0/0                         10002223 ns     10002239 ns           67 bytes_per_second=171.622M/s lines_per_second=49.9883M/s tokens_per_second=9.99776M/s
BM_CommentLines/128/0/0                      143356660 ns    143356412 ns            5 bytes_per_second=259.444M/s lines_per_second=89.9846M/s tokens_per_second=697.562k/s
BM_CommentLines/1/30/0                         7421994 ns      7421289 ns           87 bytes_per_second=501.166M/s lines_per_second=26.9492M/s tokens_per_second=13.4747M/s
BM_CommentLines/4/30/0                        11304903 ns     11303972 ns           56 bytes_per_second=1.13696G/s lines_per_second=44.2318M/s tokens_per_second=8.84645M/s
BM_CommentLines/128/30/0                     156244144 ns    156217089 ns            4 bytes_per_second=2.52178G/s lines_per_second=82.5766M/s tokens_per_second=640.135k/s
BM_CommentLines/1/70/0                         7486688 ns      7485340 ns           80 bytes_per_second=1006.49M/s lines_per_second=26.7186M/s tokens_per_second=13.3594M/s
BM_CommentLines/4/70/0                        14762881 ns     14761417 ns           45 bytes_per_second=1.88011G/s lines_per_second=33.8717M/s tokens_per_second=6.77442M/s
BM_CommentLines/128/70/0                     179933089 ns    179903592 ns            4 bytes_per_second=4.84025G/s lines_per_second=71.7044M/s tokens_per_second=555.853k/s
BM_CommentLines/1/0/2                          7473119 ns      7472370 ns           87 bytes_per_second=140.389M/s lines_per_second=26.765M/s tokens_per_second=13.3826M/s
BM_CommentLines/4/0/2                          9702727 ns      9700268 ns           66 bytes_per_second=255.615M/s lines_per_second=51.5445M/s tokens_per_second=10.309M/s
BM_CommentLines/128/0/2                      140504132 ns    140480254 ns            5 bytes_per_second=438.544M/s lines_per_second=91.8269M/s tokens_per_second=711.844k/s
BM_CommentLines/1/30/2                         7530847 ns      7529227 ns           82 bytes_per_second=519.314M/s lines_per_second=26.5629M/s tokens_per_second=13.2816M/s
BM_CommentLines/4/30/2                        11646677 ns     11644972 ns           56 bytes_per_second=1.16764G/s lines_per_second=42.9366M/s tokens_per_second=8.5874M/s
BM_CommentLines/128/30/2                     161292392 ns    161273904 ns            4 bytes_per_second=2.59054G/s lines_per_second=79.9873M/s tokens_per_second=620.063k/s
BM_CommentLines/1/70/2                         7700751 ns      7700365 ns           83 bytes_per_second=1003.16M/s lines_per_second=25.9725M/s tokens_per_second=12.9864M/s
BM_CommentLines/4/70/2                        14133018 ns     14131523 ns           45 bytes_per_second=2.01664G/s lines_per_second=35.3815M/s tokens_per_second=7.07638M/s
BM_CommentLines/128/70/2                     179085026 ns    179057072 ns            4 bytes_per_second=4.99628G/s lines_per_second=72.0433M/s tokens_per_second=558.481k/s
BM_CommentLines/1/0/8                          7792060 ns      7791951 ns           83 bytes_per_second=208.065M/s lines_per_second=25.6673M/s tokens_per_second=12.8338M/s
BM_CommentLines/4/0/8                         10329194 ns     10329006 ns           61 bytes_per_second=461.644M/s lines_per_second=48.4069M/s tokens_per_second=9.68147M/s
BM_CommentLines/128/0/8                      140917902 ns    140917975 ns            5 bytes_per_second=956.927M/s lines_per_second=91.5417M/s tokens_per_second=709.633k/s
BM_CommentLines/1/30/8                         7813308 ns      7811702 ns           82 bytes_per_second=573.784M/s lines_per_second=25.6024M/s tokens_per_second=12.8013M/s
BM_CommentLines/4/30/8                        12052779 ns     12051440 ns           54 bytes_per_second=1.31373G/s lines_per_second=41.4884M/s tokens_per_second=8.29776M/s
BM_CommentLines/128/30/8                     163767409 ns    163753783 ns            4 bytes_per_second=2.9881G/s lines_per_second=78.776M/s tokens_per_second=610.673k/s
BM_CommentLines/1/70/8                         8188137 ns      8187614 ns           80 bytes_per_second=1013.35M/s lines_per_second=24.4269M/s tokens_per_second=12.2136M/s
BM_CommentLines/4/70/8                        15723650 ns     15721559 ns           43 bytes_per_second=1.95485G/s lines_per_second=31.8031M/s tokens_per_second=6.36069M/s
BM_CommentLines/128/70/8                     182579515 ns    182554119 ns            4 bytes_per_second=5.29237G/s lines_per_second=70.6633M/s tokens_per_second=547.783k/s
BM_SpeedOfLightStrCpy                            27009 ns        27006 ns        25887 bytes_per_second=37.594G/s lines_per_second=1.17547G/s tokens_per_second=3.70286G/s
BM_SpeedOfLightDispatch<1>                     1850860 ns      1850660 ns          381 bytes_per_second=561.764M/s lines_per_second=17.1533M/s tokens_per_second=54.0348M/s
BM_SpeedOfLightDispatch<2>                     1876473 ns      1876369 ns          347 bytes_per_second=554.067M/s lines_per_second=16.9183M/s tokens_per_second=53.2944M/s
BM_SpeedOfLightDispatch<4>                     2208441 ns      2208443 ns          314 bytes_per_second=470.755M/s lines_per_second=14.3744M/s tokens_per_second=45.2808M/s
BM_SpeedOfLightDispatch<8>                     2824174 ns      2824137 ns          250 bytes_per_second=368.125M/s lines_per_second=11.2406M/s tokens_per_second=35.409M/s
BM_SpeedOfLightDispatch<16>                    4306633 ns      4306325 ns          165 bytes_per_second=241.42M/s lines_per_second=7.37172M/s tokens_per_second=23.2217M/s
BM_SpeedOfLightDispatch<32>                    6300653 ns      6300102 ns          113 bytes_per_second=165.019M/s lines_per_second=5.03881M/s tokens_per_second=15.8728M/s
BM_SpeedOfLightDispatch<MaxDispatchTargets>    8879663 ns      8878510 ns           79 bytes_per_second=117.096M/s lines_per_second=3.57549M/s tokens_per_second=11.2632M/s
```

After:

```
------------------------------------------------------------------------------------------------------
Benchmark                                            Time             CPU   Iterations UserCounters...
------------------------------------------------------------------------------------------------------
BM_ValidKeywords                               2821833 ns      2821832 ns          247 bytes_per_second=211.642M/s tokens_per_second=35.438M/s
BM_ValidKeywordsAsRawIdentifiers               3204326 ns      3203964 ns          216 bytes_per_second=245.931M/s tokens_per_second=31.2113M/s
BM_RawIdentifierFocus                          6076723 ns      6076107 ns           98 bytes_per_second=257.524M/s tokens_per_second=16.4579M/s
BM_ValidIdentifiers<1, 64, false>              6303093 ns      6302632 ns          101 bytes_per_second=233.682M/s tokens_per_second=15.8664M/s
BM_ValidIdentifiers<1, 1, true>                3687668 ns      3687338 ns          194 bytes_per_second=77.5902M/s tokens_per_second=27.1198M/s
BM_ValidIdentifiers<3, 5, true>                5298920 ns      5298465 ns          106 bytes_per_second=107.994M/s tokens_per_second=18.8734M/s
BM_ValidIdentifiers<3, 16, true>               4880695 ns      4879704 ns          118 bytes_per_second=224.75M/s tokens_per_second=20.493M/s
BM_ValidIdentifiers<12, 64, true>              5413070 ns      5411832 ns          103 bytes_per_second=704.84M/s tokens_per_second=18.478M/s
BM_ValidIdentifiers<16, 16, true>              5052780 ns      5051309 ns          110 bytes_per_second=339.835M/s tokens_per_second=19.7968M/s
BM_ValidIdentifiers<24, 24, true>              5221580 ns      5220851 ns          104 bytes_per_second=474.933M/s tokens_per_second=19.154M/s
BM_ValidIdentifiers<32, 32, true>              5786811 ns      5786806 ns           99 bytes_per_second=560.325M/s tokens_per_second=17.2807M/s
BM_ValidIdentifiers<48, 48, true>              5959506 ns      5959502 ns           96 bytes_per_second=800.129M/s tokens_per_second=16.7799M/s
BM_ValidIdentifiers<64, 64, true>              7831469 ns      7830629 ns           75 bytes_per_second=803.799M/s tokens_per_second=12.7704M/s
BM_ValidIdentifiers<80, 80, true>              7905843 ns      7904727 ns           75 bytes_per_second=989.298M/s tokens_per_second=12.6507M/s
BM_HorizontalWhitespace/1                      5091171 ns      5090660 ns          109 bytes_per_second=112.402M/s tokens_per_second=19.6438M/s
BM_HorizontalWhitespace/4                      5020344 ns      5020344 ns          112 bytes_per_second=170.965M/s tokens_per_second=19.919M/s
BM_HorizontalWhitespace/16                     5846801 ns      5846459 ns           99 bytes_per_second=342.549M/s tokens_per_second=17.1044M/s
BM_HorizontalWhitespace/64                     7803183 ns      7802357 ns           78 bytes_per_second=843.372M/s tokens_per_second=12.8166M/s
BM_HorizontalWhitespace/128                   10600500 ns     10598602 ns           59 bytes_per_second=1.16869G/s tokens_per_second=9.43521M/s
BM_RandomSource                                4824062 ns      4823496 ns          139 bytes_per_second=215.536M/s lines_per_second=6.58133M/s tokens_per_second=20.7319M/s
BM_GroupingSymbols/1/0/0                       4116846 ns      4116549 ns          170 bytes_per_second=207.431M/s lines_per_second=18.2191M/s tokens_per_second=24.2922M/s
BM_GroupingSymbols/2/0/0                       3024336 ns      3024156 ns          236 bytes_per_second=241.144M/s lines_per_second=27.5548M/s tokens_per_second=33.0671M/s
BM_GroupingSymbols/3/0/0                       2789794 ns      2789256 ns          252 bytes_per_second=255.601M/s lines_per_second=31.3704M/s tokens_per_second=35.8519M/s
BM_GroupingSymbols/4/0/0                       2496498 ns      2496237 ns          283 bytes_per_second=296.921M/s lines_per_second=36.0543M/s tokens_per_second=40.0603M/s
BM_GroupingSymbols/8/0/0                       2200846 ns      2200611 ns          313 bytes_per_second=456.487M/s lines_per_second=42.9131M/s tokens_per_second=45.4419M/s
BM_GroupingSymbols/16/0/0                      2415237 ns      2415015 ns          288 bytes_per_second=703.225M/s lines_per_second=40.1873M/s tokens_per_second=41.4076M/s
BM_GroupingSymbols/32/0/0                      3171195 ns      3170504 ns          215 bytes_per_second=1004.97M/s lines_per_second=31.0597M/s tokens_per_second=31.5407M/s
BM_GroupingSymbols/0/1/0                       3627393 ns      3626737 ns          193 bytes_per_second=209.15M/s lines_per_second=6.89325M/s tokens_per_second=27.573M/s
BM_GroupingSymbols/0/2/0                       2501189 ns      2500946 ns          275 bytes_per_second=215.33M/s lines_per_second=6.66388M/s tokens_per_second=39.9849M/s
BM_GroupingSymbols/0/3/0                       2149513 ns      2149282 ns          318 bytes_per_second=198.593M/s lines_per_second=5.8159M/s tokens_per_second=46.5272M/s
BM_GroupingSymbols/0/4/0                       1793658 ns      1793292 ns          384 bytes_per_second=200.59M/s lines_per_second=5.57634M/s tokens_per_second=55.7634M/s
BM_GroupingSymbols/0/8/0                       1302555 ns      1302272 ns          542 bytes_per_second=185.589M/s lines_per_second=4.26562M/s tokens_per_second=76.7889M/s
BM_GroupingSymbols/0/16/0                      1042993 ns      1042818 ns          671 bytes_per_second=165.428M/s lines_per_second=2.82024M/s tokens_per_second=95.8941M/s
BM_GroupingSymbols/0/32/0                       955561 ns       955471 ns          749 bytes_per_second=141.082M/s lines_per_second=1.58561M/s tokens_per_second=104.66M/s
BM_GroupingSymbols/0/0/1                       3659797 ns      3659369 ns          194 bytes_per_second=207.285M/s lines_per_second=6.83178M/s tokens_per_second=27.3271M/s
BM_GroupingSymbols/0/0/2                       2467556 ns      2467190 ns          281 bytes_per_second=218.276M/s lines_per_second=6.75505M/s tokens_per_second=40.5319M/s
BM_GroupingSymbols/0/0/3                       2152274 ns      2151938 ns          326 bytes_per_second=198.348M/s lines_per_second=5.80872M/s tokens_per_second=46.4697M/s
BM_GroupingSymbols/0/0/4                       1805982 ns      1805877 ns          368 bytes_per_second=199.192M/s lines_per_second=5.53747M/s tokens_per_second=55.3747M/s
BM_GroupingSymbols/0/0/8                       1313041 ns      1312833 ns          539 bytes_per_second=184.096M/s lines_per_second=4.23131M/s tokens_per_second=76.1711M/s
BM_GroupingSymbols/0/0/16                      1065565 ns      1065301 ns          659 bytes_per_second=161.937M/s lines_per_second=2.76072M/s tokens_per_second=93.8702M/s
BM_GroupingSymbols/0/0/32                       946630 ns       946514 ns          726 bytes_per_second=142.417M/s lines_per_second=1.60061M/s tokens_per_second=105.651M/s
BM_GroupingSymbols/32/1/0                      2991120 ns      2991121 ns          233 bytes_per_second=1034.6M/s lines_per_second=31.9445M/s tokens_per_second=33.4323M/s
BM_GroupingSymbols/32/2/0                      2893280 ns      2892944 ns          245 bytes_per_second=1039.45M/s lines_per_second=32.085M/s tokens_per_second=34.5669M/s
BM_GroupingSymbols/32/3/0                      2819177 ns      2819024 ns          239 bytes_per_second=1037.81M/s lines_per_second=32.004M/s tokens_per_second=35.4733M/s
BM_GroupingSymbols/32/4/0                      2778376 ns      2778018 ns          249 bytes_per_second=1026.15M/s lines_per_second=31.6107M/s tokens_per_second=35.9969M/s
BM_GroupingSymbols/32/8/0                      2538279 ns      2538279 ns          275 bytes_per_second=1016.65M/s lines_per_second=31.216M/s tokens_per_second=39.3968M/s
BM_GroupingSymbols/32/16/0                     2291819 ns      2291693 ns          305 bytes_per_second=948.937M/s lines_per_second=28.9306M/s tokens_per_second=43.6359M/s
BM_GroupingSymbols/32/32/0                     1943560 ns      1943560 ns          366 bytes_per_second=855.273M/s lines_per_second=25.7183M/s tokens_per_second=51.452M/s
BM_GroupingSymbols/32/32/1                     1902069 ns      1901915 ns          375 bytes_per_second=861.158M/s lines_per_second=25.8713M/s tokens_per_second=52.5786M/s
BM_GroupingSymbols/32/32/2                     1877847 ns      1877752 ns          379 bytes_per_second=860.371M/s lines_per_second=25.8234M/s tokens_per_second=53.2552M/s
BM_GroupingSymbols/32/32/3                     1837280 ns      1837016 ns          381 bytes_per_second=867.308M/s lines_per_second=26.0069M/s tokens_per_second=54.4361M/s
BM_GroupingSymbols/32/32/4                     1841010 ns      1840902 ns          380 bytes_per_second=853.349M/s lines_per_second=25.5636M/s tokens_per_second=54.3212M/s
BM_GroupingSymbols/32/32/8                     1734676 ns      1734437 ns          405 bytes_per_second=859.062M/s lines_per_second=25.6337M/s tokens_per_second=57.6556M/s
BM_GroupingSymbols/32/32/16                    1641169 ns      1640934 ns          422 bytes_per_second=824.63M/s lines_per_second=24.4403M/s tokens_per_second=60.9409M/s
BM_GroupingSymbols/32/32/32                    1506988 ns      1506914 ns          472 bytes_per_second=759.418M/s lines_per_second=22.2143M/s tokens_per_second=66.3608M/s
BM_BlankLines/1                                5658057 ns      5657150 ns          110 bytes_per_second=101.147M/s lines_per_second=17.6766M/s tokens_per_second=17.6767M/s
BM_BlankLines/4                                8346196 ns      8346052 ns           77 bytes_per_second=102.839M/s lines_per_second=47.9264M/s tokens_per_second=11.9817M/s
BM_BlankLines/16                              31147085 ns     31144610 ns           22 bytes_per_second=64.3033M/s lines_per_second=51.3727M/s tokens_per_second=3.21083M/s
BM_BlankLines/64                              83743719 ns     83743762 ns            8 bytes_per_second=78.5765M/s lines_per_second=76.4228M/s tokens_per_second=1.19412M/s
BM_BlankLines/128                            152299627 ns    152274606 ns            4 bytes_per_second=83.2952M/s lines_per_second=84.0578M/s tokens_per_second=656.708k/s
BM_CommentLines/1/0/0                          7535704 ns      7535149 ns           83 bytes_per_second=113.906M/s lines_per_second=26.542M/s tokens_per_second=13.2711M/s
BM_CommentLines/4/0/0                         10107724 ns     10106088 ns           66 bytes_per_second=169.858M/s lines_per_second=49.4746M/s tokens_per_second=9.89503M/s
BM_CommentLines/128/0/0                      130061022 ns    130029826 ns            5 bytes_per_second=286.034M/s lines_per_second=99.207M/s tokens_per_second=769.054k/s
BM_CommentLines/1/30/0                         7773816 ns      7772726 ns           83 bytes_per_second=478.506M/s lines_per_second=25.7307M/s tokens_per_second=12.8655M/s
BM_CommentLines/4/30/0                        11436116 ns     11434452 ns           56 bytes_per_second=1.12398G/s lines_per_second=43.7271M/s tokens_per_second=8.7455M/s
BM_CommentLines/128/30/0                     155047059 ns    155033899 ns            4 bytes_per_second=2.54103G/s lines_per_second=83.2068M/s tokens_per_second=645.02k/s
BM_CommentLines/1/70/0                         8016209 ns      8014861 ns           75 bytes_per_second=939.998M/s lines_per_second=24.9534M/s tokens_per_second=12.4768M/s
BM_CommentLines/4/70/0                        14894752 ns     14891800 ns           44 bytes_per_second=1.86365G/s lines_per_second=33.5752M/s tokens_per_second=6.7151M/s
BM_CommentLines/128/70/0                     176667108 ns    176631061 ns            4 bytes_per_second=4.92993G/s lines_per_second=73.0329M/s tokens_per_second=566.152k/s
BM_CommentLines/1/0/2                          7764475 ns      7763675 ns           84 bytes_per_second=135.121M/s lines_per_second=25.7607M/s tokens_per_second=12.8805M/s
BM_CommentLines/4/0/2                         10238104 ns     10236809 ns           65 bytes_per_second=242.217M/s lines_per_second=48.8429M/s tokens_per_second=9.76867M/s
BM_CommentLines/128/0/2                      130208823 ns    130190640 ns            5 bytes_per_second=473.204M/s lines_per_second=99.0845M/s tokens_per_second=768.104k/s
BM_CommentLines/1/30/2                         7941224 ns      7940584 ns           78 bytes_per_second=492.411M/s lines_per_second=25.1868M/s tokens_per_second=12.5935M/s
BM_CommentLines/4/30/2                        11936879 ns     11934453 ns           56 bytes_per_second=1.13932G/s lines_per_second=41.8951M/s tokens_per_second=8.3791M/s
BM_CommentLines/128/30/2                     156978531 ns    156967142 ns            4 bytes_per_second=2.66162G/s lines_per_second=82.182M/s tokens_per_second=637.076k/s
BM_CommentLines/1/70/2                         8223614 ns      8222923 ns           79 bytes_per_second=939.409M/s lines_per_second=24.322M/s tokens_per_second=12.1611M/s
BM_CommentLines/4/70/2                        15216239 ns     15215047 ns           45 bytes_per_second=1.87303G/s lines_per_second=32.8619M/s tokens_per_second=6.57244M/s
BM_CommentLines/128/70/2                     176810589 ns    176755958 ns            4 bytes_per_second=5.06133G/s lines_per_second=72.9813M/s tokens_per_second=565.752k/s
BM_CommentLines/1/0/8                          7901146 ns      7899003 ns           82 bytes_per_second=205.245M/s lines_per_second=25.3194M/s tokens_per_second=12.6598M/s
BM_CommentLines/4/0/8                         10135919 ns     10134576 ns           66 bytes_per_second=470.501M/s lines_per_second=49.3356M/s tokens_per_second=9.86721M/s
BM_CommentLines/128/0/8                      132206448 ns    132206347 ns            5 bytes_per_second=1019.98M/s lines_per_second=97.5738M/s tokens_per_second=756.393k/s
BM_CommentLines/1/30/8                         7981189 ns      7981202 ns           79 bytes_per_second=561.598M/s lines_per_second=25.0586M/s tokens_per_second=12.5294M/s
BM_CommentLines/4/30/8                        12311389 ns     12309078 ns           54 bytes_per_second=1.28623G/s lines_per_second=40.62M/s tokens_per_second=8.12409M/s
BM_CommentLines/128/30/8                     160432032 ns    160393999 ns            4 bytes_per_second=3.05069G/s lines_per_second=80.4261M/s tokens_per_second=623.465k/s
BM_CommentLines/1/70/8                         8199080 ns      8199078 ns           79 bytes_per_second=1011.93M/s lines_per_second=24.3927M/s tokens_per_second=12.1965M/s
BM_CommentLines/4/70/8                        16087954 ns     16086159 ns           44 bytes_per_second=1.91055G/s lines_per_second=31.0823M/s tokens_per_second=6.21652M/s
BM_CommentLines/128/70/8                     175714908 ns    175675241 ns            4 bytes_per_second=5.4996G/s lines_per_second=73.4302M/s tokens_per_second=569.232k/s
BM_SpeedOfLightStrCpy                            28860 ns        28858 ns        26092 bytes_per_second=35.181G/s lines_per_second=1.10002G/s tokens_per_second=3.46519G/s
BM_SpeedOfLightDispatch<1>                     1823558 ns      1823473 ns          385 bytes_per_second=570.14M/s lines_per_second=17.4091M/s tokens_per_second=54.8404M/s
BM_SpeedOfLightDispatch<2>                     2013453 ns      2013250 ns          343 bytes_per_second=516.396M/s lines_per_second=15.768M/s tokens_per_second=49.6709M/s
BM_SpeedOfLightDispatch<4>                     2225145 ns      2224920 ns          312 bytes_per_second=467.268M/s lines_per_second=14.2679M/s tokens_per_second=44.9454M/s
BM_SpeedOfLightDispatch<8>                     2851730 ns      2851590 ns          251 bytes_per_second=364.581M/s lines_per_second=11.1324M/s tokens_per_second=35.0682M/s
BM_SpeedOfLightDispatch<16>                    4431584 ns      4431156 ns          161 bytes_per_second=234.619M/s lines_per_second=7.16404M/s tokens_per_second=22.5675M/s
BM_SpeedOfLightDispatch<32>                    6229285 ns      6228092 ns          111 bytes_per_second=166.927M/s lines_per_second=5.09707M/s tokens_per_second=16.0563M/s
BM_SpeedOfLightDispatch<MaxDispatchTargets>    8967228 ns      8965583 ns           79 bytes_per_second=115.958M/s lines_per_second=3.54076M/s tokens_per_second=11.1538M/s
```

---------

Co-authored-by: Chandler Carruth <chandlerc@gmail.com>
2023-11-10 23:59:49 +00:00
Jon Ross-Perkins 3401eed8d8 Split IdentifierId and StringLiteralId from StringId (#3352)
Following up on discussion yesterday regarding this split.

Note, I'm expecting #3341 to do IdentifierId -> NameId in SemIR. It
might be worth adding NameId creation directly to StringStore if you're
content with this setup though.
2023-11-02 18:44:32 +00:00
Jon Ross-Perkins 6742d0d048 Add partial raw identifier support. (#3344)
I'm looking at this due to the conversation on #3341. Although
diagnostics aren't where they should be, I thought it may help to start
adding raw identifier support (which may also help show how I was
thinking about this).

Note regarding the TODO on how to form the token, `GetTokenText` returns
the `string_id`'s reference value for an `Identifier`. So to make
`GetTokenText` work in a way that returns `r#foo` for a raw identifier,
I think there are a few options:

1. Add additional data indicating the end of the identifier.
2. Add `RawIdentifier` as a token kind to indicate that it's raw and
should be prefixed with `r#` (but also giving later stages one more
token kind to handle)
3. Make the `string_id` correspond to `r#foo`, and have later stages add
`foo` to the strings table whenever `r#foo` is encountered (with map
lookups leading to deduplication).
4. Add `StringId::RawKeyword` special values for each keyword.
- This would mean `self` prints as `self`, `r#self` prints as `r#self`,
but `r#foo` is not a keyword so prints as `foo`.
- This means keywords would need to be listed in a place `StringId` can
depend on them, one way or the other (e.g., a `keywords.def` file in
`base/` should work).
5. Say that it _is_ an `Identifier`, and if it's a keyword spelling, it
must have been a raw identifier.
- Same limitation as above: This would mean `self` prints as `self`,
`r#self` prints as `r#self`, but `r#foo` is not a keyword so prints as
`foo`.

I'm hoping to resolve this issue separately though. :)
2023-10-30 18:11:55 +00:00
Jon Ross-Perkins b2cfd5a8a8 Change StringLiteral to less frequently allocate a new string. (#3314)
Building on #3311, which started moving the result string into a
`unique_ptr`, instead have `StringLiteral` use a `BumpPtrAllocator` to
manage memory. But also, detect when a string is really trivial during
`Lex` and, if so, return `contents_` directly.
2023-10-24 19:15:49 +00:00
Chandler CarruthandJon Ross-Perkins 1b0e2d3a4b Cleanups of SIMD code and document no Arm port. (#3325)
I spent (a lot) of time working to see if there was any profitable way
to port the SIMD code that scans for identifier length to Arm. There
isn't really. =/ While working on these, I made some cleanups to the
SIMD code that seemed worth landing, and added some benchmarks. All this
PR does is the cleanups, benchmarks, and documents that Arm isn't just
waiting to get attention but doesn't really have good options (so far).

For posterity, here are the core techniques I tried:

1) Direct 32-byte SIMD scanning using pair-wise add trees to build
   a 32-bit mask of valid identifier and then `clz` to compute the
   distance. This is a very good analog to the 16-byte SIMD structure
   used on x86-64. The pair-wise summing technique is the one used in
   simdjson for similar purposes.
2) A 16-byte SIMD scanning similar to the x86 version but using `shrn`
   to produce a 64-bit scalar bitmask with 4 bits per byte, and then
   scaling the bit-count distance.
3) Various hybrid versions of (1) and (2) with short scalar scans to
   identify short identifiers before paying the SIMD start-up cost.
4) A much fancier version of (1) that scanned 64-bytes at a time, but
   cached the resulting 64-bit mask and re-used it until exhausted.

Some good background on these techniques on Arm CPUs is in this blog
post:
https://community.arm.com/arm-community-blogs/b/infrastructure-solutions-blog/posts/porting-x86-vector-bitmask-optimizations-to-arm-neon

Sadly, both (1) and (2) were significantly slower than a scalar loop
over the bytes. Even (3) was consistently slower.

The only approach that came close was (4) and it was very *slightly*
slower in typical examples and very *slightly* faster in extremely
difficult cases like huge identifiers.

Ultimately, the only path I see (suggested by Dougall on a Mastodon
discussion of this whole problem space) is to take (4) to the limit of
computing an identifier-or-not bitmask *for the entire source file*
using a deeply throughput optimized routine (maybe as part of the line
scanning). That should be able to manage the high latency you end up
with when handling these patterns in SIMD on Arm.

The good news is that at least the M1 is *so* fast in the byte-scanning
loop that this isn't hurting nearly as much as I feared.

---------

Co-authored-by: Jon Ross-Perkins <jperkins@google.com>
2023-10-24 08:32:21 +00:00
Jon Ross-Perkins 1b55ad86dd Extend SharedValueStores to SemIR (#3313)
Building on #3311, change SemIR to use the SharedValueStore. Since this
removes hermeticity, raw output no longer prints ints, reals, and
strings. TokenizedBuffer accessors are modified to return IDs because
values are often passed through in semantics without needing to read
them.

I would've put SharedValueStores on Context, except for the
GetArrayBoundValue convenience method. I felt awkward removing that, so
it's on File, at least for now. That's then used by the formatter and
Lower too. The flipside of this is that TokenizedBuffer has a
SharedValueStores only for printing, so maybe that's similar enough to
what File is doing.

This doesn't start shifting other SemIR members to ValueStore, but that
seems like a next step.
2023-10-20 17:53:00 +00:00
Jon Ross-PerkinsandRichard Smith d13f76e001 Add value store to be shared across compile stages. (#3311)
This updates lexing to use the data. I'll do checking separately, just
to split changes.

Note the ValueStore structure is also set up such that SemIR::File can
use it for other fields.

---------

Co-authored-by: Richard Smith <richard@metafoo.co.uk>
2023-10-20 15:00:53 +00:00
629c63c7d1 Port the comment block scanning SIMD to Arm. (#3300)
This adds an Arm Neon code path for comment block scanning. It also
tries to tightened up the exact architecture-specific coding pattern for
SIMD code here. Notably, moving to always having architecture-specific
code inside a macro that can be used to globally disable SIMD, but any
place where *all* architectures will require some custom code, an `else`
branch with an error.

Overall, this makes large-block lexing >5% faster on an ARM server
I have access to, but that's a bit misleading. The Neon performance
there doesn't seem very good. On my M1 laptop the difference is *much*
larger. There, even 4-line comments show a noticable improvement and the
block speed looks well over 20%. Sadly, I don't have the same nice
scripts to generate good statistical data.

Raw benchmark data from an ARM sever for reference:

```
BM_CommentLines/1/0/0                          19.4ms ± 7%  19.7ms ± 6%    ~     (p=0.121 n=20+20)
BM_CommentLines/4/0/0                          24.8ms ± 7%  24.8ms ± 6%    ~     (p=0.904 n=20+20)
BM_CommentLines/128/0/0                         251ms ± 2%   234ms ± 3%  -6.70%  (p=0.000 n=20+20)
BM_CommentLines/1/30/0                         21.6ms ±10%  21.9ms ±10%    ~     (p=0.157 n=20+20)
BM_CommentLines/4/30/0                         28.8ms ± 9%  28.8ms ±11%    ~     (p=0.779 n=20+20)
BM_CommentLines/128/30/0                        248ms ± 2%   233ms ± 2%  -5.72%  (p=0.000 n=20+20)
BM_CommentLines/1/70/0                         23.4ms ±12%  23.7ms ±12%    ~     (p=0.341 n=20+20)
BM_CommentLines/4/70/0                         30.8ms ± 9%  31.1ms ±11%    ~     (p=0.602 n=20+20)
BM_CommentLines/128/70/0                        302ms ± 4%   292ms ± 4%  -3.46%  (p=0.000 n=18+20)
BM_CommentLines/1/0/2                          19.8ms ± 7%  20.0ms ± 6%    ~     (p=0.149 n=20+20)
BM_CommentLines/4/0/2                          25.1ms ± 7%  25.3ms ± 8%    ~     (p=0.659 n=20+20)
BM_CommentLines/128/0/2                         225ms ± 2%   212ms ± 2%  -5.88%  (p=0.000 n=20+20)
BM_CommentLines/1/30/2                         22.0ms ± 9%  22.2ms ±10%    ~     (p=0.289 n=20+20)
BM_CommentLines/4/30/2                         29.0ms ±11%  29.1ms ±12%    ~     (p=0.738 n=20+20)
BM_CommentLines/128/30/2                        261ms ±10%   243ms ± 3%  -6.85%  (p=0.000 n=20+20)
BM_CommentLines/1/70/2                         23.5ms ±11%  23.8ms ±15%    ~     (p=0.429 n=20+20)
BM_CommentLines/4/70/2                         31.3ms ±10%  31.5ms ±11%    ~     (p=0.478 n=20+20)
BM_CommentLines/128/70/2                        306ms ± 4%   292ms ± 4%  -4.52%  (p=0.000 n=18+19)
BM_CommentLines/1/0/8                          20.9ms ± 8%  21.2ms ± 7%    ~     (p=0.127 n=20+20)
BM_CommentLines/4/0/8                          27.3ms ± 9%  27.5ms ±12%    ~     (p=0.678 n=20+20)
BM_CommentLines/128/0/8                         227ms ± 2%   210ms ± 2%  -7.35%  (p=0.000 n=19+20)
BM_CommentLines/1/30/8                         22.6ms ±11%  23.0ms ±10%    ~     (p=0.114 n=20+20)
BM_CommentLines/4/30/8                         29.4ms ±10%  29.4ms ±12%    ~     (p=0.947 n=20+20)
BM_CommentLines/128/30/8                        275ms ± 4%   257ms ± 7%  -6.59%  (p=0.000 n=19+20)
BM_CommentLines/1/70/8                         23.9ms ±13%  24.3ms ±14%    ~     (p=0.265 n=20+20)
BM_CommentLines/4/70/8                         32.3ms ±11%  32.4ms ± 9%    ~     (p=0.478 n=20+20)
BM_CommentLines/128/70/8                        319ms ± 4%   307ms ± 4%  -3.83%  (p=0.000 n=18+19)
```

---------

Co-authored-by: Richard Smith <richard@metafoo.co.uk>
Co-authored-by: Jon Ross-Perkins <jperkins@google.com>
2023-10-20 08:28:19 +00:00
Chandler CarruthandRichard Smith 3015135a52 Skip blocks of comments with identical prefixes. (#3299)
Specifically, after lexing a comment line, look at the next line and see
if it starts with an identical sequence of indent, comment '/'s and
character after the '/'s. If so, skip it as part of a block of comments.
This skips repeatedly diagnosing the same erroneous comment introducer
after the first one in a block, but that seems like a feature rather
than a bug.

The big motivation is to make sure the lexer is minimally impacted by
the length of comment blocks and skips them as efficiently as possible.
While they aren't exactly common, large block comments do come up and
it'd be unfortunate for those to actually slow down the toolchain.

It also happens that this is particularly easy to do because we're just
looking to see if we see the same prefix byte sequence. With SIMD we can
typically handle the most common indents with just a few instructions.

Because of the diagnostic differences, I've included a scalar fallback
that replicates the functionality but has no limit on indent size or CPU
features. I've also added testing to cover this behavior.

The only non-noise benchmark changes are as expected the comment ones,
with a nice improvement across the board:

```
BM_CommentLines/1/0/0                          15.8ms ± 2%  15.6ms ± 2%   -0.87%  (p=0.004 n=19+19)
BM_CommentLines/4/0/0                          20.2ms ± 1%  18.8ms ± 1%   -6.75%  (p=0.000 n=18+18)
BM_CommentLines/128/0/0                         221ms ± 1%   167ms ± 1%  -24.44%  (p=0.000 n=20+19)
BM_CommentLines/1/30/0                         16.6ms ± 3%  16.5ms ± 3%     ~     (p=0.175 n=19+20)
BM_CommentLines/4/30/0                         26.1ms ± 1%  24.8ms ± 2%   -5.05%  (p=0.000 n=18+19)
BM_CommentLines/128/30/0                        233ms ± 1%   185ms ± 1%  -20.38%  (p=0.000 n=19+20)
BM_CommentLines/1/70/0                         19.2ms ± 1%  19.0ms ± 2%   -0.66%  (p=0.016 n=19+20)
BM_CommentLines/4/70/0                         27.9ms ± 1%  26.6ms ± 1%   -4.63%  (p=0.000 n=19+19)
BM_CommentLines/128/70/0                        251ms ± 1%   213ms ± 1%  -15.18%  (p=0.000 n=20+18)
BM_CommentLines/1/0/2                          15.9ms ± 1%  15.8ms ± 2%     ~     (p=0.061 n=19+19)
BM_CommentLines/4/0/2                          20.5ms ± 2%  19.0ms ± 2%   -7.53%  (p=0.000 n=20+20)
BM_CommentLines/128/0/2                         213ms ± 1%   153ms ± 1%  -28.18%  (p=0.000 n=19+20)
BM_CommentLines/1/30/2                         16.8ms ± 2%  16.7ms ± 3%     ~     (p=0.134 n=20+20)
BM_CommentLines/4/30/2                         26.6ms ± 1%  25.2ms ± 3%   -5.50%  (p=0.000 n=20+20)
BM_CommentLines/128/30/2                        238ms ± 1%   187ms ± 2%  -21.49%  (p=0.000 n=17+19)
BM_CommentLines/1/70/2                         19.3ms ± 1%  19.4ms ± 3%     ~     (p=0.407 n=17+20)
BM_CommentLines/4/70/2                         28.2ms ± 1%  26.9ms ± 2%   -4.70%  (p=0.000 n=19+19)
BM_CommentLines/128/70/2                        257ms ± 2%   214ms ± 1%  -16.52%  (p=0.000 n=20+18)
BM_CommentLines/1/0/8                          16.3ms ± 2%  16.1ms ± 2%   -1.22%  (p=0.001 n=20+20)
BM_CommentLines/4/0/8                          22.7ms ± 2%  20.4ms ± 2%  -10.20%  (p=0.000 n=20+20)
BM_CommentLines/128/0/8                         244ms ± 1%   153ms ± 1%  -37.26%  (p=0.000 n=20+18)
BM_CommentLines/1/30/8                         17.3ms ± 2%  17.2ms ± 3%     ~     (p=0.192 n=20+20)
BM_CommentLines/4/30/8                         28.0ms ± 2%  25.6ms ± 3%   -8.46%  (p=0.000 n=19+18)
BM_CommentLines/128/30/8                        272ms ± 1%   196ms ± 2%  -27.90%  (p=0.000 n=18+20)
BM_CommentLines/1/70/8                         19.9ms ± 2%  19.9ms ± 2%     ~     (p=0.531 n=20+19)
BM_CommentLines/4/70/8                         29.3ms ± 1%  27.3ms ± 1%   -6.87%  (p=0.000 n=19+19)
BM_CommentLines/128/70/8                        292ms ± 1%   228ms ± 1%  -21.97%  (p=0.000 n=20+19)
```

---------

Co-authored-by: Richard Smith <richard@metafoo.co.uk>
2023-10-17 16:07:03 +00:00
Chandler Carruth 7371354dc7 Consolidate indent handling to following newlines. (#3298)
The biggest advantage of this is reducing the repeated code in every
non-whitespace code path of the lexer to set indent appropriately. Now
we handle it cleanly at the start and after vertical whitespace.

While this makes the generated code for all the other paths through the
lexer quite a bit nicer, it doesn't actually move performance in
interesting ways outside of making large blocks of blank lines slightly
slower. While there are lots of fluctuations in the benchmark data, they
mostly seem to be either noise or artifacts of loop alignment and not
really due to an important change here.

Raw benchmark data:

```
BM_ValidKeywords                               3.10ms ± 1%   3.13ms ± 1%   +1.02%  (p=0.000 n=20+19)
BM_ValidIdentifiers<1, 64, false>              10.8ms ± 3%   10.7ms ± 3%     ~     (p=0.383 n=20+20)
BM_ValidIdentifiers<1, 1, true>                3.71ms ± 1%   3.67ms ± 2%   -1.05%  (p=0.000 n=19+19)
BM_ValidIdentifiers<3, 5, true>                13.1ms ± 2%   13.0ms ± 2%     ~     (p=0.120 n=20+19)
BM_ValidIdentifiers<3, 16, true>               13.1ms ± 2%   13.2ms ± 2%     ~     (p=0.091 n=20+20)
BM_ValidIdentifiers<12, 64, true>              15.1ms ± 1%   15.1ms ± 1%     ~     (p=0.138 n=19+19)
BM_HorizontalWhitespace/1                      13.2ms ± 3%   13.2ms ± 0%     ~     (p=0.458 n=20+15)
BM_HorizontalWhitespace/4                      13.4ms ± 3%   13.3ms ± 1%   -1.12%  (p=0.000 n=20+19)
BM_HorizontalWhitespace/16                     14.1ms ± 2%   14.0ms ± 2%   -0.89%  (p=0.010 n=20+20)
BM_HorizontalWhitespace/64                     17.9ms ± 2%   17.8ms ± 1%   -0.73%  (p=0.002 n=20+16)
BM_HorizontalWhitespace/128                    24.2ms ± 2%   24.1ms ± 1%     ~     (p=0.346 n=19+17)
BM_RandomSource                                7.88ms ± 2%   7.83ms ± 3%     ~     (p=0.052 n=20+20)
BM_GroupingSymbols/1/0/0                       6.25ms ± 2%   6.38ms ± 1%   +1.99%  (p=0.000 n=20+19)
BM_GroupingSymbols/2/0/0                       5.24ms ± 2%   5.27ms ± 2%     ~     (p=0.065 n=20+19)
BM_GroupingSymbols/3/0/0                       4.09ms ± 1%   4.07ms ± 1%     ~     (p=0.063 n=20+20)
BM_GroupingSymbols/4/0/0                       3.80ms ± 1%   3.79ms ± 2%     ~     (p=0.820 n=20+20)
BM_GroupingSymbols/8/0/0                       3.05ms ± 1%   3.09ms ± 1%   +1.41%  (p=0.000 n=20+20)
BM_GroupingSymbols/16/0/0                      2.95ms ± 1%   3.01ms ± 1%   +1.79%  (p=0.000 n=20+20)
BM_GroupingSymbols/32/0/0                      3.67ms ± 1%   3.77ms ± 1%   +2.75%  (p=0.000 n=20+20)
BM_GroupingSymbols/0/1/0                       5.66ms ± 1%   5.61ms ± 1%   -0.88%  (p=0.000 n=20+18)
BM_GroupingSymbols/0/2/0                       4.43ms ± 2%   4.40ms ± 1%   -0.74%  (p=0.005 n=20+17)
BM_GroupingSymbols/0/3/0                       3.15ms ± 2%   3.12ms ± 2%   -0.96%  (p=0.002 n=20+19)
BM_GroupingSymbols/0/4/0                       2.79ms ± 2%   2.77ms ± 2%   -0.88%  (p=0.005 n=20+20)
BM_GroupingSymbols/0/8/0                       1.81ms ± 2%   1.79ms ± 2%     ~     (p=0.056 n=20+20)
BM_GroupingSymbols/0/16/0                      1.26ms ± 2%   1.26ms ± 2%     ~     (p=0.547 n=20+20)
BM_GroupingSymbols/0/32/0                      1.05ms ± 1%   0.96ms ± 1%   -8.52%  (p=0.000 n=19+19)
BM_GroupingSymbols/0/0/1                       5.68ms ± 2%   5.65ms ± 2%     ~     (p=0.126 n=20+18)
BM_GroupingSymbols/0/0/2                       4.44ms ± 2%   4.40ms ± 2%   -0.99%  (p=0.001 n=20+18)
BM_GroupingSymbols/0/0/3                       3.15ms ± 1%   3.13ms ± 1%   -0.74%  (p=0.005 n=20+18)
BM_GroupingSymbols/0/0/4                       2.80ms ± 1%   2.77ms ± 2%   -1.01%  (p=0.000 n=20+19)
BM_GroupingSymbols/0/0/8                       1.81ms ± 2%   1.79ms ± 2%   -0.97%  (p=0.006 n=20+20)
BM_GroupingSymbols/0/0/16                      1.26ms ± 1%   1.26ms ± 2%     ~     (p=0.678 n=20+20)
BM_GroupingSymbols/0/0/32                      1.05ms ± 1%   0.96ms ± 1%   -8.59%  (p=0.000 n=20+19)
BM_GroupingSymbols/32/1/0                      3.57ms ± 1%   3.69ms ± 1%   +3.39%  (p=0.000 n=19+20)
BM_GroupingSymbols/32/2/0                      3.49ms ± 1%   3.60ms ± 1%   +3.16%  (p=0.000 n=20+20)
BM_GroupingSymbols/32/3/0                      3.41ms ± 1%   3.52ms ± 1%   +3.49%  (p=0.000 n=19+20)
BM_GroupingSymbols/32/4/0                      3.35ms ± 1%   3.45ms ± 1%   +3.13%  (p=0.000 n=20+20)
BM_GroupingSymbols/32/8/0                      3.12ms ± 1%   3.22ms ± 1%   +3.09%  (p=0.000 n=19+20)
BM_GroupingSymbols/32/16/0                     2.75ms ± 1%   2.82ms ± 1%   +2.71%  (p=0.000 n=19+20)
BM_GroupingSymbols/32/32/0                     2.24ms ± 2%   2.24ms ± 1%     ~     (p=0.311 n=20+17)
BM_GroupingSymbols/32/32/1                     2.20ms ± 1%   2.23ms ± 1%   +1.23%  (p=0.000 n=20+20)
BM_GroupingSymbols/32/32/2                     2.19ms ± 1%   2.21ms ± 1%   +1.08%  (p=0.000 n=20+20)
BM_GroupingSymbols/32/32/3                     2.16ms ± 0%   2.19ms ± 1%   +1.09%  (p=0.000 n=18+20)
BM_GroupingSymbols/32/32/4                     2.14ms ± 1%   2.19ms ± 1%   +2.25%  (p=0.000 n=20+20)
BM_GroupingSymbols/32/32/8                     2.07ms ± 1%   2.11ms ± 1%   +2.04%  (p=0.000 n=17+20)
BM_GroupingSymbols/32/32/16                    1.94ms ± 1%   1.98ms ± 1%   +1.95%  (p=0.000 n=20+20)
BM_GroupingSymbols/32/32/32                    1.74ms ± 1%   1.77ms ± 1%   +1.88%  (p=0.000 n=18+20)
BM_BlankLines/1                                14.2ms ± 1%   14.4ms ± 1%   +1.47%  (p=0.000 n=18+16)
BM_BlankLines/4                                17.3ms ± 1%   17.5ms ± 2%   +1.32%  (p=0.000 n=17+20)
BM_BlankLines/16                               31.3ms ± 1%   33.0ms ± 2%   +5.35%  (p=0.000 n=19+20)
BM_BlankLines/64                               89.6ms ± 1%  101.3ms ± 1%  +12.98%  (p=0.000 n=20+20)
BM_BlankLines/128                               167ms ± 3%    185ms ± 1%  +11.11%  (p=0.000 n=19+20)
BM_CommentLines/1/0/0                          15.7ms ± 1%   15.8ms ± 1%     ~     (p=0.109 n=19+16)
BM_CommentLines/4/0/0                          19.8ms ± 1%   20.2ms ± 1%   +2.12%  (p=0.000 n=20+19)
BM_CommentLines/128/0/0                         208ms ± 1%    221ms ± 1%   +6.29%  (p=0.000 n=20+19)
BM_CommentLines/1/30/0                         16.6ms ± 1%   16.6ms ± 1%     ~     (p=0.354 n=19+19)
BM_CommentLines/4/30/0                         25.8ms ± 2%   26.1ms ± 1%   +1.39%  (p=0.000 n=20+19)
BM_CommentLines/128/30/0                        222ms ± 2%    232ms ± 1%   +4.46%  (p=0.000 n=20+18)
BM_CommentLines/1/70/0                         19.2ms ± 2%   19.1ms ± 1%     ~     (p=0.478 n=20+19)
BM_CommentLines/4/70/0                         27.6ms ± 2%   27.9ms ± 1%   +0.95%  (p=0.001 n=20+18)
BM_CommentLines/128/70/0                        245ms ± 2%    250ms ± 1%   +1.96%  (p=0.000 n=20+18)
BM_CommentLines/1/0/2                          16.0ms ± 1%   15.9ms ± 2%   -0.68%  (p=0.015 n=15+18)
BM_CommentLines/4/0/2                          20.6ms ± 1%   20.5ms ± 1%   -0.53%  (p=0.024 n=18+19)
BM_CommentLines/128/0/2                         216ms ± 2%    213ms ± 1%   -1.70%  (p=0.000 n=17+19)
BM_CommentLines/1/30/2                         16.9ms ± 2%   16.8ms ± 3%     ~     (p=0.072 n=20+20)
BM_CommentLines/4/30/2                         26.8ms ± 2%   26.5ms ± 1%   -0.91%  (p=0.000 n=20+19)
BM_CommentLines/128/30/2                        244ms ± 2%    238ms ± 1%   -2.30%  (p=0.000 n=19+17)
BM_CommentLines/1/70/2                         19.4ms ± 2%   19.4ms ± 2%     ~     (p=0.059 n=18+20)
BM_CommentLines/4/70/2                         28.4ms ± 2%   28.2ms ± 1%   -0.81%  (p=0.003 n=20+19)
BM_CommentLines/128/70/2                        261ms ± 2%    255ms ± 1%   -2.33%  (p=0.000 n=20+18)
BM_CommentLines/1/0/8                          16.2ms ± 2%   16.2ms ± 2%     ~     (p=0.904 n=20+20)
BM_CommentLines/4/0/8                          25.0ms ± 1%   22.6ms ± 1%   -9.40%  (p=0.000 n=20+19)
BM_CommentLines/128/0/8                         246ms ± 2%    244ms ± 1%   -1.14%  (p=0.000 n=20+19)
BM_CommentLines/1/30/8                         17.3ms ± 2%   17.3ms ± 1%     ~     (p=1.000 n=19+18)
BM_CommentLines/4/30/8                         30.3ms ± 2%   27.9ms ± 1%   -7.83%  (p=0.000 n=20+20)
BM_CommentLines/128/30/8                        277ms ± 3%    271ms ± 1%   -2.26%  (p=0.000 n=20+17)
BM_CommentLines/1/70/8                         19.9ms ± 2%   19.9ms ± 2%     ~     (p=0.687 n=19+20)
BM_CommentLines/4/70/8                         31.7ms ± 2%   29.3ms ± 1%   -7.55%  (p=0.000 n=20+18)
BM_CommentLines/128/70/8                        296ms ± 2%    291ms ± 1%   -1.50%  (p=0.000 n=20+18)
```
2023-10-16 16:54:05 +00:00
Chandler Carruth 95a1cc8cba Tweak lexer to improve generated code. (#3296)
This is a collection of tweaks and they are ones I'm less confident in
FWIW. I set out to make the generated code cleaner and reduce loading
pointers through pointers in a bunch of cases. It also works to reduce
the working-set-size, and reduce the set of mutated values on each
iteration. But I didn't benchmark at each step and it's not clear that
incremental benchmarks will even be meaningful, so its hard to say which
tweaks were load bearing and which weren't.

For example, with this, the core lexer dispatch loop doesn't mutate
anything in memory -- the position is passed in register and incremented
in register throughout. The pointer to the source text, the size, and
the pointer to the lexer are also passed in registers.

By making the tokenized buffer a direct member of the lexer, all of its
members can be accessed at a constant offset from the lexer pointer
itself. The cost of this is a move at the end of lexing, but especially
as the buffer size gets large this seems like a trivial cost compared to
the previous double-indirection.

This uses a (significantly) cheaper representation for the line index --
the `Line` type is optimized for dense *storage*, and is not a great
type for using in a tight loop like the lexer. Once using a good index
and once the line table is readily available from the above, we can
simply index the line table rather than store (and update) a separate
pointer.

Last but not least, this removes the column as suggested in a previous
review. Now it is computed from the position and the line information.

My macro benchmark looks like it improves in the 2% - 5% range, but its
getting into the noise sadly (or maybe this is good?).

On a quiet AMD server with 20 runs before/after I get reasonably
compelling across-the-board improvements on all the microbenchmarks,
including 5% on `RandomSource` which is key:

```
BM_ValidKeywords                               3.44ms ± 1%  3.11ms ± 1%   -9.52%  (p=0.000 n=19+20)
BM_ValidIdentifiers<1, 64, false>              11.2ms ± 1%  10.8ms ± 2%   -3.23%  (p=0.000 n=19+20)
BM_ValidIdentifiers<1, 1, true>                3.96ms ± 1%  3.72ms ± 1%   -6.10%  (p=0.000 n=18+20)
BM_ValidIdentifiers<3, 5, true>                13.5ms ± 1%  13.2ms ± 3%   -2.45%  (p=0.000 n=19+20)
BM_ValidIdentifiers<3, 16, true>               13.5ms ± 2%  13.1ms ± 2%   -2.81%  (p=0.000 n=19+20)
BM_ValidIdentifiers<12, 64, true>              15.6ms ± 1%  15.2ms ± 3%   -2.41%  (p=0.000 n=19+20)
BM_HorizontalWhitespace/1                      13.6ms ± 2%  13.3ms ± 3%   -2.34%  (p=0.000 n=19+20)
BM_HorizontalWhitespace/4                      13.9ms ± 2%  13.6ms ± 3%   -2.06%  (p=0.000 n=19+20)
BM_HorizontalWhitespace/16                     14.5ms ± 2%  14.3ms ± 2%   -1.17%  (p=0.002 n=19+20)
BM_HorizontalWhitespace/64                     18.5ms ± 3%  18.1ms ± 2%   -2.17%  (p=0.000 n=19+19)
BM_HorizontalWhitespace/128                    24.7ms ± 1%  24.4ms ± 2%   -1.29%  (p=0.000 n=18+20)
BM_RandomSource                                8.38ms ± 2%  7.92ms ± 2%   -5.50%  (p=0.000 n=19+20)
BM_GroupingSymbols/1/0/0                       6.63ms ± 2%  6.30ms ± 2%   -4.97%  (p=0.000 n=19+20)
BM_GroupingSymbols/2/0/0                       5.65ms ± 2%  5.27ms ± 2%   -6.75%  (p=0.000 n=19+20)
BM_GroupingSymbols/3/0/0                       4.51ms ± 1%  4.12ms ± 1%   -8.62%  (p=0.000 n=19+18)
BM_GroupingSymbols/4/0/0                       4.23ms ± 1%  3.82ms ± 1%   -9.65%  (p=0.000 n=19+18)
BM_GroupingSymbols/8/0/0                       3.50ms ± 1%  3.08ms ± 1%  -12.21%  (p=0.000 n=19+19)
BM_GroupingSymbols/16/0/0                      3.38ms ± 1%  2.97ms ± 1%  -11.98%  (p=0.000 n=19+19)
BM_GroupingSymbols/32/0/0                      4.10ms ± 1%  3.69ms ± 1%  -10.02%  (p=0.000 n=17+19)
BM_GroupingSymbols/0/1/0                       5.85ms ± 2%  5.70ms ± 1%   -2.54%  (p=0.000 n=19+20)
BM_GroupingSymbols/0/2/0                       4.60ms ± 2%  4.46ms ± 1%   -3.12%  (p=0.000 n=19+19)
BM_GroupingSymbols/0/3/0                       3.32ms ± 2%  3.18ms ± 1%   -4.21%  (p=0.000 n=18+20)
BM_GroupingSymbols/0/4/0                       2.94ms ± 2%  2.82ms ± 1%   -4.22%  (p=0.000 n=19+20)
BM_GroupingSymbols/0/8/0                       1.95ms ± 1%  1.83ms ± 1%   -6.47%  (p=0.000 n=17+19)
BM_GroupingSymbols/0/16/0                      1.40ms ± 1%  1.27ms ± 1%   -9.12%  (p=0.000 n=19+20)
BM_GroupingSymbols/0/32/0                      1.19ms ± 1%  1.05ms ± 1%  -11.50%  (p=0.000 n=19+20)
BM_GroupingSymbols/0/0/1                       5.88ms ± 2%  5.71ms ± 1%   -2.87%  (p=0.000 n=19+20)
BM_GroupingSymbols/0/0/2                       4.62ms ± 1%  4.45ms ± 2%   -3.58%  (p=0.000 n=18+20)
BM_GroupingSymbols/0/0/3                       3.32ms ± 1%  3.17ms ± 1%   -4.72%  (p=0.000 n=16+17)
BM_GroupingSymbols/0/0/4                       2.95ms ± 1%  2.81ms ± 2%   -4.50%  (p=0.000 n=16+19)
BM_GroupingSymbols/0/0/8                       1.96ms ± 1%  1.82ms ± 2%   -6.80%  (p=0.000 n=19+20)
BM_GroupingSymbols/0/0/16                      1.40ms ± 1%  1.27ms ± 1%   -8.96%  (p=0.000 n=19+20)
BM_GroupingSymbols/0/0/32                      1.19ms ± 1%  1.05ms ± 1%  -11.64%  (p=0.000 n=19+20)
BM_GroupingSymbols/32/1/0                      4.04ms ± 1%  3.60ms ± 1%  -10.81%  (p=0.000 n=17+20)
BM_GroupingSymbols/32/2/0                      3.93ms ± 1%  3.51ms ± 1%  -10.55%  (p=0.000 n=18+20)
BM_GroupingSymbols/32/3/0                      3.84ms ± 1%  3.44ms ± 1%  -10.38%  (p=0.000 n=18+19)
BM_GroupingSymbols/32/4/0                      3.76ms ± 2%  3.38ms ± 1%  -10.21%  (p=0.000 n=19+20)
BM_GroupingSymbols/32/8/0                      3.51ms ± 1%  3.14ms ± 1%  -10.61%  (p=0.000 n=19+19)
BM_GroupingSymbols/32/16/0                     3.11ms ± 1%  2.77ms ± 1%  -10.95%  (p=0.000 n=19+20)
BM_GroupingSymbols/32/32/0                     2.54ms ± 2%  2.25ms ± 1%  -11.77%  (p=0.000 n=19+16)
BM_GroupingSymbols/32/32/1                     2.50ms ± 1%  2.22ms ± 2%  -11.19%  (p=0.000 n=19+20)
BM_GroupingSymbols/32/32/2                     2.48ms ± 1%  2.21ms ± 1%  -11.15%  (p=0.000 n=19+20)
BM_GroupingSymbols/32/32/3                     2.46ms ± 1%  2.18ms ± 1%  -11.11%  (p=0.000 n=18+20)
BM_GroupingSymbols/32/32/4                     2.43ms ± 1%  2.16ms ± 1%  -11.04%  (p=0.000 n=19+20)
BM_GroupingSymbols/32/32/8                     2.35ms ± 1%  2.09ms ± 1%  -11.11%  (p=0.000 n=19+20)
BM_GroupingSymbols/32/32/16                    2.20ms ± 1%  1.96ms ± 1%  -11.30%  (p=0.000 n=19+18)
BM_GroupingSymbols/32/32/32                    1.98ms ± 1%  1.75ms ± 1%  -11.54%  (p=0.000 n=19+19)
BM_BlankLines/1                                14.6ms ± 1%  14.2ms ± 3%   -2.53%  (p=0.000 n=19+19)
BM_BlankLines/4                                18.3ms ± 1%  17.4ms ± 2%   -4.68%  (p=0.000 n=19+20)
BM_BlankLines/16                               34.9ms ± 2%  31.6ms ± 3%   -9.42%  (p=0.000 n=19+20)
BM_BlankLines/64                                102ms ± 2%    91ms ± 3%  -10.75%  (p=0.000 n=19+20)
BM_BlankLines/128                               190ms ± 2%   169ms ± 3%  -10.82%  (p=0.000 n=19+20)
BM_CommentLines/1/0/0                          16.2ms ± 1%  15.8ms ± 2%   -2.29%  (p=0.000 n=19+19)
BM_CommentLines/4/0/0                          21.0ms ± 1%  19.9ms ± 2%   -5.17%  (p=0.000 n=19+19)
BM_CommentLines/128/0/0                         237ms ± 1%   211ms ± 2%  -10.93%  (p=0.000 n=19+20)
BM_CommentLines/1/30/0                         17.0ms ± 1%  16.8ms ± 3%   -1.36%  (p=0.001 n=17+20)
BM_CommentLines/4/30/0                         27.2ms ± 2%  26.0ms ± 2%   -4.24%  (p=0.000 n=19+20)
BM_CommentLines/128/30/0                        255ms ± 2%   223ms ± 2%  -12.58%  (p=0.000 n=19+20)
BM_CommentLines/1/70/0                         19.7ms ± 1%  19.2ms ± 1%   -2.35%  (p=0.000 n=18+18)
BM_CommentLines/4/70/0                         29.0ms ± 2%  27.6ms ± 1%   -4.81%  (p=0.000 n=18+17)
BM_CommentLines/128/70/0                        273ms ± 7%   249ms ± 3%   -9.05%  (p=0.000 n=20+20)
BM_CommentLines/1/0/2                          16.4ms ± 2%  16.0ms ± 1%   -2.49%  (p=0.000 n=19+15)
BM_CommentLines/4/0/2                          22.0ms ± 2%  20.7ms ± 1%   -5.84%  (p=0.000 n=19+17)
BM_CommentLines/128/0/2                         256ms ± 2%   218ms ± 1%  -14.88%  (p=0.000 n=19+17)
BM_CommentLines/1/30/2                         17.4ms ± 1%  17.0ms ± 2%   -2.26%  (p=0.000 n=19+19)
BM_CommentLines/4/30/2                         28.5ms ± 1%  26.9ms ± 2%   -5.67%  (p=0.000 n=19+18)
BM_CommentLines/128/30/2                        288ms ± 5%   246ms ± 3%  -14.70%  (p=0.000 n=19+20)
BM_CommentLines/1/70/2                         19.9ms ± 1%  19.4ms ± 1%   -2.56%  (p=0.000 n=19+18)
BM_CommentLines/4/70/2                         30.0ms ± 2%  28.5ms ± 2%   -5.20%  (p=0.000 n=19+19)
BM_CommentLines/128/70/2                        300ms ± 3%   265ms ± 3%  -11.69%  (p=0.000 n=18+20)
BM_CommentLines/1/0/8                          16.7ms ± 2%  16.3ms ± 2%   -2.22%  (p=0.000 n=19+19)
BM_CommentLines/4/0/8                          26.4ms ± 1%  25.2ms ± 1%   -4.42%  (p=0.000 n=19+18)
BM_CommentLines/128/0/8                         285ms ± 2%   248ms ± 1%  -13.16%  (p=0.000 n=19+18)
BM_CommentLines/1/30/8                         17.9ms ± 2%  17.4ms ± 1%   -2.99%  (p=0.000 n=19+17)
BM_CommentLines/4/30/8                         32.0ms ± 1%  30.5ms ± 2%   -4.73%  (p=0.000 n=19+20)
BM_CommentLines/128/30/8                        320ms ± 2%   280ms ± 3%  -12.52%  (p=0.000 n=18+20)
BM_CommentLines/1/70/8                         20.7ms ± 2%  20.0ms ± 2%   -3.22%  (p=0.000 n=19+19)
BM_CommentLines/4/70/8                         33.5ms ± 2%  31.8ms ± 1%   -5.23%  (p=0.000 n=19+19)
BM_CommentLines/128/70/8                        338ms ± 3%   298ms ± 2%  -11.64%  (p=0.000 n=19+20)
```
2023-10-13 21:30:24 +00:00
Chandler Carruth 0e2b6c7f1a Optimize runs of horizontal whitespace. (#3288)
So, this is a somewhat fun, simple improvement. =] Just use a loop and
count runs of whitespace. I didn't even work all that hard to make the
loop fast, but it seems great. Makes long runs of indentation more than
2x faster at basically no code complexity.

I thought about doing this for vertical whitespace as well but it's not
easy to do, and didn't seem worth adding complexity. Huge runs of blank
lines aren't nearly as common as lots of indentation. I do have a plan
for an analogous optimization for comment blocks, but want to simplify
some other code first.

We could also make this (hilariously) faster with some judicious use of
SIMD or clever use of a string function, but it doesn't seem worth it
given how fast the simple loop is already.

I didn't work to get a high N count and so there's plenty of noise here,
but the benchmark data speaks for itself:

```
BM_ValidKeywords                               2.71ms ± 1%  2.75ms ± 1%   +1.32%  (p=0.016 n=5+5)
BM_ValidIdentifiers<1, 64, false>              9.71ms ± 2%  9.74ms ± 1%     ~     (p=1.000 n=5+5)
BM_ValidIdentifiers<1, 1, true>                3.17ms ± 1%  3.22ms ± 2%     ~     (p=0.151 n=5+5)
BM_ValidIdentifiers<3, 5, true>                11.5ms ± 2%  11.6ms ± 3%     ~     (p=0.548 n=5+5)
BM_ValidIdentifiers<3, 16, true>               11.3ms ± 3%  11.6ms ± 3%     ~     (p=0.095 n=5+5)
BM_ValidIdentifiers<12, 64, true>              12.8ms ± 1%  12.8ms ± 1%     ~     (p=1.000 n=5+5)
BM_HorizontalWhitespace/1                      11.6ms ± 1%  11.7ms ± 2%     ~     (p=0.310 n=5+5)
BM_HorizontalWhitespace/4                      12.9ms ± 1%  11.8ms ± 0%   -8.50%  (p=0.008 n=5+5)
BM_HorizontalWhitespace/16                     17.3ms ± 4%  12.5ms ± 1%  -27.69%  (p=0.008 n=5+5)
BM_HorizontalWhitespace/64                     28.9ms ± 3%  16.0ms ± 2%  -44.88%  (p=0.008 n=5+5)
BM_HorizontalWhitespace/128                    47.6ms ± 3%  21.0ms ± 0%  -55.88%  (p=0.016 n=5+4)
BM_RandomSource                                7.92ms ± 1%  7.70ms ± 3%   -2.74%  (p=0.016 n=5+5)
BM_GroupingSymbols/1/0/0                       5.92ms ± 2%  5.86ms ± 1%     ~     (p=0.310 n=5+5)
BM_GroupingSymbols/2/0/0                       5.30ms ± 1%  5.01ms ± 1%   -5.52%  (p=0.008 n=5+5)
BM_GroupingSymbols/3/0/0                       4.48ms ± 0%  3.95ms ± 1%  -11.69%  (p=0.008 n=5+5)
BM_GroupingSymbols/4/0/0                       4.61ms ± 1%  3.73ms ± 1%  -19.12%  (p=0.008 n=5+5)
BM_GroupingSymbols/8/0/0                       5.34ms ± 6%  3.05ms ± 1%  -42.82%  (p=0.008 n=5+5)
BM_GroupingSymbols/16/0/0                      6.44ms ± 1%  3.20ms ± 2%  -50.24%  (p=0.008 n=5+5)
BM_GroupingSymbols/32/0/0                      10.3ms ± 5%   4.2ms ± 1%  -59.81%  (p=0.008 n=5+5)
BM_GroupingSymbols/0/1/0                       5.17ms ± 2%  5.17ms ± 1%     ~     (p=0.690 n=5+5)
BM_GroupingSymbols/0/2/0                       4.04ms ± 1%  4.04ms ± 2%     ~     (p=1.000 n=5+5)
BM_GroupingSymbols/0/3/0                       2.89ms ± 1%  2.89ms ± 1%     ~     (p=1.000 n=5+5)
BM_GroupingSymbols/0/4/0                       2.54ms ± 1%  2.55ms ± 1%     ~     (p=0.421 n=5+5)
BM_GroupingSymbols/0/8/0                       1.65ms ± 2%  1.67ms ± 1%     ~     (p=0.310 n=5+5)
BM_GroupingSymbols/0/16/0                      1.18ms ± 1%  1.19ms ± 2%     ~     (p=0.222 n=5+5)
BM_GroupingSymbols/0/32/0                       948µs ± 1%   957µs ± 2%     ~     (p=0.310 n=5+5)
BM_GroupingSymbols/0/0/1                       5.17ms ± 1%  5.16ms ± 2%     ~     (p=0.841 n=5+5)
BM_GroupingSymbols/0/0/2                       4.03ms ± 2%  4.04ms ± 2%     ~     (p=0.548 n=5+5)
BM_GroupingSymbols/0/0/3                       2.89ms ± 1%  2.88ms ± 1%     ~     (p=0.841 n=5+5)
BM_GroupingSymbols/0/0/4                       2.54ms ± 1%  2.54ms ± 2%     ~     (p=0.548 n=5+5)
BM_GroupingSymbols/0/0/8                       1.66ms ± 3%  1.67ms ± 2%     ~     (p=0.690 n=5+5)
BM_GroupingSymbols/0/0/16                      1.19ms ± 1%  1.18ms ± 2%     ~     (p=0.548 n=5+5)
BM_GroupingSymbols/0/0/32                       949µs ± 2%   955µs ± 1%     ~     (p=0.310 n=5+5)
BM_GroupingSymbols/32/1/0                      10.0ms ± 1%   4.0ms ± 1%  -59.76%  (p=0.008 n=5+5)
BM_GroupingSymbols/32/2/0                      9.73ms ± 2%  3.92ms ± 2%  -59.73%  (p=0.008 n=5+5)
BM_GroupingSymbols/32/3/0                      9.40ms ± 2%  3.84ms ± 3%  -59.10%  (p=0.008 n=5+5)
BM_GroupingSymbols/32/4/0                      9.20ms ± 2%  3.74ms ± 1%  -59.35%  (p=0.008 n=5+5)
BM_GroupingSymbols/32/8/0                      8.38ms ± 2%  3.50ms ± 1%  -58.22%  (p=0.008 n=5+5)
BM_GroupingSymbols/32/16/0                     7.18ms ± 2%  3.06ms ± 3%  -57.41%  (p=0.008 n=5+5)
BM_GroupingSymbols/32/32/0                     5.53ms ± 1%  2.44ms ± 2%  -55.99%  (p=0.008 n=5+5)
BM_GroupingSymbols/32/32/1                     5.47ms ± 2%  2.40ms ± 2%  -56.08%  (p=0.008 n=5+5)
BM_GroupingSymbols/32/32/2                     5.41ms ± 3%  2.40ms ± 2%  -55.66%  (p=0.008 n=5+5)
BM_GroupingSymbols/32/32/3                     5.28ms ± 2%  2.37ms ± 2%  -55.18%  (p=0.008 n=5+5)
BM_GroupingSymbols/32/32/4                     5.25ms ± 2%  2.34ms ± 2%  -55.52%  (p=0.008 n=5+5)
BM_GroupingSymbols/32/32/8                     5.03ms ± 3%  2.25ms ± 1%  -55.29%  (p=0.008 n=5+5)
BM_GroupingSymbols/32/32/16                    4.62ms ± 1%  2.15ms ± 3%  -53.41%  (p=0.008 n=5+5)
BM_GroupingSymbols/32/32/32                    3.99ms ± 1%  1.86ms ± 1%  -53.24%  (p=0.008 n=5+5)
BM_BlankLines/1                                12.8ms ± 1%  12.6ms ± 2%     ~     (p=0.310 n=5+5)
BM_BlankLines/4                                16.0ms ± 3%  15.8ms ± 1%     ~     (p=0.310 n=5+5)
BM_BlankLines/16                               33.1ms ± 2%  32.1ms ± 1%   -3.29%  (p=0.008 n=5+5)
BM_BlankLines/64                               84.6ms ± 3%  84.2ms ± 3%     ~     (p=0.690 n=5+5)
BM_BlankLines/128                               159ms ± 4%   155ms ± 3%     ~     (p=0.310 n=5+5)
BM_CommentLines/1/0/0                          14.5ms ± 2%  14.5ms ± 2%     ~     (p=0.690 n=5+5)
BM_CommentLines/4/0/0                          19.0ms ± 1%  19.0ms ± 1%     ~     (p=0.548 n=5+5)
BM_CommentLines/128/0/0                         188ms ± 2%   186ms ± 1%     ~     (p=0.151 n=5+5)
BM_CommentLines/1/30/0                         14.8ms ± 3%  14.7ms ± 2%     ~     (p=0.421 n=5+5)
BM_CommentLines/4/30/0                         21.8ms ± 1%  21.3ms ± 4%     ~     (p=0.095 n=5+5)
BM_CommentLines/128/30/0                        201ms ± 1%   200ms ± 2%     ~     (p=0.421 n=5+5)
BM_CommentLines/1/70/0                         15.5ms ± 3%  15.2ms ± 4%     ~     (p=0.095 n=5+5)
BM_CommentLines/4/70/0                         23.2ms ± 1%  22.5ms ± 2%   -2.76%  (p=0.008 n=5+5)
BM_CommentLines/128/70/0                        213ms ± 1%   209ms ± 1%   -1.54%  (p=0.016 n=5+5)
BM_CommentLines/1/0/2                          15.3ms ± 1%  14.5ms ± 1%   -4.99%  (p=0.008 n=5+5)
BM_CommentLines/4/0/2                          21.4ms ± 1%  20.2ms ± 2%   -5.48%  (p=0.008 n=5+5)
BM_CommentLines/128/0/2                         242ms ± 2%   218ms ± 5%  -10.13%  (p=0.008 n=5+5)
BM_CommentLines/1/30/2                         15.7ms ± 4%  15.1ms ± 2%   -3.37%  (p=0.008 n=5+5)
BM_CommentLines/4/30/2                         24.3ms ± 1%  22.5ms ± 3%   -7.42%  (p=0.008 n=5+5)
BM_CommentLines/128/30/2                        268ms ± 2%   240ms ± 3%  -10.22%  (p=0.008 n=5+5)
BM_CommentLines/1/70/2                         16.1ms ± 2%  15.3ms ± 3%   -5.24%  (p=0.008 n=5+5)
BM_CommentLines/4/70/2                         25.7ms ± 3%  24.0ms ± 3%   -6.69%  (p=0.008 n=5+5)
BM_CommentLines/128/70/2                        272ms ± 1%   247ms ± 1%   -9.21%  (p=0.008 n=5+5)
BM_CommentLines/1/0/8                          17.2ms ± 5%  14.8ms ± 2%  -14.32%  (p=0.008 n=5+5)
BM_CommentLines/4/0/8                          30.4ms ± 2%  20.8ms ± 2%  -31.47%  (p=0.008 n=5+5)
BM_CommentLines/128/0/8                         463ms ± 1%   260ms ± 1%  -43.89%  (p=0.008 n=5+5)
BM_CommentLines/1/30/8                         17.2ms ± 2%  15.2ms ± 2%  -12.01%  (p=0.008 n=5+5)
BM_CommentLines/4/30/8                         32.4ms ± 1%  23.2ms ± 3%  -28.61%  (p=0.008 n=5+5)
BM_CommentLines/128/30/8                        498ms ± 3%   287ms ± 2%  -42.32%  (p=0.008 n=5+5)
BM_CommentLines/1/70/8                         17.6ms ± 2%  15.5ms ± 3%  -11.61%  (p=0.008 n=5+5)
BM_CommentLines/4/70/8                         34.0ms ± 4%  24.7ms ± 3%  -27.31%  (p=0.008 n=5+5)
BM_CommentLines/128/70/8                        497ms ± 2%   297ms ± 4%  -40.29%  (p=0.008 n=5+5)
```

Stacked on top of #3287 -- only the last commit should be reviewed here.
2023-10-13 05:11:04 +00:00
Chandler Carruth a79ea4b28d Do more precise table dispatch for symbols. (#3287)
When originally switching to the table dispatch approach we discussed
that it'd be nice to disentangle the monolithic symbol lexing routine
with this as we'll typically have fairly precise dispatch. This is
especially true for grouping symbols, which in Carbon are all
constructively one-character (at this point).

I think this provides a substantial improvement to the clarity of the
code by disentangling the different paths. It also allowed a bunch of
simplifications / clarifications to exactly what the behavior with
closing invalid groups actually involves currently.

This was initially motivated by code organization improvements, and any
performance wins were speculative. However, when benchmarking it
surfaced a problem that hadn't been clear -- we're generating too many
distinct functions here, and the table-based dispatch slows down in the
face of that.

So this PR also includes a fix for that, removing the template-generated
fan-out of dispatch functions for distinct symbols. Instead, we have
a dedicated table to translate one character into the token kinds. This
seems to work quite well, avoiding the huge branch-y structure and just
do fairly cheap table translation & dispatch for all one-character
symbols. Building the table requires the token kinds to be default
constructable, so this also enables that and arranges for the zero-value
kind to be the error kind.

Combined, this is a modest speedup for *non* grouping symbols (3-4%,
a bit noisy). And in some cases it is a huge speedup for grouping
symbols (>10%).

Raw benchmark data with 20 runs before/after -- despite the # of runs,
the grouping symbols benchmarks were frustratingly noisy in non-uniform
ways that couldn't fully be accounted for here. Still, this seems like
an overall improvement.

```
BM_RandomSource               7.98ms ± 2%  7.73ms ± 3%   -3.12%  (p=0.000 n=18+19)
BM_GroupingSymbols/1/0/0      5.90ms ± 2%  5.82ms ± 4%   -1.38%  (p=0.001 n=20+20)
BM_GroupingSymbols/2/0/0      5.21ms ± 2%  5.15ms ± 2%   -1.14%  (p=0.002 n=20+18)
BM_GroupingSymbols/3/0/0      4.42ms ± 2%  4.34ms ± 2%   -1.87%  (p=0.000 n=19+18)
BM_GroupingSymbols/4/0/0      4.29ms ± 2%  4.38ms ± 5%     ~     (p=0.297 n=17+20)
BM_GroupingSymbols/8/0/0      5.09ms ±10%  5.10ms ± 7%     ~     (p=0.919 n=18+20)
BM_GroupingSymbols/16/0/0     6.35ms ± 8%  6.29ms ± 6%     ~     (p=0.201 n=20+20)
BM_GroupingSymbols/32/0/0     9.88ms ± 2%  9.83ms ± 1%     ~     (p=0.167 n=18+20)
BM_GroupingSymbols/0/1/0      5.12ms ± 2%  5.01ms ± 2%   -2.14%  (p=0.000 n=20+19)
BM_GroupingSymbols/0/2/0      4.01ms ± 2%  3.93ms ± 4%   -2.03%  (p=0.000 n=20+19)
BM_GroupingSymbols/0/3/0      2.92ms ± 3%  2.81ms ± 2%   -3.87%  (p=0.000 n=20+19)
BM_GroupingSymbols/0/4/0      2.61ms ± 3%  2.47ms ± 2%   -5.30%  (p=0.000 n=20+18)
BM_GroupingSymbols/0/8/0      1.77ms ± 3%  1.61ms ± 2%   -8.91%  (p=0.000 n=18+19)
BM_GroupingSymbols/0/16/0     1.41ms ± 3%  1.16ms ± 4%  -17.66%  (p=0.000 n=20+20)
BM_GroupingSymbols/0/32/0     1.10ms ± 2%  0.92ms ± 3%  -16.36%  (p=0.000 n=20+17)
BM_GroupingSymbols/0/0/1      5.09ms ± 2%  5.03ms ± 3%   -1.11%  (p=0.001 n=20+18)
BM_GroupingSymbols/0/0/2      4.01ms ± 2%  3.91ms ± 2%   -2.67%  (p=0.000 n=20+18)
BM_GroupingSymbols/0/0/3      2.93ms ± 3%  2.81ms ± 2%   -4.23%  (p=0.000 n=20+19)
BM_GroupingSymbols/0/0/4      2.59ms ± 2%  2.48ms ± 3%   -4.48%  (p=0.000 n=20+19)
BM_GroupingSymbols/0/0/8      1.75ms ± 1%  1.62ms ± 3%   -7.65%  (p=0.000 n=17+19)
BM_GroupingSymbols/0/0/16     1.40ms ± 2%  1.15ms ± 3%  -17.67%  (p=0.000 n=19+20)
BM_GroupingSymbols/0/0/32     1.10ms ± 2%  0.92ms ± 3%  -15.91%  (p=0.000 n=20+19)
BM_GroupingSymbols/32/1/0     9.62ms ± 2%  9.65ms ± 2%     ~     (p=0.654 n=18+20)
BM_GroupingSymbols/32/2/0     9.41ms ± 2%  9.37ms ± 2%     ~     (p=0.095 n=20+19)
BM_GroupingSymbols/32/3/0     9.13ms ± 2%  9.13ms ± 3%     ~     (p=0.687 n=19+20)
BM_GroupingSymbols/32/4/0     8.93ms ± 1%  8.87ms ± 2%   -0.69%  (p=0.010 n=20+18)
BM_GroupingSymbols/32/8/0     8.15ms ± 2%  8.14ms ± 3%     ~     (p=0.729 n=19+19)
BM_GroupingSymbols/32/16/0    7.04ms ± 3%  6.92ms ± 1%   -1.71%  (p=0.000 n=20+18)
BM_GroupingSymbols/32/32/0    5.48ms ± 2%  5.38ms ± 3%   -1.81%  (p=0.000 n=20+20)
BM_GroupingSymbols/32/32/1    5.39ms ± 2%  5.29ms ± 2%   -1.87%  (p=0.000 n=19+19)
BM_GroupingSymbols/32/32/2    5.34ms ± 2%  5.21ms ± 1%   -2.45%  (p=0.000 n=20+18)
BM_GroupingSymbols/32/32/3    5.27ms ± 3%  5.16ms ± 2%   -2.18%  (p=0.000 n=20+19)
BM_GroupingSymbols/32/32/4    5.21ms ± 2%  5.10ms ± 3%   -2.11%  (p=0.000 n=19+20)
BM_GroupingSymbols/32/32/8    4.98ms ± 2%  4.83ms ± 2%   -2.85%  (p=0.000 n=19+19)
BM_GroupingSymbols/32/32/16   4.55ms ± 2%  4.45ms ± 2%   -2.25%  (p=0.000 n=18+20)
BM_GroupingSymbols/32/32/32   3.95ms ± 2%  3.84ms ± 2%   -2.98%  (p=0.000 n=19+20)
```
2023-10-13 03:58:25 +00:00
6ba8712fbd Predetermine all the line splits in the lexer. (#3278)
## Summary ##

Restructures the lexer to first scan the entire source text for newlines
and create all the line structures needed. Doing this up-front makes it
easy to produce an optimized version with minimal complexity. Currently,
it leverages the system `memchr`, but even when expanded to handle more
complex cases like CR+LF line endings, being isolated in this way will
result in a significantly simpler implementation. This change improves
the lexing of comment lines significantly by skipping their contents
immediately. The overhead of the pre-scan is unmeasurable in all
realistic benchmarks, and 10-30% in benchmarks consisting almost
entirely of blank lines or comments. The improvement of comment lexing
with average length comment lines mixed with code starts at 20% and goes
up. Regressing blank line handling for non-empty comments seems like the
right tradeoff (by far).

## Background and details ##

One weak point in the lexer implementation were large runs of comments.
While those aren't terribly common, they shouldn't present a hazard to
the lexer performance.

A bit more common is a pattern of comments like the following:

```carbon
  // Some method comment here.
  fn SomeMethodName(...) -> ...;

  // Some other method comment here.
  fn SomeOtherMethodName(...) -> ...;
```

Here, the lexer spends an inordinate amount of time getting from the
`\n` after the first semicolon to the `fn` token. It has to skip a blank
line, scan a line, find the `//` comment start, then scan to find the
next `\n`, and then scan horizontal whitespace, etc.

It is tempting to build a scanner *exactly* for this. In fact, I built
one, and I can publish it in a PR if folks are interested in what it
looks like. For x86-64, the PSHUFB trick used for scanning identifiers
technically works. But it is *complicated*. Amazingly so. 150 lines of
very subtle code with subtle performance pitfalls at ever turn. I felt
very uncomfortable submitting it, but we can always go back to it.
Nothing I've come up with quite matches it for sheer speed.

However, most of the complexity and time is spent walking from a `//` to
the end of the line. And *that* is something we can do very simply. In
fact, there is a tuned function for that in libc: `memchr`. Using this
we can build a very fast and much simpler scanner to split lines
up-front. This PR uses that and a carefully crafted fast loop to first
build up all the line info we need. Getting this to be as fast as
possible required some other subtle changes, for example always creating
a line structure that goes from the last `\n` and the end of the file.
We then back up the EOF token to avoid surfacing this to users. The nice
thing is the EOF token isn't part of any hot loop, and so this removes
branches everywhere else at modest complexity.

Once we have that, the rest of the lexer just needs to keep track of its
current line in order to record column offsets. I've taken some care to
try and optimize the lexer's usage of the line structures but there are
more opportunities here I suspect.

Combined, this gets much but not all of the performance of a huge SIMD
scanner for newline-through-to-next-token. For extreme cases (100s of
blank lines or empty comment lines between tokens) the holistic scanner
is of course still much faster, but those don't seem nearly worth the
cost.

I was initially worried about the overhead of taking two passes over the
source text, but in practice I've not been able to measure any
appreciable cost to this with realistic source files. In some cases
benchmarks with no newlines get *faster* because we use a much more
efficient approach to fetch the source text into cache as
a happenstance. And that in turn makes the byte-wise dispatched loop run
faster as it stalls less.

I'm particularly happy with this approach because it seems very clear
how to extend this to support CR+LF, bare CR, and even complex mixtures
without any significant speed cost. That wasn't at all true for the
other approaches explored.

I may try some further PRs to smooth out the last bits of slowness here,
but already this is working excellent for me in practice. My 10mloc test
case is down to 2.3s to lex.

## Raw benchmark data

Using a tool that runs benchmarks before and after and analyzes the
results, the following summarizes the CPU-time impact, each of these for
lexing 100k tokens:

```
BM_ValidKeywords                               2.57ms ± 1%  2.58ms ± 0%     ~     (p=0.190 n=5+4)
BM_ValidIdentifiers<1, 64, false>              9.24ms ± 4%  9.31ms ± 4%     ~     (p=0.421 n=5+5)
BM_ValidIdentifiers<1, 1, true>                3.05ms ± 4%  3.11ms ± 4%     ~     (p=0.222 n=5+5)
BM_ValidIdentifiers<3, 5, true>                10.9ms ± 0%  11.1ms ± 1%   +1.76%  (p=0.016 n=4+5)
BM_ValidIdentifiers<3, 16, true>               11.1ms ± 7%  11.0ms ± 1%     ~     (p=0.310 n=5+5)
BM_ValidIdentifiers<12, 64, true>              12.2ms ± 1%  12.3ms ± 2%     ~     (p=0.111 n=4+5)
BM_HorizontalWhitespace/1                      11.2ms ± 6%  11.1ms ± 2%     ~     (p=0.841 n=5+5)
BM_HorizontalWhitespace/4                      12.0ms ± 3%  12.0ms ± 2%     ~     (p=0.548 n=5+5)
BM_HorizontalWhitespace/16                     16.2ms ± 6%  15.9ms ± 8%     ~     (p=0.690 n=5+5)
BM_HorizontalWhitespace/64                     27.7ms ± 3%  28.4ms ± 3%     ~     (p=0.151 n=5+5)
BM_HorizontalWhitespace/128                    44.3ms ± 1%  45.6ms ± 6%   +3.15%  (p=0.032 n=5+5)
BM_RandomSource                                7.75ms ± 2%  7.72ms ± 1%     ~     (p=1.000 n=5+5)
BM_BlankLines/1                                11.7ms ± 1%  12.1ms ± 1%   +3.46%  (p=0.008 n=5+5)
BM_BlankLines/4                                14.0ms ± 2%  15.2ms ± 3%   +8.12%  (p=0.008 n=5+5)
BM_BlankLines/16                               23.5ms ± 2%  31.1ms ± 4%  +32.26%  (p=0.008 n=5+5)
BM_BlankLines/64                               75.3ms ± 1%  81.2ms ± 3%   +7.83%  (p=0.008 n=5+5)
BM_BlankLines/128                               133ms ± 3%   150ms ± 2%  +12.74%  (p=0.008 n=5+5)
BM_CommentLines/1/0/0                          13.1ms ± 0%  13.7ms ± 1%   +5.11%  (p=0.008 n=5+5)
BM_CommentLines/4/0/0                          16.6ms ± 1%  18.2ms ± 4%   +9.56%  (p=0.008 n=5+5)
BM_CommentLines/128/0/0                         169ms ± 4%   182ms ± 1%   +7.24%  (p=0.008 n=5+5)
BM_CommentLines/1/30/0                         18.7ms ± 5%  14.1ms ± 0%  -24.84%  (p=0.008 n=5+5)
BM_CommentLines/4/30/0                         36.5ms ± 6%  20.6ms ± 3%  -43.59%  (p=0.008 n=5+5)
BM_CommentLines/128/30/0                        525ms ± 4%   198ms ± 1%  -62.38%  (p=0.008 n=5+5)
BM_CommentLines/1/70/0                         23.4ms ± 6%  14.7ms ± 2%  -37.15%  (p=0.008 n=5+5)
BM_CommentLines/4/70/0                         53.3ms ± 7%  22.4ms ± 4%  -57.99%  (p=0.008 n=5+5)
BM_CommentLines/128/70/0                        1.05s ± 4%   0.21s ± 2%  -80.31%  (p=0.008 n=5+5)
BM_CommentLines/1/0/2                          14.1ms ± 6%  14.3ms ± 1%     ~     (p=0.151 n=5+5)
BM_CommentLines/4/0/2                          19.4ms ± 5%  20.1ms ± 1%     ~     (p=0.151 n=5+5)
BM_CommentLines/128/0/2                         238ms ± 8%   229ms ± 0%     ~     (p=0.151 n=5+5)
BM_CommentLines/1/30/2                         19.2ms ± 7%  14.6ms ± 1%  -23.87%  (p=0.008 n=5+5)
BM_CommentLines/4/30/2                         40.3ms ±13%  22.3ms ± 4%  -44.63%  (p=0.008 n=5+5)
BM_CommentLines/128/30/2                        568ms ± 7%   254ms ± 3%  -55.28%  (p=0.008 n=5+5)
BM_CommentLines/1/70/2                         23.3ms ± 1%  15.0ms ± 3%  -35.61%  (p=0.016 n=4+5)
BM_CommentLines/4/70/2                         57.2ms ± 9%  24.1ms ± 2%  -57.81%  (p=0.008 n=5+5)
BM_CommentLines/128/70/2                        1.07s ± 0%   0.26s ± 2%  -75.51%  (p=0.016 n=4+5)
BM_CommentLines/1/0/8                          15.9ms ± 7%  16.0ms ± 1%     ~     (p=0.151 n=5+5)
BM_CommentLines/4/0/8                          24.2ms ± 6%  27.9ms ± 2%  +15.36%  (p=0.008 n=5+5)
BM_CommentLines/128/0/8                         386ms ± 4%   445ms ± 1%  +15.28%  (p=0.008 n=5+5)
BM_CommentLines/1/30/8                         20.6ms ± 5%  16.3ms ± 1%  -20.95%  (p=0.008 n=5+5)
BM_CommentLines/4/30/8                         45.3ms ± 6%  30.3ms ± 3%  -32.98%  (p=0.008 n=5+5)
BM_CommentLines/128/30/8                        699ms ± 3%   477ms ± 3%  -31.83%  (p=0.008 n=5+5)
BM_CommentLines/1/70/8                         25.7ms ± 5%  16.8ms ± 2%  -34.67%  (p=0.008 n=5+5)
BM_CommentLines/4/70/8                         62.0ms ± 4%  31.6ms ± 2%  -49.10%  (p=0.008 n=5+5)
BM_CommentLines/128/70/8                        1.20s ± 2%   0.48s ± 4%  -59.60%  (p=0.008 n=5+5)
```

The horizontal whitespace benchmark (and all of the non-line-oriented
ones) are noisier than they appear here but do show some improvements
(surprisingly). My guess is that it has a lot to do with system load, as
the advantage is that we're using a vectorized loop to scan the text
first and then doing the byte-dispatched loop. So when the cache is
a bit slower to populate, the vectorized version starts to be faster.

---------

Co-authored-by: Richard Smith <richard@metafoo.co.uk>
Co-authored-by: josh11b <josh11b@users.noreply.github.com>
2023-10-12 07:13:44 +00:00
Chandler Carruth 6f5934a505 Unblock more lexer inlining. (#3274)
The big change is to make the lexer helpers have internal linkage,
making all of them easy to inline into single call sites.

Looking at the profile showed several other cases of unfortunate
out-of-line functions. Two were due to the code size produced for checks
-- those are switched to `DCHECK`s to remove that code from optimized
builds. The loss of coverage seems minor.

A last one was closing open groups. This was a surprising routine to be
hot, but it the paths to discover "nothing to do here" were intertwined
into the code. This PR extracts this common trace into a separate
function that delegates to the looping recovery path. This lets the hot
path inline easily.

At this point, for a large lexing benchmark I'm using, 50% of the time
is in the identifier hash table at this point. The remaining
improvements are to actually make some of the hot routines like symbol
lexing and comment lexing faster.
2023-10-10 21:30:07 +00:00
Chandler Carruth 03c3b86758 Switch lexer to fully table-driven design. (#3273)
This uses the musttail dispatched table approach to drive the entire
lexing. The result is that there is no main lexer loop at all in a
traditional sense, now everything is driven through tail recursive
dispatch on the next byte of the source text.

This should be easy to extend still -- the design pattern is to add
lexer methods for handling specific cases, and then add a dispatch
function to dispatch to them from the table. For example, we can add a
method that handles decoding UTF-8 outside of the ASCII subset and set
the table entries used by non-ASCII initial bytes to dispatch to it.

The performance is already surprisingly good, benchmarks show a modest
improvement across the board. That's despite there still being some
*serious* performance issues that I'll fix in a separate patch. There
are also opportunities to leverage this structure more heavily as needed
by putting more specialized dispatch targets in for specific bytes.

A follow-up PR will re-organize the functions here, as almost all of the
methods on the `Lexer` should become private, but I wanted to keep that
a separate change since it will probably render the diff even more hard
to read than it already is.
2023-10-07 03:35:24 +00:00
Chandler CarruthandRichard Smith d552545c6d Move dispatch routines to be static member functions. (#3265)
These routines have a regular signature and are used to build a table of
function pointers for fast dispatch. However, the previous approach
relied on lambdas to build these functions which resulted in very hard
to read functions in the profile and backtrace.

In preparation for expanding dispatch to handle (many) more cases in the
lexer and also enabling more aggressive inlining into the dispatch
routines, I wanted to tidy up how they appear.

This PR alone shouldn't have any interesting functional effect, it's
just re-organizing the code.

Co-authored-by: Richard Smith <richard@metafoo.co.uk>
2023-10-05 00:03:56 +00:00
Chandler Carruth a46ca6bf7a Add a start-of-file token and parse node. (#3263)
This removes a (very) hot branch in the lexer where we need to special
case when a token is the first token and can't look at its previous
token. It also seems like a generally nice change to the structure of
both the token buffer and parse tree as there are now bracketing
elements for both ends and we should be able to avoid similar branching
in the future.

Mostly mechanical updates to the lexer and parser code to handle this,
but also needed to special case the location information in the
autoupdate code. And then the usual large body of auto-updated tests.

No benchmark data for this change alone as in isolation and in the
current lexer structure it doesn't make a big difference. But this
branch was particularly difficult to handle when trying to update the
whitespace skipping code to be faster, and so I think it is worth
systematically avoiding the special case here.
2023-10-04 23:36:35 +00:00
Jon Ross-Perkins 2ecab78297 Support multi-file lex printing and testing. (#3214)
Lex now prints its yaml as:
```
- filename: name
  tokens: [ ... ]
```

New support in file_test allows the `filename` marker at the top to
define the default file number for later lines, meaning multi-file
output from lexing is now associated with the appropriate file. Similar
support will probably also apply to lowering, semir, and other places
that print a filename once for the full dump.

This hammers a bit at how line number replacements work in file_test,
allowing stacking them so that lex errors and stdout can both be
line-associated properly. I've tried to make the autoupdate more
frequently work in one pass, now also taking into account the file index
when doing line replacements.

There are still some issues with EndOfFile that it may be good to
discuss: because CHECK lines are appended to the end of the file now,
and the EndOfFile token points at the last line including comments, new
lex tests now take two runs to autoupdate (because without CHECK lines,
the EndOfFile points at a content line, which content is then inserted
after). Note that removing CHECK lines from the test is not a solution:
autoupdate also started inserting blank lines, which breaks this for a
similar reason. One solution here might be to not have EndOfFile
associate with a line or column, which has been a bit of an issue
regardless.

Also fixes a small issue with toolchain's autoupdate script.
2023-09-13 16:14:09 +00:00
Jon Ross-PerkinsandChandler Carruth ec182fb00d Rename lexer dir to lex (#3179)
Continuing with #3070. Just a dir and file rename (only prefix change is
lexer_file_test). Everything in the lex dir should be marked as a move.

Note, I think this closes #3070. There may still be further cleanup
later, but the organizational changes suggested there are being
completed.

---------

Co-authored-by: Chandler Carruth <chandlerc@gmail.com>
2023-09-01 02:39:04 +00:00