Skip to content

Commit a6a2bd6

Browse files
committed
Performance fix: if the last block is 64 bytes, process it without copying
1 parent b492280 commit a6a2bd6

File tree

1 file changed

+9
-6
lines changed

1 file changed

+9
-6
lines changed

src/implementation/macros.rs

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -79,12 +79,11 @@ macro_rules! validate_utf8_basic_simd {
7979
const SIMDINPUT_LENGTH: usize = 64;
8080
let len = input.len();
8181
let mut state = SimdInput::new_utf8_checking_state();
82-
let lenminus64: usize = if len < 64 { 0 } else { len as usize - 64 };
8382
let mut idx: usize = 0;
8483
let mut tmpbuf = $buf2type::new();
8584

8685
let align: usize = core::mem::align_of::<$buf2type>();
87-
if lenminus64 >= 4096 {
86+
if len >= 4096 {
8887
let off = (input.as_ptr() as usize) % align;
8988
if off != 0 {
9089
let to_copy = align - off;
@@ -98,7 +97,10 @@ macro_rules! validate_utf8_basic_simd {
9897
idx += to_copy;
9998
}
10099
}
101-
while idx < lenminus64 {
100+
101+
let rem = len - idx;
102+
let iter_lim = idx + (rem - (rem % 64));
103+
while idx < iter_lim {
102104
let input = SimdInput::new(input.get_unchecked(idx as usize..));
103105
input.check_utf8(&mut state);
104106
idx += SIMDINPUT_LENGTH;
@@ -151,12 +153,11 @@ macro_rules! validate_utf8_compat_simd {
151153
const SIMDINPUT_LENGTH: usize = 64;
152154
let len = input.len();
153155
let mut state = SimdInput::new_utf8_checking_state();
154-
let lenminus64: usize = if len < 64 { 0 } else { len as usize - 64 };
155156
let mut idx: usize = 0;
156157
let mut tmpbuf = $buf2type::new();
157158

158159
let align: usize = core::mem::align_of::<$buf2type>();
159-
if lenminus64 >= 4096 {
160+
if len >= 4096 {
160161
let off = (input.as_ptr() as usize) % align;
161162
if off != 0 {
162163
let to_copy = align - off;
@@ -174,7 +175,9 @@ macro_rules! validate_utf8_compat_simd {
174175
}
175176
}
176177

177-
while idx < lenminus64 {
178+
let rem = len - idx;
179+
let iter_lim = idx + (rem - (rem % 64));
180+
while idx < iter_lim {
178181
let simd_input = SimdInput::new(input.get_unchecked(idx as usize..));
179182
simd_input.check_utf8(&mut state);
180183
if SimdInput::check_utf8_errors(&state) {

0 commit comments

Comments
 (0)