Once again positively surprised how well single-pass compilers synergize with modern branch predictors. The CPU time distribution in my single-pass compiler is roughly 40% lexing + 40% parsing and semantic analysis + 20% code generation (x86-64). The lexer is a conventional switch-based lexer and its hot paths are well optimized but don't use any crazy tricks: it processes input at ~0.75 GB/s on one thread. But 40% is enough that I thought I'd compare with a full AVX-512 SIMD lexer.
The main cause of the lackluster speed-up in the full compiler is a massive increase in branch mispredicts in the parser, despite identical branching structure to the old code. Basically, most of the mispredicted branches we previously paid for in the branch-based lexer were now appearing in the parser. I knew some of that would happen from the last time I did this kind of analysis years ago but seeing the net impact is still kind of shocking.
If you don't quite see what's going on, when you're running a single-pass compiler like this, you're finely interleaving the execution of all the different logical stages like lexing, parsing, semantic analysis and code generation. While that adds pressure to instruction and op caches, BTBs, etc, it also lets you condition on the recent branch history across stages. So the predictor can condition the predictions for parser branches based on recent branches in the lexer.
Anyway, this made me want to do a more precise analysis I didn't do back in the day. How many extra branch mispredicts appear in the lexer (not the parser) if you don't run it interleaved with the parser (the lexer branch predictions get to condition on parser branches, too). The lexer runs significantly faster (if you attribute time by instruction sampling) when interleaved with the parser and there's about 20% fewer mispredicts in the same lexer when run interleaved than when run standalone.
The TL; DR is that if you're writing a very fast compiler like this where lexing is a significant time cost, it's almost certainly not worth it to use even a hyper-optimized SIMD lexer. After all the experimentation back in the day, that was my final conclusion as well. The 5% is not worth giving up the flexibility of a conventional lexer, and that's just about the best case given that this is a micro-optimized full AVX-512 lexer on just about the fastest single core machine you can buy.
If your workload happens to be even more lexer dominant (i.e. not a programming language compiler) that trade-off would change. Regardless, mastering these SIMD techniques is very helpful in general for high-performance text processing and it's worth learning if you care about making things run fast on computers, so I'm not trying to discourage anyone and I'd love to be proven wrong specifically for the fast compiler use case.
As a coda, the per-core (2 hardware threads on 1 core) performance of the full compiler was actually slightly worse with the AVX-512 lexer than with the old lexer. I didn't do the microarchitectural analysis of that to confirm the root cause, but it makes total sense given that the AVX-512 lexer pass puts high pressure on the limited vector unit ports even with 1 thread. This kind of thing is why you should measure both per-thread and per-core performance for parallel-friendly workloads.