Skip to content

Commit 55227cf

Browse files
committed
buffer: optimize byteLength for short strings
1 parent 298ff4f commit 55227cf

File tree

1 file changed

+13
-1
lines changed

1 file changed

+13
-1
lines changed

src/node_buffer.cc

Lines changed: 13 additions & 1 deletion
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 <bit>
3839
#include <climits>
3940
#include <cstring>
4041
#include "nbytes.h"
@@ -752,12 +753,23 @@ uint32_t FastByteLengthUtf8(Local<Value> receiver,
752753
if (source.length > 128) {
753754
return simdutf::utf8_length_from_latin1(source.data, source.length);
754755
}
756+
755757
uint32_t length = source.length;
756758
uint32_t result = length;
757759
const uint8_t* data = reinterpret_cast<const uint8_t*>(source.data);
758-
for (uint32_t i = 0; i < length; ++i) {
760+
761+
uint32_t i = 0;
762+
const auto length8 = length & ~0x7;
763+
while (i < length8) {
764+
result += std::popcount(*reinterpret_cast<const uint64_t*>(data + i) & 0x8080808080808080);
765+
i += 8;
766+
}
767+
768+
while (i < length) {
759769
result += (data[i] >> 7);
770+
i++;
760771
}
772+
761773
return result;
762774
}
763775

0 commit comments

Comments
 (0)