Skip to content

Commit

Permalink
buffer: optimize byteLength for short strings
Browse files Browse the repository at this point in the history
PR-URL: nodejs#54345
  • Loading branch information
ronag committed Aug 13, 2024
1 parent 298ff4f commit 6e3ab46
Showing 1 changed file with 27 additions and 5 deletions.
32 changes: 27 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 <bit>
#include <climits>
#include <cstring>
#include "nbytes.h"
Expand Down Expand Up @@ -752,13 +753,34 @@ 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;

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

return answer;
}

static v8::CFunction fast_byte_length_utf8(
Expand Down

0 comments on commit 6e3ab46

Please sign in to comment.