Optimize the horizontal space skip fast path for x86 (#8033)

This uses SSE2 to optimize the horizontal space skipping path when there
is more than a single character. That helps with indentation and other
edge cases and doesn't cost much because we only begin when >1
horizontal space to skip.

We use SSE2 here which is available ubiquitously as AVX2 doesn't provide
any significant advantage.

Agy's benchmarks (`./scripts/bench_runner.py --runs=12`, `-c opt
--copt=-march=x86-64-v3`, vs. `trunk`):
- `BM_HorizontalWhitespace/16`: -4.610% CPU time, +4.832% bytes/s &
tokens/s
- `BM_HorizontalWhitespace/64`: -45.977% CPU time, +85.106% bytes/s &
tokens/s
- `BM_HorizontalWhitespace/128`: 0.428x CPU time, 2.338x bytes/s &
tokens/s

Assisted-by: Antigravity with Gemini

---------

Co-authored-by: Geoff Romer <gromer@google.com>
This commit is contained in:
Chandler CarruthandGeoff Romer authored and GitHub committed 2026-10-26 22:46:56 +00:00
1 parent 7cc1d08989
commit 5f1dd4941a
1 file changed
+60 -7
+60 -7
View File
@@ -1010,18 +1010,64 @@ auto Lexer::MakeLines(llvm::StringRef source_text) -> void {
line_index_ = LineIndex(0);
}
auto Lexer::SkipHorizontalWhitespace(llvm::StringRef source_text,
ssize_t& position) -> void {
// Handle adjacent whitespace quickly. This comes up frequently for example
// due to indentation. We don't expect *huge* runs, so just use a scalar
// loop. While still scalar, this avoids repeated table dispatch and marking
// whitespace.
while (position < static_cast<ssize_t>(source_text.size()) &&
static auto SkipHorizontalWhitespaceScalar(llvm::StringRef source_text,
ssize_t& position) -> void {
const ssize_t size = source_text.size();
while (position < size &&
(source_text[position] == ' ' || source_text[position] == '\t')) {
++position;
}
}
#if CARBON_USE_SIMD && __x86_64__
// Advances `position` past contiguous `' '` and `'\t'` bytes in `source_text`
// using 16-byte SSE2 scans, falling back to `SkipHorizontalWhitespaceScalar`
// for any `< 16`-byte tail near EOF.
static auto SkipHorizontalWhitespaceX86(llvm::StringRef source_text,
ssize_t& position) -> void {
const ssize_t size = source_text.size();
const char* const data = source_text.data();
// Broadcast `' '` and `'\t'` across all 16 byte lanes of 128-bit registers.
const __m128i spaces = _mm_set1_epi8(' ');
const __m128i tabs = _mm_set1_epi8('\t');
while (LLVM_LIKELY(position + 16 <= size)) {
// Unaligned 16-byte load; the loop guard guarantees `[position, position +
// 16)` lies within `source_text` without requiring trailing buffer padding.
__m128i chunk =
_mm_loadu_si128(reinterpret_cast<const __m128i*>(data + position));
// `_mm_cmpeq_epi8` sets matching byte lanes to `0xFF` and others to `0x00`;
// OR-ing the two comparisons marks every `' '` or `'\t'` byte with `0xFF`.
__m128i is_ws = _mm_or_si128(_mm_cmpeq_epi8(chunk, spaces),
_mm_cmpeq_epi8(chunk, tabs));
// `_mm_movemask_epi8` packs bit 7 of each byte into the low 16 bits of an
// `int`. Invert and mask to 16 bits so set bits mark non-whitespace bytes.
// Most indentation runs are shorter than 16 characters, so predict a hit
// here and advance directly to the first non-whitespace byte with
// `tzcnt`/`bsf`
// (`std::countr_zero`) instead of looping per byte.
uint32_t non_ws_mask =
~static_cast<uint32_t>(_mm_movemask_epi8(is_ws)) & 0xFFFFu;
if (LLVM_LIKELY(non_ws_mask != 0)) {
position += std::countr_zero(non_ws_mask);
return;
}
position += 16;
}
SkipHorizontalWhitespaceScalar(source_text, position);
}
#endif
auto Lexer::SkipHorizontalWhitespace(llvm::StringRef source_text,
ssize_t& position) -> void {
// Handle adjacent whitespace quickly. This comes up frequently for example
// due to indentation.
#if CARBON_USE_SIMD && __x86_64__
SkipHorizontalWhitespaceX86(source_text, position);
#else
SkipHorizontalWhitespaceScalar(source_text, position);
#endif
}
auto Lexer::AdvanceToLine(llvm::StringRef source_text, ssize_t& position,
LineIndex to_line_index) -> void {
CARBON_DCHECK(to_line_index >= line_index_);
@@ -1037,6 +1083,13 @@ auto Lexer::LexHorizontalWhitespace(llvm::StringRef source_text,
ssize_t& position) -> void {
CARBON_DCHECK(source_text[position] == ' ' || source_text[position] == '\t');
NoteWhitespace();
++position;
// Check if we're already done before an optimized skip routine.
if (LLVM_LIKELY(position < static_cast<ssize_t>(source_text.size()) &&
source_text[position] != ' ' &&
source_text[position] != '\t')) {
return;
}
// Skip runs using an optimized code path.
SkipHorizontalWhitespace(source_text, position);
}