Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit da31d84

Browse files
authoredFeb 25, 2025
Rollup merge of #137360 - real-eren:rustc_span/use-chunks-exact, r=Noratrieb
Use `as_chunks` in `analyze_source_file_sse2` Follow-up to #136460. Uses a slightly cleaner method of iterating over chunks of bytes.
2 parents fb31487 + 62f5a55 commit da31d84

File tree

2 files changed

+5
-7
lines changed

2 files changed

+5
-7
lines changed
 

‎compiler/rustc_span/src/analyze_source_file.rs

+4-7
Original file line numberDiff line numberDiff line change
@@ -68,21 +68,18 @@ cfg_match! {
6868

6969
const CHUNK_SIZE: usize = 16;
7070

71-
let src_bytes = src.as_bytes();
72-
73-
let chunk_count = src.len() / CHUNK_SIZE;
71+
let (chunks, tail) = src.as_bytes().as_chunks::<CHUNK_SIZE>();
7472

7573
// This variable keeps track of where we should start decoding a
7674
// chunk. If a multi-byte character spans across chunk boundaries,
7775
// we need to skip that part in the next chunk because we already
7876
// handled it.
7977
let mut intra_chunk_offset = 0;
8078

81-
for chunk_index in 0..chunk_count {
82-
let ptr = src_bytes.as_ptr() as *const __m128i;
79+
for (chunk_index, chunk) in chunks.iter().enumerate() {
8380
// We don't know if the pointer is aligned to 16 bytes, so we
8481
// use `loadu`, which supports unaligned loading.
85-
let chunk = unsafe { _mm_loadu_si128(ptr.add(chunk_index)) };
82+
let chunk = unsafe { _mm_loadu_si128(chunk.as_ptr() as *const __m128i) };
8683

8784
// For character in the chunk, see if its byte value is < 0, which
8885
// indicates that it's part of a UTF-8 char.
@@ -123,7 +120,7 @@ cfg_match! {
123120
}
124121

125122
// There might still be a tail left to analyze
126-
let tail_start = chunk_count * CHUNK_SIZE + intra_chunk_offset;
123+
let tail_start = src.len() - tail.len() + intra_chunk_offset;
127124
if tail_start < src.len() {
128125
analyze_source_file_generic(
129126
&src[tail_start..],

‎compiler/rustc_span/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
#![feature(round_char_boundary)]
3232
#![feature(rustc_attrs)]
3333
#![feature(rustdoc_internals)]
34+
#![feature(slice_as_chunks)]
3435
#![warn(unreachable_pub)]
3536
// tidy-alphabetical-end
3637

0 commit comments

Comments
 (0)
Failed to load comments.