GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — buffer: optimize byteLength for short strings · nodejs/node@7b641bc

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • src
    • node_buffer.cc
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎src/node_buffer.cc‎

Copy file name to clipboardExpand all lines: src/node_buffer.cc
+33-5Lines changed: 33 additions & 5 deletions
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 (0)
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.