Skip to content

Commit

Permalink
buffer: optimize byteLength for short strings
Browse files Browse the repository at this point in the history
PR-URL: #54345
Reviewed-By: Daniel Lemire <daniel@lemire.me>
Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
  • Loading branch information
ronag authored and RafaelGSS committed Aug 21, 2024
1 parent 4e4d1de commit 7b641bc
Showing 1 changed file with 33 additions and 5 deletions.
38 changes: 33 additions & 5 deletions src/node_buffer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
#include "v8-fast-api-calls.h"
#include "v8.h"

#include <stdint.h>
#include <climits>
#include <cstring>
#include "nbytes.h"
Expand Down Expand Up @@ -741,13 +742,40 @@ uint32_t FastByteLengthUtf8(Local<Value> receiver,
if (source.length > 128) {
return simdutf::utf8_length_from_latin1(source.data, source.length);
}

uint32_t length = source.length;
uint32_t result = length;
const uint8_t* data = reinterpret_cast<const uint8_t*>(source.data);
for (uint32_t i = 0; i < length; ++i) {
result += (data[i] >> 7);
const auto input = reinterpret_cast<const uint8_t*>(source.data);

uint32_t answer = length;
uint32_t i = 0;

auto pop = [](uint64_t v) {
return static_cast<size_t>(((v >> 7) & UINT64_C(0x0101010101010101)) *
UINT64_C(0x0101010101010101) >>
56);
};

for (; i + 32 <= length; i += 32) {
uint64_t v;
memcpy(&v, input + i, 8);
answer += pop(v);
memcpy(&v, input + i + 8, 8);
answer += pop(v);
memcpy(&v, input + i + 16, 8);
answer += pop(v);
memcpy(&v, input + i + 24, 8);
answer += pop(v);
}
for (; i + 8 <= length; i += 8) {
uint64_t v;
memcpy(&v, input + i, 8);
answer += pop(v);
}
return result;
for (; i + 1 <= length; i += 1) {
answer += input[i] >> 7;
}

return answer;
}

static v8::CFunction fast_byte_length_utf8(
Expand Down

0 comments on commit 7b641bc

Please sign in to comment.