Skip to content

Commit a80f660

Browse files
lemirerichardlau
authored andcommitted
src: implement FastByteLengthUtf8 with simdutf::utf8_length_from_latin1
PR-URL: #50840 Reviewed-By: Yagiz Nizipli <yagiz.nizipli@sentry.io> Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Vinícius Lourenço Claro Cardoso <contact@viniciusl.com.br>
1 parent 454b4f8 commit a80f660

File tree

2 files changed

+10
-3
lines changed

2 files changed

+10
-3
lines changed

benchmark/buffers/buffer-bytelength-string.js

+4-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22
const common = require('../common');
33

44
const bench = common.createBenchmark(main, {
5-
type: ['one_byte', 'two_bytes', 'three_bytes', 'four_bytes'],
5+
type: ['one_byte', 'two_bytes', 'three_bytes',
6+
'four_bytes', 'latin1'],
67
encoding: ['utf8', 'base64'],
78
repeat: [1, 2, 16, 256], // x16
89
n: [4e6],
@@ -14,6 +15,8 @@ const chars = {
1415
two_bytes: 'ΰαβγδεζηθικλμνξο',
1516
three_bytes: '挰挱挲挳挴挵挶挷挸挹挺挻挼挽挾挿',
1617
four_bytes: '𠜎𠜱𠝹𠱓𠱸𠲖𠳏𠳕𠴕𠵼𠵿𠸎𠸏𠹷𠺝𠺢',
18+
latin1: 'Un homme sage est supérieur à toutes ' +
19+
'les insultes qui peuvent lui être adressées, et la meilleure réponse est la patience et la modération.',
1720
};
1821

1922
function getInput(type, repeat, encoding) {

src/node_buffer.cc

+6-2
Original file line numberDiff line numberDiff line change
@@ -743,13 +743,17 @@ void SlowByteLengthUtf8(const FunctionCallbackInfo<Value>& args) {
743743

744744
uint32_t FastByteLengthUtf8(Local<Value> receiver,
745745
const v8::FastOneByteString& source) {
746-
uint32_t result = 0;
746+
// For short inputs, the function call overhead to simdutf is maybe
747+
// not worth it, reserve simdutf for long strings.
748+
if (source.length > 128) {
749+
return simdutf::utf8_length_from_latin1(source.data, source.length);
750+
}
747751
uint32_t length = source.length;
752+
uint32_t result = length;
748753
const uint8_t* data = reinterpret_cast<const uint8_t*>(source.data);
749754
for (uint32_t i = 0; i < length; ++i) {
750755
result += (data[i] >> 7);
751756
}
752-
result += length;
753757
return result;
754758
}
755759

0 commit comments

Comments
 (0)