Skip to content

Commit 15c5f87

Browse files
ronagRafaelGSS
authored andcommitted
buffer: optimize byteLength for short strings
PR-URL: #54345 Reviewed-By: Daniel Lemire <[email protected]> Reviewed-By: Yagiz Nizipli <[email protected]> Reviewed-By: Matteo Collina <[email protected]>
1 parent 04fe033 commit 15c5f87

File tree

1 file changed

+33
-5
lines changed

1 file changed

+33
-5
lines changed

src/node_buffer.cc

+33-5
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
#include "v8-fast-api-calls.h"
3636
#include "v8.h"
3737

38+
#include <stdint.h>
3839
#include <climits>
3940
#include <cstring>
4041
#include "nbytes.h"
@@ -741,13 +742,40 @@ uint32_t FastByteLengthUtf8(Local<Value> receiver,
741742
if (source.length > 128) {
742743
return simdutf::utf8_length_from_latin1(source.data, source.length);
743744
}
745+
744746
uint32_t length = source.length;
745-
uint32_t result = length;
746-
const uint8_t* data = reinterpret_cast<const uint8_t*>(source.data);
747-
for (uint32_t i = 0; i < length; ++i) {
748-
result += (data[i] >> 7);
747+
const auto input = reinterpret_cast<const uint8_t*>(source.data);
748+
749+
uint32_t answer = length;
750+
uint32_t i = 0;
751+
752+
auto pop = [](uint64_t v) {
753+
return static_cast<size_t>(((v >> 7) & UINT64_C(0x0101010101010101)) *
754+
UINT64_C(0x0101010101010101) >>
755+
56);
756+
};
757+
758+
for (; i + 32 <= length; i += 32) {
759+
uint64_t v;
760+
memcpy(&v, input + i, 8);
761+
answer += pop(v);
762+
memcpy(&v, input + i + 8, 8);
763+
answer += pop(v);
764+
memcpy(&v, input + i + 16, 8);
765+
answer += pop(v);
766+
memcpy(&v, input + i + 24, 8);
767+
answer += pop(v);
768+
}
769+
for (; i + 8 <= length; i += 8) {
770+
uint64_t v;
771+
memcpy(&v, input + i, 8);
772+
answer += pop(v);
749773
}
750-
return result;
774+
for (; i + 1 <= length; i += 1) {
775+
answer += input[i] >> 7;
776+
}
777+
778+
return answer;
751779
}
752780

753781
static v8::CFunction fast_byte_length_utf8(

0 commit comments

Comments
 (0)