Commit 32bb197

Browse files
codebytereaduh95
authored andcommitted
src: use simdutf for two-byte strings in UTF-8 writes
StringBytes::Write() already used simdutf to encode one-byte strings as UTF-8 but sent every two-byte (UTF-16) string through v8::String::WriteUtf8V2(), which is several times slower. That path is behind Buffer.from(string), buf.write(), fs.write*() with string data and every string written to a libuv stream, and JSON.stringify() output is a two-byte string as soon as any value in the payload is outside Latin-1. Encode two-byte strings with simdutf as well whenever their UTF-8 form is guaranteed to fit in the target: well-formed input is converted directly, and input with unpaired surrogates is converted from a copy passed through simdutf::to_well_formed_utf16(), which replaces each unpaired surrogate with U+FFFD exactly like kReplaceInvalidUtf8 (this mirrors what TextEncoder already does). Writes that have to truncate at a character boundary keep using WriteUtf8V2(), so their output is byte-for-byte unchanged, and so do strings of up to 32 code units, for which V8 is already as fast (the same threshold TextEncoder uses). buf.write() of a 2 KiB two-byte string improves ~5x (astral-heavy and lone-surrogate strings ~3.5x and ~5x), Buffer.from() of a 64 KiB JSON string ~2.7x; one-byte strings are unaffected. Signed-off-by: Shelley Vohr <shelley.vohr@gmail.com> PR-URL: #65324 Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com> Reviewed-By: Gürgün Dayıoğlu <hey@gurgun.day> Reviewed-By: Daniel Lemire <daniel@lemire.me> Reviewed-By: James M Snell <jasnell@gmail.com>
1 parent f33dba7 commit 32bb197

3 files changed

Lines changed: 193 additions & 1 deletion

File tree

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
'use strict';
2+
3+
// buf.write(string, 'utf8') for strings whose in-memory representation is
4+
// one-byte (Latin-1) or two-byte (UTF-16), which take different encoder paths.
5+
constcommon=require('../common.js');
6+
constbench=common.createBenchmark(main,{
7+
chars: ['one-byte','two-byte','two-byte-astral','two-byte-lone-surrogate'],
8+
len: [16,256,2048,65536],
9+
n: [5e5],
10+
});
11+
12+
functionmakeString(chars,len){
13+
switch(chars){
14+
case'one-byte':
15+
return'aé'.repeat(len/2);
16+
case'two-byte':
17+
return'aé€日'.repeat(len/4);
18+
case'two-byte-astral':
19+
return'aé€日\u{1F600}'.repeat(len/6).padEnd(len,'a');
20+
case'two-byte-lone-surrogate':
21+
return'aé€日'.repeat(len/4-1)+'ab\ud800c';
22+
default:
23+
thrownewError(chars);
24+
}
25+
}
26+
27+
functionmain({ chars, len, n }){
28+
conststring=makeString(chars,len);
29+
constbuf=Buffer.allocUnsafe(Buffer.byteLength(string));
30+
if(len>=65536)n=Math.floor(n/32);
31+
bench.start();
32+
for(leti=0;i<n;++i){
33+
buf.write(string,0,'utf8');
34+
}
35+
bench.end(n);
36+
}

‎src/string_bytes.cc‎

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -310,9 +310,37 @@ size_t StringBytes::Write(Isolate* isolate,
310310
input_view.length(),
311311
buf,
312312
buflen);
313-
} else {
313+
} elseif (input_view.length() <= 32) {
314+
// V8 is as fast for tiny strings (same threshold TextEncoder uses).
314315
nbytes = str->WriteUtf8V2(
315316
isolate, buf, buflen, String::WriteFlags::kReplaceInvalidUtf8);
317+
} else {
318+
// Use simdutf for two-byte strings as well whenever the UTF-8 form
319+
// is guaranteed to fit; truncating writes (which must stop at a
320+
// character boundary) keep going through V8 so that their output
321+
// stays byte-for-byte identical.
322+
constchar16_t* data =
323+
reinterpret_cast<constchar16_t*>(input_view.data16());
324+
constsize_t length = input_view.length();
325+
MaybeStackBuffer<char16_t, 1024> well_formed;
326+
if (!simdutf::validate_utf16(data, length)) {
327+
// Unpaired surrogates: encode a copy in which each of them has been
328+
// replaced with U+FFFD, which is what kReplaceInvalidUtf8 produces.
329+
well_formed.AllocateSufficientStorage(length);
330+
simdutf::to_well_formed_utf16(data, length, well_formed.out());
331+
data = well_formed.out();
332+
}
333+
// A UTF-16 code unit never expands to more than 3 UTF-8 bytes, so
334+
// 3 * length is what StorageSize() hands most callers; only compute
335+
// the exact length when the buffer is smaller than that.
336+
if (buflen >= 3 * length ||
337+
buflen >= simdutf::utf8_length_from_utf16(data, length)) {
338+
nbytes = simdutf::convert_utf16_to_utf8(data, length, buf);
339+
} else {
340+
// Does not fit: let V8 truncate at a character boundary.
341+
nbytes = str->WriteUtf8V2(
342+
isolate, buf, buflen, String::WriteFlags::kReplaceInvalidUtf8);
343+
}
316344
}
317345
break;
318346

Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
'use strict';
2+
// UTF-8 encoding of two-byte (UTF-16) JS strings through the Buffer write
3+
// paths (Buffer.from, Buffer#write, Buffer.byteLength) must:
4+
// - produce standard UTF-8 for well-formed input of any size,
5+
// - replace lone surrogates with U+FFFD (EF BF BD),
6+
// - never write a partial character when the target is too small,
7+
// independent of which internal fast path handles the string.
8+
require('../common');
9+
constassert=require('assert');
10+
11+
// Reference encoder written out longhand so the test does not depend on the
12+
// implementation under test (TextEncoder shares code with it).
13+
functionutf8Reference(str){
14+
constout=[];
15+
for(leti=0;i<str.length;i++){
16+
letcp=str.charCodeAt(i);
17+
if(cp>=0xd800&&cp<=0xdbff){
18+
constnext=i+1<str.length ? str.charCodeAt(i+1) : 0;
19+
if(next>=0xdc00&&next<=0xdfff){
20+
cp=0x10000+((cp-0xd800)<<10)+(next-0xdc00);
21+
i++;
22+
}else{
23+
cp=0xfffd;
24+
}
25+
}elseif(cp>=0xdc00&&cp<=0xdfff){
26+
cp=0xfffd;
27+
}
28+
if(cp<0x80){
29+
out.push(cp);
30+
}elseif(cp<0x800){
31+
out.push(0xc0|(cp>>6),0x80|(cp&0x3f));
32+
}elseif(cp<0x10000){
33+
out.push(0xe0|(cp>>12),0x80|((cp>>6)&0x3f),0x80|(cp&0x3f));
34+
}else{
35+
out.push(0xf0|(cp>>18),0x80|((cp>>12)&0x3f),
36+
0x80|((cp>>6)&0x3f),0x80|(cp&0x3f));
37+
}
38+
}
39+
returnBuffer.from(out);
40+
}
41+
42+
functioncheckFull(str,label){
43+
constexpected=utf8Reference(str);
44+
assert.deepStrictEqual(Buffer.from(str,'utf8'),expected,`${label}: Buffer.from`);
45+
assert.strictEqual(Buffer.byteLength(str,'utf8'),expected.length,`${label}: byteLength`);
46+
// Exact-size target.
47+
constexact=Buffer.alloc(expected.length);
48+
assert.strictEqual(exact.write(str,'utf8'),expected.length,`${label}: write exact`);
49+
assert.deepStrictEqual(exact,expected,`${label}: write exact bytes`);
50+
// Oversized target (3 bytes per code unit is what most internal callers allocate).
51+
constbig=Buffer.alloc(str.length*3+7,0xaa);
52+
assert.strictEqual(big.write(str,2,'utf8'),expected.length,`${label}: write big`);
53+
assert.deepStrictEqual(big.subarray(2,2+expected.length),expected,`${label}: write big bytes`);
54+
assert.strictEqual(big[0],0xaa);
55+
assert.strictEqual(big[2+expected.length],0xaa,`${label}: no overrun`);
56+
}
57+
58+
// Truncating writes must stop before the first character that does not fit.
59+
functioncheckTruncation(str,label){
60+
constexpected=utf8Reference(str);
61+
for(letsize=0;size<=Math.min(expected.length,70);size++){
62+
consttarget=Buffer.alloc(size+1,0xaa);
63+
constn=target.write(str,0,size,'utf8');
64+
assert.ok(n<=size,`${label}: size=${size} wrote ${n}`);
65+
assert.deepStrictEqual(target.subarray(0,n),expected.subarray(0,n),`${label}: prefix size=${size}`);
66+
assert.strictEqual(target[size],0xaa,`${label}: overrun size=${size}`);
67+
// What was written must be a whole number of characters: the next byte in
68+
// the reference (if any) has to be a lead byte, not a continuation byte.
69+
if(n<expected.length){
70+
assert.notStrictEqual(expected[n]&0xc0,0x80,`${label}: split char at size=${size}`);
71+
// And it stopped only because the next character really did not fit.
72+
letnext=n+1;
73+
while(next<expected.length&&(expected[next]&0xc0)===0x80)next++;
74+
assert.ok(next>size,`${label}: stopped early at size=${size} (n=${n}, next=${next})`);
75+
}
76+
}
77+
}
78+
79+
// Force a two-byte representation even for ASCII/Latin-1 content by building
80+
// the string from a two-byte seed and slicing (V8 keeps the representation).
81+
functiontwoByte(str){
82+
consts=('\u{1F600}'+str).slice(2);
83+
assert.strictEqual(s,str);
84+
returns;
85+
}
86+
87+
constsamples={
88+
ascii: 'The quick brown fox jumps over the lazy dog 0123456789',
89+
latin1: 'français élan über naïve façade ÿ',
90+
bmp: '日本語テキストとハングル한국어',
91+
astral: 'emoji \u{1F600}\u{1F4A9} math \u{1D49C} han \u{20BB7}',
92+
mixed: 'a é 日 \u{1F600} b ü 本 \u{1F4A9}',
93+
};
94+
95+
for(const[name,base]ofObject.entries(samples)){
96+
for(constrepeatof[1,3,40,700,12000]){
97+
conststr=twoByte(base.repeat(repeat));
98+
checkFull(str,`${name} x${repeat}`);
99+
}
100+
checkTruncation(twoByte(base.repeat(3)),`${name} truncation`);
101+
}
102+
103+
// Lone surrogates in various positions and sizes -> U+FFFD, rest intact.
104+
consthigh='\ud83d';
105+
constlow='\ude00';
106+
constsurrogateCases={
107+
'lone high': `ab${high}cd`,
108+
'lone low': `ab${low}cd`,
109+
'reversed pair': `ab${low}${high}cd`,
110+
'high at end': `abcd${high}`,
111+
'low at start': `${low}abcd`,
112+
'high high low': `${high}${high}${low}x`,
113+
'pair then lone': `${high}${low}${high}`,
114+
'only lone': high,
115+
};
116+
for(const[name,base]ofObject.entries(surrogateCases)){
117+
for(constpadof['','x'.repeat(50),'é'.repeat(300),'日'.repeat(30000)]){
118+
conststr=pad+base+pad;
119+
checkFull(str,`${name} pad=${pad.length}`);
120+
}
121+
checkTruncation(base+'zz',`${name} truncation`);
122+
}
123+
124+
// Buffer.from of a large two-byte string equals TextEncoder output.
125+
{
126+
conststr=twoByte(samples.mixed.repeat(50000));
127+
assert.deepStrictEqual(Buffer.from(str),Buffer.from(newTextEncoder().encode(str)));
128+
}

0 commit comments

Comments
 (0)
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Add copy buttons to all
 blocks\n(function() {\n function addCopyButtons() {\n document.querySelectorAll('pre code').forEach(function(codeBlock) {\n if (codeBlock.parentElement.hasAttribute('data-copy-added')) return;\n codeBlock.parentElement.setAttribute('data-copy-added', 'true');\n \n var btn = document.createElement('button');\n btn.textContent = 'Copy';\n btn.style.cssText = 'position:absolute;top:4px;right:4px;padding:2px 8px;font-size:11px;background:#4ecdc4;border:none;border-radius:4px;color:#1a1a2e;cursor:pointer;opacity:0.7;transition:opacity 0.2s;';\n btn.onmouseover = function() { this.style.opacity = '1'; };\n btn.onmouseout = function() { this.style.opacity = '0.7'; };\n btn.onclick = function() {\n navigator.clipboard.writeText(codeBlock.textContent).then(function() {\n btn.textContent = 'Copied!';\n setTimeout(function() { btn.textContent = 'Copy'; }, 1500);\n });\n };\n codeBlock.parentElement.style.position = 'relative';\n codeBlock.parentElement.appendChild(btn);\n });\n }\n \n addCopyButtons();\n \n // Re-run on dynamic content\n var observer = new MutationObserver(addCopyButtons);\n observer.observe(document.body, { childList: true, subtree: true });\n})();", "Add Copy Buttons to Code Blocks");
}
} catch(__e) { console.warn('[Userscript:Add Copy Buttons to Code Blocks]', __e); }
})();
(function(){
try {
var __m = "github.com";
var __re = new RegExp('^' + "github\\.com" + '
Skip to content

Commit 32bb197

Browse files
codebytereaduh95
authored andcommitted
src: use simdutf for two-byte strings in UTF-8 writes
StringBytes::Write() already used simdutf to encode one-byte strings as UTF-8 but sent every two-byte (UTF-16) string through v8::String::WriteUtf8V2(), which is several times slower. That path is behind Buffer.from(string), buf.write(), fs.write*() with string data and every string written to a libuv stream, and JSON.stringify() output is a two-byte string as soon as any value in the payload is outside Latin-1. Encode two-byte strings with simdutf as well whenever their UTF-8 form is guaranteed to fit in the target: well-formed input is converted directly, and input with unpaired surrogates is converted from a copy passed through simdutf::to_well_formed_utf16(), which replaces each unpaired surrogate with U+FFFD exactly like kReplaceInvalidUtf8 (this mirrors what TextEncoder already does). Writes that have to truncate at a character boundary keep using WriteUtf8V2(), so their output is byte-for-byte unchanged, and so do strings of up to 32 code units, for which V8 is already as fast (the same threshold TextEncoder uses). buf.write() of a 2 KiB two-byte string improves ~5x (astral-heavy and lone-surrogate strings ~3.5x and ~5x), Buffer.from() of a 64 KiB JSON string ~2.7x; one-byte strings are unaffected. Signed-off-by: Shelley Vohr <shelley.vohr@gmail.com> PR-URL: #65324 Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com> Reviewed-By: Gürgün Dayıoğlu <hey@gurgun.day> Reviewed-By: Daniel Lemire <daniel@lemire.me> Reviewed-By: James M Snell <jasnell@gmail.com>
1 parent f33dba7 commit 32bb197

3 files changed

Lines changed: 193 additions & 1 deletion

File tree

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
'use strict';
2+
3+
// buf.write(string, 'utf8') for strings whose in-memory representation is
4+
// one-byte (Latin-1) or two-byte (UTF-16), which take different encoder paths.
5+
constcommon=require('../common.js');
6+
constbench=common.createBenchmark(main,{
7+
chars: ['one-byte','two-byte','two-byte-astral','two-byte-lone-surrogate'],
8+
len: [16,256,2048,65536],
9+
n: [5e5],
10+
});
11+
12+
functionmakeString(chars,len){
13+
switch(chars){
14+
case'one-byte':
15+
return'aé'.repeat(len/2);
16+
case'two-byte':
17+
return'aé€日'.repeat(len/4);
18+
case'two-byte-astral':
19+
return'aé€日\u{1F600}'.repeat(len/6).padEnd(len,'a');
20+
case'two-byte-lone-surrogate':
21+
return'aé€日'.repeat(len/4-1)+'ab\ud800c';
22+
default:
23+
thrownewError(chars);
24+
}
25+
}
26+
27+
functionmain({ chars, len, n }){
28+
conststring=makeString(chars,len);
29+
constbuf=Buffer.allocUnsafe(Buffer.byteLength(string));
30+
if(len>=65536)n=Math.floor(n/32);
31+
bench.start();
32+
for(leti=0;i<n;++i){
33+
buf.write(string,0,'utf8');
34+
}
35+
bench.end(n);
36+
}

‎src/string_bytes.cc‎

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -310,9 +310,37 @@ size_t StringBytes::Write(Isolate* isolate,
310310
input_view.length(),
311311
buf,
312312
buflen);
313-
} else {
313+
} elseif (input_view.length() <= 32) {
314+
// V8 is as fast for tiny strings (same threshold TextEncoder uses).
314315
nbytes = str->WriteUtf8V2(
315316
isolate, buf, buflen, String::WriteFlags::kReplaceInvalidUtf8);
317+
} else {
318+
// Use simdutf for two-byte strings as well whenever the UTF-8 form
319+
// is guaranteed to fit; truncating writes (which must stop at a
320+
// character boundary) keep going through V8 so that their output
321+
// stays byte-for-byte identical.
322+
constchar16_t* data =
323+
reinterpret_cast<constchar16_t*>(input_view.data16());
324+
constsize_t length = input_view.length();
325+
MaybeStackBuffer<char16_t, 1024> well_formed;
326+
if (!simdutf::validate_utf16(data, length)) {
327+
// Unpaired surrogates: encode a copy in which each of them has been
328+
// replaced with U+FFFD, which is what kReplaceInvalidUtf8 produces.
329+
well_formed.AllocateSufficientStorage(length);
330+
simdutf::to_well_formed_utf16(data, length, well_formed.out());
331+
data = well_formed.out();
332+
}
333+
// A UTF-16 code unit never expands to more than 3 UTF-8 bytes, so
334+
// 3 * length is what StorageSize() hands most callers; only compute
335+
// the exact length when the buffer is smaller than that.
336+
if (buflen >= 3 * length ||
337+
buflen >= simdutf::utf8_length_from_utf16(data, length)) {
338+
nbytes = simdutf::convert_utf16_to_utf8(data, length, buf);
339+
} else {
340+
// Does not fit: let V8 truncate at a character boundary.
341+
nbytes = str->WriteUtf8V2(
342+
isolate, buf, buflen, String::WriteFlags::kReplaceInvalidUtf8);
343+
}
316344
}
317345
break;
318346

Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
'use strict';
2+
// UTF-8 encoding of two-byte (UTF-16) JS strings through the Buffer write
3+
// paths (Buffer.from, Buffer#write, Buffer.byteLength) must:
4+
// - produce standard UTF-8 for well-formed input of any size,
5+
// - replace lone surrogates with U+FFFD (EF BF BD),
6+
// - never write a partial character when the target is too small,
7+
// independent of which internal fast path handles the string.
8+
require('../common');
9+
constassert=require('assert');
10+
11+
// Reference encoder written out longhand so the test does not depend on the
12+
// implementation under test (TextEncoder shares code with it).
13+
functionutf8Reference(str){
14+
constout=[];
15+
for(leti=0;i<str.length;i++){
16+
letcp=str.charCodeAt(i);
17+
if(cp>=0xd800&&cp<=0xdbff){
18+
constnext=i+1<str.length ? str.charCodeAt(i+1) : 0;
19+
if(next>=0xdc00&&next<=0xdfff){
20+
cp=0x10000+((cp-0xd800)<<10)+(next-0xdc00);
21+
i++;
22+
}else{
23+
cp=0xfffd;
24+
}
25+
}elseif(cp>=0xdc00&&cp<=0xdfff){
26+
cp=0xfffd;
27+
}
28+
if(cp<0x80){
29+
out.push(cp);
30+
}elseif(cp<0x800){
31+
out.push(0xc0|(cp>>6),0x80|(cp&0x3f));
32+
}elseif(cp<0x10000){
33+
out.push(0xe0|(cp>>12),0x80|((cp>>6)&0x3f),0x80|(cp&0x3f));
34+
}else{
35+
out.push(0xf0|(cp>>18),0x80|((cp>>12)&0x3f),
36+
0x80|((cp>>6)&0x3f),0x80|(cp&0x3f));
37+
}
38+
}
39+
returnBuffer.from(out);
40+
}
41+
42+
functioncheckFull(str,label){
43+
constexpected=utf8Reference(str);
44+
assert.deepStrictEqual(Buffer.from(str,'utf8'),expected,`${label}: Buffer.from`);
45+
assert.strictEqual(Buffer.byteLength(str,'utf8'),expected.length,`${label}: byteLength`);
46+
// Exact-size target.
47+
constexact=Buffer.alloc(expected.length);
48+
assert.strictEqual(exact.write(str,'utf8'),expected.length,`${label}: write exact`);
49+
assert.deepStrictEqual(exact,expected,`${label}: write exact bytes`);
50+
// Oversized target (3 bytes per code unit is what most internal callers allocate).
51+
constbig=Buffer.alloc(str.length*3+7,0xaa);
52+
assert.strictEqual(big.write(str,2,'utf8'),expected.length,`${label}: write big`);
53+
assert.deepStrictEqual(big.subarray(2,2+expected.length),expected,`${label}: write big bytes`);
54+
assert.strictEqual(big[0],0xaa);
55+
assert.strictEqual(big[2+expected.length],0xaa,`${label}: no overrun`);
56+
}
57+
58+
// Truncating writes must stop before the first character that does not fit.
59+
functioncheckTruncation(str,label){
60+
constexpected=utf8Reference(str);
61+
for(letsize=0;size<=Math.min(expected.length,70);size++){
62+
consttarget=Buffer.alloc(size+1,0xaa);
63+
constn=target.write(str,0,size,'utf8');
64+
assert.ok(n<=size,`${label}: size=${size} wrote ${n}`);
65+
assert.deepStrictEqual(target.subarray(0,n),expected.subarray(0,n),`${label}: prefix size=${size}`);
66+
assert.strictEqual(target[size],0xaa,`${label}: overrun size=${size}`);
67+
// What was written must be a whole number of characters: the next byte in
68+
// the reference (if any) has to be a lead byte, not a continuation byte.
69+
if(n<expected.length){
70+
assert.notStrictEqual(expected[n]&0xc0,0x80,`${label}: split char at size=${size}`);
71+
// And it stopped only because the next character really did not fit.
72+
letnext=n+1;
73+
while(next<expected.length&&(expected[next]&0xc0)===0x80)next++;
74+
assert.ok(next>size,`${label}: stopped early at size=${size} (n=${n}, next=${next})`);
75+
}
76+
}
77+
}
78+
79+
// Force a two-byte representation even for ASCII/Latin-1 content by building
80+
// the string from a two-byte seed and slicing (V8 keeps the representation).
81+
functiontwoByte(str){
82+
consts=('\u{1F600}'+str).slice(2);
83+
assert.strictEqual(s,str);
84+
returns;
85+
}
86+
87+
constsamples={
88+
ascii: 'The quick brown fox jumps over the lazy dog 0123456789',
89+
latin1: 'français élan über naïve façade ÿ',
90+
bmp: '日本語テキストとハングル한국어',
91+
astral: 'emoji \u{1F600}\u{1F4A9} math \u{1D49C} han \u{20BB7}',
92+
mixed: 'a é 日 \u{1F600} b ü 本 \u{1F4A9}',
93+
};
94+
95+
for(const[name,base]ofObject.entries(samples)){
96+
for(constrepeatof[1,3,40,700,12000]){
97+
conststr=twoByte(base.repeat(repeat));
98+
checkFull(str,`${name} x${repeat}`);
99+
}
100+
checkTruncation(twoByte(base.repeat(3)),`${name} truncation`);
101+
}
102+
103+
// Lone surrogates in various positions and sizes -> U+FFFD, rest intact.
104+
consthigh='\ud83d';
105+
constlow='\ude00';
106+
constsurrogateCases={
107+
'lone high': `ab${high}cd`,
108+
'lone low': `ab${low}cd`,
109+
'reversed pair': `ab${low}${high}cd`,
110+
'high at end': `abcd${high}`,
111+
'low at start': `${low}abcd`,
112+
'high high low': `${high}${high}${low}x`,
113+
'pair then lone': `${high}${low}${high}`,
114+
'only lone': high,
115+
};
116+
for(const[name,base]ofObject.entries(surrogateCases)){
117+
for(constpadof['','x'.repeat(50),'é'.repeat(300),'日'.repeat(30000)]){
118+
conststr=pad+base+pad;
119+
checkFull(str,`${name} pad=${pad.length}`);
120+
}
121+
checkTruncation(base+'zz',`${name} truncation`);
122+
}
123+
124+
// Buffer.from of a large two-byte string equals TextEncoder output.
125+
{
126+
conststr=twoByte(samples.mixed.repeat(50000));
127+
assert.deepStrictEqual(Buffer.from(str),Buffer.from(newTextEncoder().encode(str)));
128+
}

0 commit comments

Comments
 (0)
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Force GitHub README to respect dark mode\n(function() {\n var style = document.createElement('style');\n style.textContent = '\n .markdown-body {\n color-scheme: dark light;\n }\n .markdown-body pre { background: #161b22 !important; }\n .markdown-body code { background: rgba(110, 118, 129, 0.4) !important; }\n .markdown-body table th, .markdown-body table td { border-color: #30363d !important; }\n .markdown-body img { background: #0d1117; }\n .markdown-body blockquote { border-left-color: #8b949e; }\n .markdown-body hr { border-color: #30363d; }\n ';\n document.head.appendChild(style);\n})();", "GitHub Dark Mode README Fix"); } } catch(__e) { console.warn('[Userscript:GitHub Dark Mode README Fix]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + '
Skip to content

Commit 32bb197

Browse files
codebytereaduh95
authored andcommitted
src: use simdutf for two-byte strings in UTF-8 writes
StringBytes::Write() already used simdutf to encode one-byte strings as UTF-8 but sent every two-byte (UTF-16) string through v8::String::WriteUtf8V2(), which is several times slower. That path is behind Buffer.from(string), buf.write(), fs.write*() with string data and every string written to a libuv stream, and JSON.stringify() output is a two-byte string as soon as any value in the payload is outside Latin-1. Encode two-byte strings with simdutf as well whenever their UTF-8 form is guaranteed to fit in the target: well-formed input is converted directly, and input with unpaired surrogates is converted from a copy passed through simdutf::to_well_formed_utf16(), which replaces each unpaired surrogate with U+FFFD exactly like kReplaceInvalidUtf8 (this mirrors what TextEncoder already does). Writes that have to truncate at a character boundary keep using WriteUtf8V2(), so their output is byte-for-byte unchanged, and so do strings of up to 32 code units, for which V8 is already as fast (the same threshold TextEncoder uses). buf.write() of a 2 KiB two-byte string improves ~5x (astral-heavy and lone-surrogate strings ~3.5x and ~5x), Buffer.from() of a 64 KiB JSON string ~2.7x; one-byte strings are unaffected. Signed-off-by: Shelley Vohr <shelley.vohr@gmail.com> PR-URL: #65324 Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com> Reviewed-By: Gürgün Dayıoğlu <hey@gurgun.day> Reviewed-By: Daniel Lemire <daniel@lemire.me> Reviewed-By: James M Snell <jasnell@gmail.com>
1 parent f33dba7 commit 32bb197

3 files changed

Lines changed: 193 additions & 1 deletion

File tree

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
'use strict';
2+
3+
// buf.write(string, 'utf8') for strings whose in-memory representation is
4+
// one-byte (Latin-1) or two-byte (UTF-16), which take different encoder paths.
5+
constcommon=require('../common.js');
6+
constbench=common.createBenchmark(main,{
7+
chars: ['one-byte','two-byte','two-byte-astral','two-byte-lone-surrogate'],
8+
len: [16,256,2048,65536],
9+
n: [5e5],
10+
});
11+
12+
functionmakeString(chars,len){
13+
switch(chars){
14+
case'one-byte':
15+
return'aé'.repeat(len/2);
16+
case'two-byte':
17+
return'aé€日'.repeat(len/4);
18+
case'two-byte-astral':
19+
return'aé€日\u{1F600}'.repeat(len/6).padEnd(len,'a');
20+
case'two-byte-lone-surrogate':
21+
return'aé€日'.repeat(len/4-1)+'ab\ud800c';
22+
default:
23+
thrownewError(chars);
24+
}
25+
}
26+
27+
functionmain({ chars, len, n }){
28+
conststring=makeString(chars,len);
29+
constbuf=Buffer.allocUnsafe(Buffer.byteLength(string));
30+
if(len>=65536)n=Math.floor(n/32);
31+
bench.start();
32+
for(leti=0;i<n;++i){
33+
buf.write(string,0,'utf8');
34+
}
35+
bench.end(n);
36+
}

‎src/string_bytes.cc‎

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -310,9 +310,37 @@ size_t StringBytes::Write(Isolate* isolate,
310310
input_view.length(),
311311
buf,
312312
buflen);
313-
} else {
313+
} elseif (input_view.length() <= 32) {
314+
// V8 is as fast for tiny strings (same threshold TextEncoder uses).
314315
nbytes = str->WriteUtf8V2(
315316
isolate, buf, buflen, String::WriteFlags::kReplaceInvalidUtf8);
317+
} else {
318+
// Use simdutf for two-byte strings as well whenever the UTF-8 form
319+
// is guaranteed to fit; truncating writes (which must stop at a
320+
// character boundary) keep going through V8 so that their output
321+
// stays byte-for-byte identical.
322+
constchar16_t* data =
323+
reinterpret_cast<constchar16_t*>(input_view.data16());
324+
constsize_t length = input_view.length();
325+
MaybeStackBuffer<char16_t, 1024> well_formed;
326+
if (!simdutf::validate_utf16(data, length)) {
327+
// Unpaired surrogates: encode a copy in which each of them has been
328+
// replaced with U+FFFD, which is what kReplaceInvalidUtf8 produces.
329+
well_formed.AllocateSufficientStorage(length);
330+
simdutf::to_well_formed_utf16(data, length, well_formed.out());
331+
data = well_formed.out();
332+
}
333+
// A UTF-16 code unit never expands to more than 3 UTF-8 bytes, so
334+
// 3 * length is what StorageSize() hands most callers; only compute
335+
// the exact length when the buffer is smaller than that.
336+
if (buflen >= 3 * length ||
337+
buflen >= simdutf::utf8_length_from_utf16(data, length)) {
338+
nbytes = simdutf::convert_utf16_to_utf8(data, length, buf);
339+
} else {
340+
// Does not fit: let V8 truncate at a character boundary.
341+
nbytes = str->WriteUtf8V2(
342+
isolate, buf, buflen, String::WriteFlags::kReplaceInvalidUtf8);
343+
}
316344
}
317345
break;
318346

Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
'use strict';
2+
// UTF-8 encoding of two-byte (UTF-16) JS strings through the Buffer write
3+
// paths (Buffer.from, Buffer#write, Buffer.byteLength) must:
4+
// - produce standard UTF-8 for well-formed input of any size,
5+
// - replace lone surrogates with U+FFFD (EF BF BD),
6+
// - never write a partial character when the target is too small,
7+
// independent of which internal fast path handles the string.
8+
require('../common');
9+
constassert=require('assert');
10+
11+
// Reference encoder written out longhand so the test does not depend on the
12+
// implementation under test (TextEncoder shares code with it).
13+
functionutf8Reference(str){
14+
constout=[];
15+
for(leti=0;i<str.length;i++){
16+
letcp=str.charCodeAt(i);
17+
if(cp>=0xd800&&cp<=0xdbff){
18+
constnext=i+1<str.length ? str.charCodeAt(i+1) : 0;
19+
if(next>=0xdc00&&next<=0xdfff){
20+
cp=0x10000+((cp-0xd800)<<10)+(next-0xdc00);
21+
i++;
22+
}else{
23+
cp=0xfffd;
24+
}
25+
}elseif(cp>=0xdc00&&cp<=0xdfff){
26+
cp=0xfffd;
27+
}
28+
if(cp<0x80){
29+
out.push(cp);
30+
}elseif(cp<0x800){
31+
out.push(0xc0|(cp>>6),0x80|(cp&0x3f));
32+
}elseif(cp<0x10000){
33+
out.push(0xe0|(cp>>12),0x80|((cp>>6)&0x3f),0x80|(cp&0x3f));
34+
}else{
35+
out.push(0xf0|(cp>>18),0x80|((cp>>12)&0x3f),
36+
0x80|((cp>>6)&0x3f),0x80|(cp&0x3f));
37+
}
38+
}
39+
returnBuffer.from(out);
40+
}
41+
42+
functioncheckFull(str,label){
43+
constexpected=utf8Reference(str);
44+
assert.deepStrictEqual(Buffer.from(str,'utf8'),expected,`${label}: Buffer.from`);
45+
assert.strictEqual(Buffer.byteLength(str,'utf8'),expected.length,`${label}: byteLength`);
46+
// Exact-size target.
47+
constexact=Buffer.alloc(expected.length);
48+
assert.strictEqual(exact.write(str,'utf8'),expected.length,`${label}: write exact`);
49+
assert.deepStrictEqual(exact,expected,`${label}: write exact bytes`);
50+
// Oversized target (3 bytes per code unit is what most internal callers allocate).
51+
constbig=Buffer.alloc(str.length*3+7,0xaa);
52+
assert.strictEqual(big.write(str,2,'utf8'),expected.length,`${label}: write big`);
53+
assert.deepStrictEqual(big.subarray(2,2+expected.length),expected,`${label}: write big bytes`);
54+
assert.strictEqual(big[0],0xaa);
55+
assert.strictEqual(big[2+expected.length],0xaa,`${label}: no overrun`);
56+
}
57+
58+
// Truncating writes must stop before the first character that does not fit.
59+
functioncheckTruncation(str,label){
60+
constexpected=utf8Reference(str);
61+
for(letsize=0;size<=Math.min(expected.length,70);size++){
62+
consttarget=Buffer.alloc(size+1,0xaa);
63+
constn=target.write(str,0,size,'utf8');
64+
assert.ok(n<=size,`${label}: size=${size} wrote ${n}`);
65+
assert.deepStrictEqual(target.subarray(0,n),expected.subarray(0,n),`${label}: prefix size=${size}`);
66+
assert.strictEqual(target[size],0xaa,`${label}: overrun size=${size}`);
67+
// What was written must be a whole number of characters: the next byte in
68+
// the reference (if any) has to be a lead byte, not a continuation byte.
69+
if(n<expected.length){
70+
assert.notStrictEqual(expected[n]&0xc0,0x80,`${label}: split char at size=${size}`);
71+
// And it stopped only because the next character really did not fit.
72+
letnext=n+1;
73+
while(next<expected.length&&(expected[next]&0xc0)===0x80)next++;
74+
assert.ok(next>size,`${label}: stopped early at size=${size} (n=${n}, next=${next})`);
75+
}
76+
}
77+
}
78+
79+
// Force a two-byte representation even for ASCII/Latin-1 content by building
80+
// the string from a two-byte seed and slicing (V8 keeps the representation).
81+
functiontwoByte(str){
82+
consts=('\u{1F600}'+str).slice(2);
83+
assert.strictEqual(s,str);
84+
returns;
85+
}
86+
87+
constsamples={
88+
ascii: 'The quick brown fox jumps over the lazy dog 0123456789',
89+
latin1: 'français élan über naïve façade ÿ',
90+
bmp: '日本語テキストとハングル한국어',
91+
astral: 'emoji \u{1F600}\u{1F4A9} math \u{1D49C} han \u{20BB7}',
92+
mixed: 'a é 日 \u{1F600} b ü 本 \u{1F4A9}',
93+
};
94+
95+
for(const[name,base]ofObject.entries(samples)){
96+
for(constrepeatof[1,3,40,700,12000]){
97+
conststr=twoByte(base.repeat(repeat));
98+
checkFull(str,`${name} x${repeat}`);
99+
}
100+
checkTruncation(twoByte(base.repeat(3)),`${name} truncation`);
101+
}
102+
103+
// Lone surrogates in various positions and sizes -> U+FFFD, rest intact.
104+
consthigh='\ud83d';
105+
constlow='\ude00';
106+
constsurrogateCases={
107+
'lone high': `ab${high}cd`,
108+
'lone low': `ab${low}cd`,
109+
'reversed pair': `ab${low}${high}cd`,
110+
'high at end': `abcd${high}`,
111+
'low at start': `${low}abcd`,
112+
'high high low': `${high}${high}${low}x`,
113+
'pair then lone': `${high}${low}${high}`,
114+
'only lone': high,
115+
};
116+
for(const[name,base]ofObject.entries(surrogateCases)){
117+
for(constpadof['','x'.repeat(50),'é'.repeat(300),'日'.repeat(30000)]){
118+
conststr=pad+base+pad;
119+
checkFull(str,`${name} pad=${pad.length}`);
120+
}
121+
checkTruncation(base+'zz',`${name} truncation`);
122+
}
123+
124+
// Buffer.from of a large two-byte string equals TextEncoder output.
125+
{
126+
conststr=twoByte(samples.mixed.repeat(50000));
127+
assert.deepStrictEqual(Buffer.from(str),Buffer.from(newTextEncoder().encode(str)));
128+
}

0 commit comments

Comments
 (0)
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Highlight search terms from Google/DuckDuckGo/Bing referrer\n(function() {\n var ref = document.referrer;\n var terms = [];\n \n if (ref.includes('google.com') || ref.includes('duckduckgo.com') || ref.includes('bing.com')) {\n var url = new URL(ref);\n var q = url.searchParams.get('q') || url.searchParams.get('p');\n if (q) {\n terms = q.split(/\\s+/).filter(function(t) { return t.length > 2; });\n }\n }\n \n if (terms.length === 0) return;\n \n var style = document.createElement('style');\n style.textContent = '.userscript-highlight { background: #fbbf24; color: #1a1a2e; padding: 1px 3px; border-radius: 2px; }';\n document.head.appendChild(style);\n \n function highlight(node) {\n if (node.nodeType === 3) { // text node\n var text = node.textContent;\n var found = false;\n terms.forEach(function(term) {\n var regex = new RegExp('(' + term.replace(/[.*+?^${}()|[\\]\\\\]/g, '\\\\') + ')', 'gi');\n if (regex.test(text)) {\n found = true;\n var frag = document.createDocumentFragment();\n var parts = text.split(regex);\n parts.forEach(function(part, i) {\n if (i % 2 === 0) {\n frag.appendChild(document.createTextNode(part));\n } else {\n var span = document.createElement('span');\n span.className = 'userscript-highlight';\n span.textContent = part;\n frag.appendChild(span);\n }\n });\n node.parentNode.replaceChild(frag, node);\n }\n });\n } else if (node.nodeType === 1 && node.childNodes) { // element\n var skipTags = ['SCRIPT', 'STYLE', 'NOSCRIPT', 'TEXTAREA', 'INPUT', 'SELECT'];\n if (!skipTags.includes(node.tagName)) {\n Array.from(node.childNodes).forEach(highlight);\n }\n }\n }\n \n highlight(document.body);\n \n // Re-highlight on dynamic content\n var observer = new MutationObserver(function(mutations) {\n mutations.forEach(function(m) {\n m.addedNodes.forEach(function(node) {\n if (node.nodeType === 1 || node.nodeType === 3) highlight(node);\n });\n });\n });\n observer.observe(document.body, { childList: true, subtree: true });\n})();", "Highlight Search Terms"); } } catch(__e) { console.warn('[Userscript:Highlight Search Terms]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + '
Skip to content

Commit 32bb197

Browse files
codebytereaduh95
authored andcommitted
src: use simdutf for two-byte strings in UTF-8 writes
StringBytes::Write() already used simdutf to encode one-byte strings as UTF-8 but sent every two-byte (UTF-16) string through v8::String::WriteUtf8V2(), which is several times slower. That path is behind Buffer.from(string), buf.write(), fs.write*() with string data and every string written to a libuv stream, and JSON.stringify() output is a two-byte string as soon as any value in the payload is outside Latin-1. Encode two-byte strings with simdutf as well whenever their UTF-8 form is guaranteed to fit in the target: well-formed input is converted directly, and input with unpaired surrogates is converted from a copy passed through simdutf::to_well_formed_utf16(), which replaces each unpaired surrogate with U+FFFD exactly like kReplaceInvalidUtf8 (this mirrors what TextEncoder already does). Writes that have to truncate at a character boundary keep using WriteUtf8V2(), so their output is byte-for-byte unchanged, and so do strings of up to 32 code units, for which V8 is already as fast (the same threshold TextEncoder uses). buf.write() of a 2 KiB two-byte string improves ~5x (astral-heavy and lone-surrogate strings ~3.5x and ~5x), Buffer.from() of a 64 KiB JSON string ~2.7x; one-byte strings are unaffected. Signed-off-by: Shelley Vohr <shelley.vohr@gmail.com> PR-URL: #65324 Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com> Reviewed-By: Gürgün Dayıoğlu <hey@gurgun.day> Reviewed-By: Daniel Lemire <daniel@lemire.me> Reviewed-By: James M Snell <jasnell@gmail.com>
1 parent f33dba7 commit 32bb197

3 files changed

Lines changed: 193 additions & 1 deletion

File tree

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
'use strict';
2+
3+
// buf.write(string, 'utf8') for strings whose in-memory representation is
4+
// one-byte (Latin-1) or two-byte (UTF-16), which take different encoder paths.
5+
constcommon=require('../common.js');
6+
constbench=common.createBenchmark(main,{
7+
chars: ['one-byte','two-byte','two-byte-astral','two-byte-lone-surrogate'],
8+
len: [16,256,2048,65536],
9+
n: [5e5],
10+
});
11+
12+
functionmakeString(chars,len){
13+
switch(chars){
14+
case'one-byte':
15+
return'aé'.repeat(len/2);
16+
case'two-byte':
17+
return'aé€日'.repeat(len/4);
18+
case'two-byte-astral':
19+
return'aé€日\u{1F600}'.repeat(len/6).padEnd(len,'a');
20+
case'two-byte-lone-surrogate':
21+
return'aé€日'.repeat(len/4-1)+'ab\ud800c';
22+
default:
23+
thrownewError(chars);
24+
}
25+
}
26+
27+
functionmain({ chars, len, n }){
28+
conststring=makeString(chars,len);
29+
constbuf=Buffer.allocUnsafe(Buffer.byteLength(string));
30+
if(len>=65536)n=Math.floor(n/32);
31+
bench.start();
32+
for(leti=0;i<n;++i){
33+
buf.write(string,0,'utf8');
34+
}
35+
bench.end(n);
36+
}

‎src/string_bytes.cc‎

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -310,9 +310,37 @@ size_t StringBytes::Write(Isolate* isolate,
310310
input_view.length(),
311311
buf,
312312
buflen);
313-
} else {
313+
} elseif (input_view.length() <= 32) {
314+
// V8 is as fast for tiny strings (same threshold TextEncoder uses).
314315
nbytes = str->WriteUtf8V2(
315316
isolate, buf, buflen, String::WriteFlags::kReplaceInvalidUtf8);
317+
} else {
318+
// Use simdutf for two-byte strings as well whenever the UTF-8 form
319+
// is guaranteed to fit; truncating writes (which must stop at a
320+
// character boundary) keep going through V8 so that their output
321+
// stays byte-for-byte identical.
322+
constchar16_t* data =
323+
reinterpret_cast<constchar16_t*>(input_view.data16());
324+
constsize_t length = input_view.length();
325+
MaybeStackBuffer<char16_t, 1024> well_formed;
326+
if (!simdutf::validate_utf16(data, length)) {
327+
// Unpaired surrogates: encode a copy in which each of them has been
328+
// replaced with U+FFFD, which is what kReplaceInvalidUtf8 produces.
329+
well_formed.AllocateSufficientStorage(length);
330+
simdutf::to_well_formed_utf16(data, length, well_formed.out());
331+
data = well_formed.out();
332+
}
333+
// A UTF-16 code unit never expands to more than 3 UTF-8 bytes, so
334+
// 3 * length is what StorageSize() hands most callers; only compute
335+
// the exact length when the buffer is smaller than that.
336+
if (buflen >= 3 * length ||
337+
buflen >= simdutf::utf8_length_from_utf16(data, length)) {
338+
nbytes = simdutf::convert_utf16_to_utf8(data, length, buf);
339+
} else {
340+
// Does not fit: let V8 truncate at a character boundary.
341+
nbytes = str->WriteUtf8V2(
342+
isolate, buf, buflen, String::WriteFlags::kReplaceInvalidUtf8);
343+
}
316344
}
317345
break;
318346

Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
'use strict';
2+
// UTF-8 encoding of two-byte (UTF-16) JS strings through the Buffer write
3+
// paths (Buffer.from, Buffer#write, Buffer.byteLength) must:
4+
// - produce standard UTF-8 for well-formed input of any size,
5+
// - replace lone surrogates with U+FFFD (EF BF BD),
6+
// - never write a partial character when the target is too small,
7+
// independent of which internal fast path handles the string.
8+
require('../common');
9+
constassert=require('assert');
10+
11+
// Reference encoder written out longhand so the test does not depend on the
12+
// implementation under test (TextEncoder shares code with it).
13+
functionutf8Reference(str){
14+
constout=[];
15+
for(leti=0;i<str.length;i++){
16+
letcp=str.charCodeAt(i);
17+
if(cp>=0xd800&&cp<=0xdbff){
18+
constnext=i+1<str.length ? str.charCodeAt(i+1) : 0;
19+
if(next>=0xdc00&&next<=0xdfff){
20+
cp=0x10000+((cp-0xd800)<<10)+(next-0xdc00);
21+
i++;
22+
}else{
23+
cp=0xfffd;
24+
}
25+
}elseif(cp>=0xdc00&&cp<=0xdfff){
26+
cp=0xfffd;
27+
}
28+
if(cp<0x80){
29+
out.push(cp);
30+
}elseif(cp<0x800){
31+
out.push(0xc0|(cp>>6),0x80|(cp&0x3f));
32+
}elseif(cp<0x10000){
33+
out.push(0xe0|(cp>>12),0x80|((cp>>6)&0x3f),0x80|(cp&0x3f));
34+
}else{
35+
out.push(0xf0|(cp>>18),0x80|((cp>>12)&0x3f),
36+
0x80|((cp>>6)&0x3f),0x80|(cp&0x3f));
37+
}
38+
}
39+
returnBuffer.from(out);
40+
}
41+
42+
functioncheckFull(str,label){
43+
constexpected=utf8Reference(str);
44+
assert.deepStrictEqual(Buffer.from(str,'utf8'),expected,`${label}: Buffer.from`);
45+
assert.strictEqual(Buffer.byteLength(str,'utf8'),expected.length,`${label}: byteLength`);
46+
// Exact-size target.
47+
constexact=Buffer.alloc(expected.length);
48+
assert.strictEqual(exact.write(str,'utf8'),expected.length,`${label}: write exact`);
49+
assert.deepStrictEqual(exact,expected,`${label}: write exact bytes`);
50+
// Oversized target (3 bytes per code unit is what most internal callers allocate).
51+
constbig=Buffer.alloc(str.length*3+7,0xaa);
52+
assert.strictEqual(big.write(str,2,'utf8'),expected.length,`${label}: write big`);
53+
assert.deepStrictEqual(big.subarray(2,2+expected.length),expected,`${label}: write big bytes`);
54+
assert.strictEqual(big[0],0xaa);
55+
assert.strictEqual(big[2+expected.length],0xaa,`${label}: no overrun`);
56+
}
57+
58+
// Truncating writes must stop before the first character that does not fit.
59+
functioncheckTruncation(str,label){
60+
constexpected=utf8Reference(str);
61+
for(letsize=0;size<=Math.min(expected.length,70);size++){
62+
consttarget=Buffer.alloc(size+1,0xaa);
63+
constn=target.write(str,0,size,'utf8');
64+
assert.ok(n<=size,`${label}: size=${size} wrote ${n}`);
65+
assert.deepStrictEqual(target.subarray(0,n),expected.subarray(0,n),`${label}: prefix size=${size}`);
66+
assert.strictEqual(target[size],0xaa,`${label}: overrun size=${size}`);
67+
// What was written must be a whole number of characters: the next byte in
68+
// the reference (if any) has to be a lead byte, not a continuation byte.
69+
if(n<expected.length){
70+
assert.notStrictEqual(expected[n]&0xc0,0x80,`${label}: split char at size=${size}`);
71+
// And it stopped only because the next character really did not fit.
72+
letnext=n+1;
73+
while(next<expected.length&&(expected[next]&0xc0)===0x80)next++;
74+
assert.ok(next>size,`${label}: stopped early at size=${size} (n=${n}, next=${next})`);
75+
}
76+
}
77+
}
78+
79+
// Force a two-byte representation even for ASCII/Latin-1 content by building
80+
// the string from a two-byte seed and slicing (V8 keeps the representation).
81+
functiontwoByte(str){
82+
consts=('\u{1F600}'+str).slice(2);
83+
assert.strictEqual(s,str);
84+
returns;
85+
}
86+
87+
constsamples={
88+
ascii: 'The quick brown fox jumps over the lazy dog 0123456789',
89+
latin1: 'français élan über naïve façade ÿ',
90+
bmp: '日本語テキストとハングル한국어',
91+
astral: 'emoji \u{1F600}\u{1F4A9} math \u{1D49C} han \u{20BB7}',
92+
mixed: 'a é 日 \u{1F600} b ü 本 \u{1F4A9}',
93+
};
94+
95+
for(const[name,base]ofObject.entries(samples)){
96+
for(constrepeatof[1,3,40,700,12000]){
97+
conststr=twoByte(base.repeat(repeat));
98+
checkFull(str,`${name} x${repeat}`);
99+
}
100+
checkTruncation(twoByte(base.repeat(3)),`${name} truncation`);
101+
}
102+
103+
// Lone surrogates in various positions and sizes -> U+FFFD, rest intact.
104+
consthigh='\ud83d';
105+
constlow='\ude00';
106+
constsurrogateCases={
107+
'lone high': `ab${high}cd`,
108+
'lone low': `ab${low}cd`,
109+
'reversed pair': `ab${low}${high}cd`,
110+
'high at end': `abcd${high}`,
111+
'low at start': `${low}abcd`,
112+
'high high low': `${high}${high}${low}x`,
113+
'pair then lone': `${high}${low}${high}`,
114+
'only lone': high,
115+
};
116+
for(const[name,base]ofObject.entries(surrogateCases)){
117+
for(constpadof['','x'.repeat(50),'é'.repeat(300),'日'.repeat(30000)]){
118+
conststr=pad+base+pad;
119+
checkFull(str,`${name} pad=${pad.length}`);
120+
}
121+
checkTruncation(base+'zz',`${name} truncation`);
122+
}
123+
124+
// Buffer.from of a large two-byte string equals TextEncoder output.
125+
{
126+
conststr=twoByte(samples.mixed.repeat(50000));
127+
assert.deepStrictEqual(Buffer.from(str),Buffer.from(newTextEncoder().encode(str)));
128+
}

0 commit comments

Comments
 (0)
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Strip utm_, fbclid, gclid, etc. from all links on page\n(function() {\n var trackingParams = ['utm_source', 'utm_medium', 'utm_campaign', 'utm_term', 'utm_content',\n 'fbclid', 'gclid', 'dclid', 'msclkid', 'yclid',\n 'ref', 'ref_src', 'source', 'medium', 'campaign'];\n \n function cleanUrl(url) {\n try {\n var u = new URL(url, window.location.origin);\n var changed = false;\n trackingParams.forEach(function(p) {\n if (u.searchParams.has(p)) {\n u.searchParams.delete(p);\n changed = true;\n }\n });\n return changed ? u.toString() : url;\n } catch (e) {\n return url;\n }\n }\n \n function cleanLinks() {\n document.querySelectorAll('a[href]').forEach(function(a) {\n var clean = cleanUrl(a.href);\n if (clean !== a.href) a.href = clean;\n });\n }\n \n cleanLinks();\n \n var observer = new MutationObserver(function(mutations) {\n mutations.forEach(function(m) {\n m.addedNodes.forEach(function(node) {\n if (node.nodeType === 1) {\n if (node.tagName === 'A') cleanLinks();\n node.querySelectorAll('a[href]').forEach(function(a) {\n var clean = cleanUrl(a.href);\n if (clean !== a.href) a.href = clean;\n });\n }\n });\n });\n });\n observer.observe(document.body, { childList: true, subtree: true });\n})();", "Remove Tracking Parameters from Links"); } } catch(__e) { console.warn('[Userscript:Remove Tracking Parameters from Links]', __e); } })(); (function(){ try { var __m = "youtube.com"; var __re = new RegExp('^' + "youtube\\.com" + '
Skip to content

Commit 32bb197

Browse files
codebytereaduh95
authored andcommitted
src: use simdutf for two-byte strings in UTF-8 writes
StringBytes::Write() already used simdutf to encode one-byte strings as UTF-8 but sent every two-byte (UTF-16) string through v8::String::WriteUtf8V2(), which is several times slower. That path is behind Buffer.from(string), buf.write(), fs.write*() with string data and every string written to a libuv stream, and JSON.stringify() output is a two-byte string as soon as any value in the payload is outside Latin-1. Encode two-byte strings with simdutf as well whenever their UTF-8 form is guaranteed to fit in the target: well-formed input is converted directly, and input with unpaired surrogates is converted from a copy passed through simdutf::to_well_formed_utf16(), which replaces each unpaired surrogate with U+FFFD exactly like kReplaceInvalidUtf8 (this mirrors what TextEncoder already does). Writes that have to truncate at a character boundary keep using WriteUtf8V2(), so their output is byte-for-byte unchanged, and so do strings of up to 32 code units, for which V8 is already as fast (the same threshold TextEncoder uses). buf.write() of a 2 KiB two-byte string improves ~5x (astral-heavy and lone-surrogate strings ~3.5x and ~5x), Buffer.from() of a 64 KiB JSON string ~2.7x; one-byte strings are unaffected. Signed-off-by: Shelley Vohr <shelley.vohr@gmail.com> PR-URL: #65324 Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com> Reviewed-By: Gürgün Dayıoğlu <hey@gurgun.day> Reviewed-By: Daniel Lemire <daniel@lemire.me> Reviewed-By: James M Snell <jasnell@gmail.com>
1 parent f33dba7 commit 32bb197

3 files changed

Lines changed: 193 additions & 1 deletion

File tree

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
'use strict';
2+
3+
// buf.write(string, 'utf8') for strings whose in-memory representation is
4+
// one-byte (Latin-1) or two-byte (UTF-16), which take different encoder paths.
5+
constcommon=require('../common.js');
6+
constbench=common.createBenchmark(main,{
7+
chars: ['one-byte','two-byte','two-byte-astral','two-byte-lone-surrogate'],
8+
len: [16,256,2048,65536],
9+
n: [5e5],
10+
});
11+
12+
functionmakeString(chars,len){
13+
switch(chars){
14+
case'one-byte':
15+
return'aé'.repeat(len/2);
16+
case'two-byte':
17+
return'aé€日'.repeat(len/4);
18+
case'two-byte-astral':
19+
return'aé€日\u{1F600}'.repeat(len/6).padEnd(len,'a');
20+
case'two-byte-lone-surrogate':
21+
return'aé€日'.repeat(len/4-1)+'ab\ud800c';
22+
default:
23+
thrownewError(chars);
24+
}
25+
}
26+
27+
functionmain({ chars, len, n }){
28+
conststring=makeString(chars,len);
29+
constbuf=Buffer.allocUnsafe(Buffer.byteLength(string));
30+
if(len>=65536)n=Math.floor(n/32);
31+
bench.start();
32+
for(leti=0;i<n;++i){
33+
buf.write(string,0,'utf8');
34+
}
35+
bench.end(n);
36+
}

‎src/string_bytes.cc‎

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -310,9 +310,37 @@ size_t StringBytes::Write(Isolate* isolate,
310310
input_view.length(),
311311
buf,
312312
buflen);
313-
} else {
313+
} elseif (input_view.length() <= 32) {
314+
// V8 is as fast for tiny strings (same threshold TextEncoder uses).
314315
nbytes = str->WriteUtf8V2(
315316
isolate, buf, buflen, String::WriteFlags::kReplaceInvalidUtf8);
317+
} else {
318+
// Use simdutf for two-byte strings as well whenever the UTF-8 form
319+
// is guaranteed to fit; truncating writes (which must stop at a
320+
// character boundary) keep going through V8 so that their output
321+
// stays byte-for-byte identical.
322+
constchar16_t* data =
323+
reinterpret_cast<constchar16_t*>(input_view.data16());
324+
constsize_t length = input_view.length();
325+
MaybeStackBuffer<char16_t, 1024> well_formed;
326+
if (!simdutf::validate_utf16(data, length)) {
327+
// Unpaired surrogates: encode a copy in which each of them has been
328+
// replaced with U+FFFD, which is what kReplaceInvalidUtf8 produces.
329+
well_formed.AllocateSufficientStorage(length);
330+
simdutf::to_well_formed_utf16(data, length, well_formed.out());
331+
data = well_formed.out();
332+
}
333+
// A UTF-16 code unit never expands to more than 3 UTF-8 bytes, so
334+
// 3 * length is what StorageSize() hands most callers; only compute
335+
// the exact length when the buffer is smaller than that.
336+
if (buflen >= 3 * length ||
337+
buflen >= simdutf::utf8_length_from_utf16(data, length)) {
338+
nbytes = simdutf::convert_utf16_to_utf8(data, length, buf);
339+
} else {
340+
// Does not fit: let V8 truncate at a character boundary.
341+
nbytes = str->WriteUtf8V2(
342+
isolate, buf, buflen, String::WriteFlags::kReplaceInvalidUtf8);
343+
}
316344
}
317345
break;
318346

Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
'use strict';
2+
// UTF-8 encoding of two-byte (UTF-16) JS strings through the Buffer write
3+
// paths (Buffer.from, Buffer#write, Buffer.byteLength) must:
4+
// - produce standard UTF-8 for well-formed input of any size,
5+
// - replace lone surrogates with U+FFFD (EF BF BD),
6+
// - never write a partial character when the target is too small,
7+
// independent of which internal fast path handles the string.
8+
require('../common');
9+
constassert=require('assert');
10+
11+
// Reference encoder written out longhand so the test does not depend on the
12+
// implementation under test (TextEncoder shares code with it).
13+
functionutf8Reference(str){
14+
constout=[];
15+
for(leti=0;i<str.length;i++){
16+
letcp=str.charCodeAt(i);
17+
if(cp>=0xd800&&cp<=0xdbff){
18+
constnext=i+1<str.length ? str.charCodeAt(i+1) : 0;
19+
if(next>=0xdc00&&next<=0xdfff){
20+
cp=0x10000+((cp-0xd800)<<10)+(next-0xdc00);
21+
i++;
22+
}else{
23+
cp=0xfffd;
24+
}
25+
}elseif(cp>=0xdc00&&cp<=0xdfff){
26+
cp=0xfffd;
27+
}
28+
if(cp<0x80){
29+
out.push(cp);
30+
}elseif(cp<0x800){
31+
out.push(0xc0|(cp>>6),0x80|(cp&0x3f));
32+
}elseif(cp<0x10000){
33+
out.push(0xe0|(cp>>12),0x80|((cp>>6)&0x3f),0x80|(cp&0x3f));
34+
}else{
35+
out.push(0xf0|(cp>>18),0x80|((cp>>12)&0x3f),
36+
0x80|((cp>>6)&0x3f),0x80|(cp&0x3f));
37+
}
38+
}
39+
returnBuffer.from(out);
40+
}
41+
42+
functioncheckFull(str,label){
43+
constexpected=utf8Reference(str);
44+
assert.deepStrictEqual(Buffer.from(str,'utf8'),expected,`${label}: Buffer.from`);
45+
assert.strictEqual(Buffer.byteLength(str,'utf8'),expected.length,`${label}: byteLength`);
46+
// Exact-size target.
47+
constexact=Buffer.alloc(expected.length);
48+
assert.strictEqual(exact.write(str,'utf8'),expected.length,`${label}: write exact`);
49+
assert.deepStrictEqual(exact,expected,`${label}: write exact bytes`);
50+
// Oversized target (3 bytes per code unit is what most internal callers allocate).
51+
constbig=Buffer.alloc(str.length*3+7,0xaa);
52+
assert.strictEqual(big.write(str,2,'utf8'),expected.length,`${label}: write big`);
53+
assert.deepStrictEqual(big.subarray(2,2+expected.length),expected,`${label}: write big bytes`);
54+
assert.strictEqual(big[0],0xaa);
55+
assert.strictEqual(big[2+expected.length],0xaa,`${label}: no overrun`);
56+
}
57+
58+
// Truncating writes must stop before the first character that does not fit.
59+
functioncheckTruncation(str,label){
60+
constexpected=utf8Reference(str);
61+
for(letsize=0;size<=Math.min(expected.length,70);size++){
62+
consttarget=Buffer.alloc(size+1,0xaa);
63+
constn=target.write(str,0,size,'utf8');
64+
assert.ok(n<=size,`${label}: size=${size} wrote ${n}`);
65+
assert.deepStrictEqual(target.subarray(0,n),expected.subarray(0,n),`${label}: prefix size=${size}`);
66+
assert.strictEqual(target[size],0xaa,`${label}: overrun size=${size}`);
67+
// What was written must be a whole number of characters: the next byte in
68+
// the reference (if any) has to be a lead byte, not a continuation byte.
69+
if(n<expected.length){
70+
assert.notStrictEqual(expected[n]&0xc0,0x80,`${label}: split char at size=${size}`);
71+
// And it stopped only because the next character really did not fit.
72+
letnext=n+1;
73+
while(next<expected.length&&(expected[next]&0xc0)===0x80)next++;
74+
assert.ok(next>size,`${label}: stopped early at size=${size} (n=${n}, next=${next})`);
75+
}
76+
}
77+
}
78+
79+
// Force a two-byte representation even for ASCII/Latin-1 content by building
80+
// the string from a two-byte seed and slicing (V8 keeps the representation).
81+
functiontwoByte(str){
82+
consts=('\u{1F600}'+str).slice(2);
83+
assert.strictEqual(s,str);
84+
returns;
85+
}
86+
87+
constsamples={
88+
ascii: 'The quick brown fox jumps over the lazy dog 0123456789',
89+
latin1: 'français élan über naïve façade ÿ',
90+
bmp: '日本語テキストとハングル한국어',
91+
astral: 'emoji \u{1F600}\u{1F4A9} math \u{1D49C} han \u{20BB7}',
92+
mixed: 'a é 日 \u{1F600} b ü 本 \u{1F4A9}',
93+
};
94+
95+
for(const[name,base]ofObject.entries(samples)){
96+
for(constrepeatof[1,3,40,700,12000]){
97+
conststr=twoByte(base.repeat(repeat));
98+
checkFull(str,`${name} x${repeat}`);
99+
}
100+
checkTruncation(twoByte(base.repeat(3)),`${name} truncation`);
101+
}
102+
103+
// Lone surrogates in various positions and sizes -> U+FFFD, rest intact.
104+
consthigh='\ud83d';
105+
constlow='\ude00';
106+
constsurrogateCases={
107+
'lone high': `ab${high}cd`,
108+
'lone low': `ab${low}cd`,
109+
'reversed pair': `ab${low}${high}cd`,
110+
'high at end': `abcd${high}`,
111+
'low at start': `${low}abcd`,
112+
'high high low': `${high}${high}${low}x`,
113+
'pair then lone': `${high}${low}${high}`,
114+
'only lone': high,
115+
};
116+
for(const[name,base]ofObject.entries(surrogateCases)){
117+
for(constpadof['','x'.repeat(50),'é'.repeat(300),'日'.repeat(30000)]){
118+
conststr=pad+base+pad;
119+
checkFull(str,`${name} pad=${pad.length}`);
120+
}
121+
checkTruncation(base+'zz',`${name} truncation`);
122+
}
123+
124+
// Buffer.from of a large two-byte string equals TextEncoder output.
125+
{
126+
conststr=twoByte(samples.mixed.repeat(50000));
127+
assert.deepStrictEqual(Buffer.from(str),Buffer.from(newTextEncoder().encode(str)));
128+
}

0 commit comments

Comments
 (0)
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Auto-enable theater mode on YouTube\n(function() {\n function tryTheater() {\n var btn = document.querySelector('button[aria-label=\"Theater mode\"], ytd-player #player button[title=\"Theater mode\"]');\n if (btn && !btn.classList.contains('activated')) {\n btn.click();\n }\n }\n \n // Try immediately\n tryTheater();\n \n // Try after navigation (SPA)\n var lastUrl = location.href;\n setInterval(function() {\n if (location.href !== lastUrl) {\n lastUrl = location.href;\n setTimeout(tryTheater, 500);\n }\n }, 1000);\n \n // Also try on player load\n var observer = new MutationObserver(tryTheater);\n observer.observe(document.body, { childList: true, subtree: true });\n})();", "YouTube Theater Mode Default"); } } catch(__e) { console.warn('[Userscript:YouTube Theater Mode Default]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + '
Skip to content

Commit 32bb197

Browse files
codebytereaduh95
authored andcommitted
src: use simdutf for two-byte strings in UTF-8 writes
StringBytes::Write() already used simdutf to encode one-byte strings as UTF-8 but sent every two-byte (UTF-16) string through v8::String::WriteUtf8V2(), which is several times slower. That path is behind Buffer.from(string), buf.write(), fs.write*() with string data and every string written to a libuv stream, and JSON.stringify() output is a two-byte string as soon as any value in the payload is outside Latin-1. Encode two-byte strings with simdutf as well whenever their UTF-8 form is guaranteed to fit in the target: well-formed input is converted directly, and input with unpaired surrogates is converted from a copy passed through simdutf::to_well_formed_utf16(), which replaces each unpaired surrogate with U+FFFD exactly like kReplaceInvalidUtf8 (this mirrors what TextEncoder already does). Writes that have to truncate at a character boundary keep using WriteUtf8V2(), so their output is byte-for-byte unchanged, and so do strings of up to 32 code units, for which V8 is already as fast (the same threshold TextEncoder uses). buf.write() of a 2 KiB two-byte string improves ~5x (astral-heavy and lone-surrogate strings ~3.5x and ~5x), Buffer.from() of a 64 KiB JSON string ~2.7x; one-byte strings are unaffected. Signed-off-by: Shelley Vohr <shelley.vohr@gmail.com> PR-URL: #65324 Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com> Reviewed-By: Gürgün Dayıoğlu <hey@gurgun.day> Reviewed-By: Daniel Lemire <daniel@lemire.me> Reviewed-By: James M Snell <jasnell@gmail.com>
1 parent f33dba7 commit 32bb197

3 files changed

Lines changed: 193 additions & 1 deletion

File tree

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
'use strict';
2+
3+
// buf.write(string, 'utf8') for strings whose in-memory representation is
4+
// one-byte (Latin-1) or two-byte (UTF-16), which take different encoder paths.
5+
constcommon=require('../common.js');
6+
constbench=common.createBenchmark(main,{
7+
chars: ['one-byte','two-byte','two-byte-astral','two-byte-lone-surrogate'],
8+
len: [16,256,2048,65536],
9+
n: [5e5],
10+
});
11+
12+
functionmakeString(chars,len){
13+
switch(chars){
14+
case'one-byte':
15+
return'aé'.repeat(len/2);
16+
case'two-byte':
17+
return'aé€日'.repeat(len/4);
18+
case'two-byte-astral':
19+
return'aé€日\u{1F600}'.repeat(len/6).padEnd(len,'a');
20+
case'two-byte-lone-surrogate':
21+
return'aé€日'.repeat(len/4-1)+'ab\ud800c';
22+
default:
23+
thrownewError(chars);
24+
}
25+
}
26+
27+
functionmain({ chars, len, n }){
28+
conststring=makeString(chars,len);
29+
constbuf=Buffer.allocUnsafe(Buffer.byteLength(string));
30+
if(len>=65536)n=Math.floor(n/32);
31+
bench.start();
32+
for(leti=0;i<n;++i){
33+
buf.write(string,0,'utf8');
34+
}
35+
bench.end(n);
36+
}

‎src/string_bytes.cc‎

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -310,9 +310,37 @@ size_t StringBytes::Write(Isolate* isolate,
310310
input_view.length(),
311311
buf,
312312
buflen);
313-
} else {
313+
} elseif (input_view.length() <= 32) {
314+
// V8 is as fast for tiny strings (same threshold TextEncoder uses).
314315
nbytes = str->WriteUtf8V2(
315316
isolate, buf, buflen, String::WriteFlags::kReplaceInvalidUtf8);
317+
} else {
318+
// Use simdutf for two-byte strings as well whenever the UTF-8 form
319+
// is guaranteed to fit; truncating writes (which must stop at a
320+
// character boundary) keep going through V8 so that their output
321+
// stays byte-for-byte identical.
322+
constchar16_t* data =
323+
reinterpret_cast<constchar16_t*>(input_view.data16());
324+
constsize_t length = input_view.length();
325+
MaybeStackBuffer<char16_t, 1024> well_formed;
326+
if (!simdutf::validate_utf16(data, length)) {
327+
// Unpaired surrogates: encode a copy in which each of them has been
328+
// replaced with U+FFFD, which is what kReplaceInvalidUtf8 produces.
329+
well_formed.AllocateSufficientStorage(length);
330+
simdutf::to_well_formed_utf16(data, length, well_formed.out());
331+
data = well_formed.out();
332+
}
333+
// A UTF-16 code unit never expands to more than 3 UTF-8 bytes, so
334+
// 3 * length is what StorageSize() hands most callers; only compute
335+
// the exact length when the buffer is smaller than that.
336+
if (buflen >= 3 * length ||
337+
buflen >= simdutf::utf8_length_from_utf16(data, length)) {
338+
nbytes = simdutf::convert_utf16_to_utf8(data, length, buf);
339+
} else {
340+
// Does not fit: let V8 truncate at a character boundary.
341+
nbytes = str->WriteUtf8V2(
342+
isolate, buf, buflen, String::WriteFlags::kReplaceInvalidUtf8);
343+
}
316344
}
317345
break;
318346

Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
'use strict';
2+
// UTF-8 encoding of two-byte (UTF-16) JS strings through the Buffer write
3+
// paths (Buffer.from, Buffer#write, Buffer.byteLength) must:
4+
// - produce standard UTF-8 for well-formed input of any size,
5+
// - replace lone surrogates with U+FFFD (EF BF BD),
6+
// - never write a partial character when the target is too small,
7+
// independent of which internal fast path handles the string.
8+
require('../common');
9+
constassert=require('assert');
10+
11+
// Reference encoder written out longhand so the test does not depend on the
12+
// implementation under test (TextEncoder shares code with it).
13+
functionutf8Reference(str){
14+
constout=[];
15+
for(leti=0;i<str.length;i++){
16+
letcp=str.charCodeAt(i);
17+
if(cp>=0xd800&&cp<=0xdbff){
18+
constnext=i+1<str.length ? str.charCodeAt(i+1) : 0;
19+
if(next>=0xdc00&&next<=0xdfff){
20+
cp=0x10000+((cp-0xd800)<<10)+(next-0xdc00);
21+
i++;
22+
}else{
23+
cp=0xfffd;
24+
}
25+
}elseif(cp>=0xdc00&&cp<=0xdfff){
26+
cp=0xfffd;
27+
}
28+
if(cp<0x80){
29+
out.push(cp);
30+
}elseif(cp<0x800){
31+
out.push(0xc0|(cp>>6),0x80|(cp&0x3f));
32+
}elseif(cp<0x10000){
33+
out.push(0xe0|(cp>>12),0x80|((cp>>6)&0x3f),0x80|(cp&0x3f));
34+
}else{
35+
out.push(0xf0|(cp>>18),0x80|((cp>>12)&0x3f),
36+
0x80|((cp>>6)&0x3f),0x80|(cp&0x3f));
37+
}
38+
}
39+
returnBuffer.from(out);
40+
}
41+
42+
functioncheckFull(str,label){
43+
constexpected=utf8Reference(str);
44+
assert.deepStrictEqual(Buffer.from(str,'utf8'),expected,`${label}: Buffer.from`);
45+
assert.strictEqual(Buffer.byteLength(str,'utf8'),expected.length,`${label}: byteLength`);
46+
// Exact-size target.
47+
constexact=Buffer.alloc(expected.length);
48+
assert.strictEqual(exact.write(str,'utf8'),expected.length,`${label}: write exact`);
49+
assert.deepStrictEqual(exact,expected,`${label}: write exact bytes`);
50+
// Oversized target (3 bytes per code unit is what most internal callers allocate).
51+
constbig=Buffer.alloc(str.length*3+7,0xaa);
52+
assert.strictEqual(big.write(str,2,'utf8'),expected.length,`${label}: write big`);
53+
assert.deepStrictEqual(big.subarray(2,2+expected.length),expected,`${label}: write big bytes`);
54+
assert.strictEqual(big[0],0xaa);
55+
assert.strictEqual(big[2+expected.length],0xaa,`${label}: no overrun`);
56+
}
57+
58+
// Truncating writes must stop before the first character that does not fit.
59+
functioncheckTruncation(str,label){
60+
constexpected=utf8Reference(str);
61+
for(letsize=0;size<=Math.min(expected.length,70);size++){
62+
consttarget=Buffer.alloc(size+1,0xaa);
63+
constn=target.write(str,0,size,'utf8');
64+
assert.ok(n<=size,`${label}: size=${size} wrote ${n}`);
65+
assert.deepStrictEqual(target.subarray(0,n),expected.subarray(0,n),`${label}: prefix size=${size}`);
66+
assert.strictEqual(target[size],0xaa,`${label}: overrun size=${size}`);
67+
// What was written must be a whole number of characters: the next byte in
68+
// the reference (if any) has to be a lead byte, not a continuation byte.
69+
if(n<expected.length){
70+
assert.notStrictEqual(expected[n]&0xc0,0x80,`${label}: split char at size=${size}`);
71+
// And it stopped only because the next character really did not fit.
72+
letnext=n+1;
73+
while(next<expected.length&&(expected[next]&0xc0)===0x80)next++;
74+
assert.ok(next>size,`${label}: stopped early at size=${size} (n=${n}, next=${next})`);
75+
}
76+
}
77+
}
78+
79+
// Force a two-byte representation even for ASCII/Latin-1 content by building
80+
// the string from a two-byte seed and slicing (V8 keeps the representation).
81+
functiontwoByte(str){
82+
consts=('\u{1F600}'+str).slice(2);
83+
assert.strictEqual(s,str);
84+
returns;
85+
}
86+
87+
constsamples={
88+
ascii: 'The quick brown fox jumps over the lazy dog 0123456789',
89+
latin1: 'français élan über naïve façade ÿ',
90+
bmp: '日本語テキストとハングル한국어',
91+
astral: 'emoji \u{1F600}\u{1F4A9} math \u{1D49C} han \u{20BB7}',
92+
mixed: 'a é 日 \u{1F600} b ü 本 \u{1F4A9}',
93+
};
94+
95+
for(const[name,base]ofObject.entries(samples)){
96+
for(constrepeatof[1,3,40,700,12000]){
97+
conststr=twoByte(base.repeat(repeat));
98+
checkFull(str,`${name} x${repeat}`);
99+
}
100+
checkTruncation(twoByte(base.repeat(3)),`${name} truncation`);
101+
}
102+
103+
// Lone surrogates in various positions and sizes -> U+FFFD, rest intact.
104+
consthigh='\ud83d';
105+
constlow='\ude00';
106+
constsurrogateCases={
107+
'lone high': `ab${high}cd`,
108+
'lone low': `ab${low}cd`,
109+
'reversed pair': `ab${low}${high}cd`,
110+
'high at end': `abcd${high}`,
111+
'low at start': `${low}abcd`,
112+
'high high low': `${high}${high}${low}x`,
113+
'pair then lone': `${high}${low}${high}`,
114+
'only lone': high,
115+
};
116+
for(const[name,base]ofObject.entries(surrogateCases)){
117+
for(constpadof['','x'.repeat(50),'é'.repeat(300),'日'.repeat(30000)]){
118+
conststr=pad+base+pad;
119+
checkFull(str,`${name} pad=${pad.length}`);
120+
}
121+
checkTruncation(base+'zz',`${name} truncation`);
122+
}
123+
124+
// Buffer.from of a large two-byte string equals TextEncoder output.
125+
{
126+
conststr=twoByte(samples.mixed.repeat(50000));
127+
assert.deepStrictEqual(Buffer.from(str),Buffer.from(newTextEncoder().encode(str)));
128+
}

0 commit comments

Comments
 (0)
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Remove or un-stick sticky/fixed headers that block content\n(function() {\n function unstick() {\n document.querySelectorAll('header, nav, [role=\"banner\"], .header, .navbar, .sticky, .fixed-top, [style*=\"position: fixed\"], [style*=\"position:sticky\"]').forEach(function(el) {\n if (el.style.position === 'fixed' || el.style.position === 'sticky' || \n getComputedStyle(el).position === 'fixed' || getComputedStyle(el).position === 'sticky') {\n el.style.position = 'static';\n el.style.top = 'auto';\n el.style.zIndex = 'auto';\n }\n });\n }\n \n unstick();\n \n var observer = new MutationObserver(unstick);\n observer.observe(document.body, { childList: true, subtree: true, attributes: true, attributeFilter: ['style', 'class'] });\n})();", "Kill Sticky Headers"); } } catch(__e) { console.warn('[Userscript:Kill Sticky Headers]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + '
Skip to content

Commit 32bb197

Browse files
codebytereaduh95
authored andcommitted
src: use simdutf for two-byte strings in UTF-8 writes
StringBytes::Write() already used simdutf to encode one-byte strings as UTF-8 but sent every two-byte (UTF-16) string through v8::String::WriteUtf8V2(), which is several times slower. That path is behind Buffer.from(string), buf.write(), fs.write*() with string data and every string written to a libuv stream, and JSON.stringify() output is a two-byte string as soon as any value in the payload is outside Latin-1. Encode two-byte strings with simdutf as well whenever their UTF-8 form is guaranteed to fit in the target: well-formed input is converted directly, and input with unpaired surrogates is converted from a copy passed through simdutf::to_well_formed_utf16(), which replaces each unpaired surrogate with U+FFFD exactly like kReplaceInvalidUtf8 (this mirrors what TextEncoder already does). Writes that have to truncate at a character boundary keep using WriteUtf8V2(), so their output is byte-for-byte unchanged, and so do strings of up to 32 code units, for which V8 is already as fast (the same threshold TextEncoder uses). buf.write() of a 2 KiB two-byte string improves ~5x (astral-heavy and lone-surrogate strings ~3.5x and ~5x), Buffer.from() of a 64 KiB JSON string ~2.7x; one-byte strings are unaffected. Signed-off-by: Shelley Vohr <shelley.vohr@gmail.com> PR-URL: #65324 Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com> Reviewed-By: Gürgün Dayıoğlu <hey@gurgun.day> Reviewed-By: Daniel Lemire <daniel@lemire.me> Reviewed-By: James M Snell <jasnell@gmail.com>
1 parent f33dba7 commit 32bb197

3 files changed

Lines changed: 193 additions & 1 deletion

File tree

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
'use strict';
2+
3+
// buf.write(string, 'utf8') for strings whose in-memory representation is
4+
// one-byte (Latin-1) or two-byte (UTF-16), which take different encoder paths.
5+
constcommon=require('../common.js');
6+
constbench=common.createBenchmark(main,{
7+
chars: ['one-byte','two-byte','two-byte-astral','two-byte-lone-surrogate'],
8+
len: [16,256,2048,65536],
9+
n: [5e5],
10+
});
11+
12+
functionmakeString(chars,len){
13+
switch(chars){
14+
case'one-byte':
15+
return'aé'.repeat(len/2);
16+
case'two-byte':
17+
return'aé€日'.repeat(len/4);
18+
case'two-byte-astral':
19+
return'aé€日\u{1F600}'.repeat(len/6).padEnd(len,'a');
20+
case'two-byte-lone-surrogate':
21+
return'aé€日'.repeat(len/4-1)+'ab\ud800c';
22+
default:
23+
thrownewError(chars);
24+
}
25+
}
26+
27+
functionmain({ chars, len, n }){
28+
conststring=makeString(chars,len);
29+
constbuf=Buffer.allocUnsafe(Buffer.byteLength(string));
30+
if(len>=65536)n=Math.floor(n/32);
31+
bench.start();
32+
for(leti=0;i<n;++i){
33+
buf.write(string,0,'utf8');
34+
}
35+
bench.end(n);
36+
}

‎src/string_bytes.cc‎

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -310,9 +310,37 @@ size_t StringBytes::Write(Isolate* isolate,
310310
input_view.length(),
311311
buf,
312312
buflen);
313-
} else {
313+
} elseif (input_view.length() <= 32) {
314+
// V8 is as fast for tiny strings (same threshold TextEncoder uses).
314315
nbytes = str->WriteUtf8V2(
315316
isolate, buf, buflen, String::WriteFlags::kReplaceInvalidUtf8);
317+
} else {
318+
// Use simdutf for two-byte strings as well whenever the UTF-8 form
319+
// is guaranteed to fit; truncating writes (which must stop at a
320+
// character boundary) keep going through V8 so that their output
321+
// stays byte-for-byte identical.
322+
constchar16_t* data =
323+
reinterpret_cast<constchar16_t*>(input_view.data16());
324+
constsize_t length = input_view.length();
325+
MaybeStackBuffer<char16_t, 1024> well_formed;
326+
if (!simdutf::validate_utf16(data, length)) {
327+
// Unpaired surrogates: encode a copy in which each of them has been
328+
// replaced with U+FFFD, which is what kReplaceInvalidUtf8 produces.
329+
well_formed.AllocateSufficientStorage(length);
330+
simdutf::to_well_formed_utf16(data, length, well_formed.out());
331+
data = well_formed.out();
332+
}
333+
// A UTF-16 code unit never expands to more than 3 UTF-8 bytes, so
334+
// 3 * length is what StorageSize() hands most callers; only compute
335+
// the exact length when the buffer is smaller than that.
336+
if (buflen >= 3 * length ||
337+
buflen >= simdutf::utf8_length_from_utf16(data, length)) {
338+
nbytes = simdutf::convert_utf16_to_utf8(data, length, buf);
339+
} else {
340+
// Does not fit: let V8 truncate at a character boundary.
341+
nbytes = str->WriteUtf8V2(
342+
isolate, buf, buflen, String::WriteFlags::kReplaceInvalidUtf8);
343+
}
316344
}
317345
break;
318346

Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
'use strict';
2+
// UTF-8 encoding of two-byte (UTF-16) JS strings through the Buffer write
3+
// paths (Buffer.from, Buffer#write, Buffer.byteLength) must:
4+
// - produce standard UTF-8 for well-formed input of any size,
5+
// - replace lone surrogates with U+FFFD (EF BF BD),
6+
// - never write a partial character when the target is too small,
7+
// independent of which internal fast path handles the string.
8+
require('../common');
9+
constassert=require('assert');
10+
11+
// Reference encoder written out longhand so the test does not depend on the
12+
// implementation under test (TextEncoder shares code with it).
13+
functionutf8Reference(str){
14+
constout=[];
15+
for(leti=0;i<str.length;i++){
16+
letcp=str.charCodeAt(i);
17+
if(cp>=0xd800&&cp<=0xdbff){
18+
constnext=i+1<str.length ? str.charCodeAt(i+1) : 0;
19+
if(next>=0xdc00&&next<=0xdfff){
20+
cp=0x10000+((cp-0xd800)<<10)+(next-0xdc00);
21+
i++;
22+
}else{
23+
cp=0xfffd;
24+
}
25+
}elseif(cp>=0xdc00&&cp<=0xdfff){
26+
cp=0xfffd;
27+
}
28+
if(cp<0x80){
29+
out.push(cp);
30+
}elseif(cp<0x800){
31+
out.push(0xc0|(cp>>6),0x80|(cp&0x3f));
32+
}elseif(cp<0x10000){
33+
out.push(0xe0|(cp>>12),0x80|((cp>>6)&0x3f),0x80|(cp&0x3f));
34+
}else{
35+
out.push(0xf0|(cp>>18),0x80|((cp>>12)&0x3f),
36+
0x80|((cp>>6)&0x3f),0x80|(cp&0x3f));
37+
}
38+
}
39+
returnBuffer.from(out);
40+
}
41+
42+
functioncheckFull(str,label){
43+
constexpected=utf8Reference(str);
44+
assert.deepStrictEqual(Buffer.from(str,'utf8'),expected,`${label}: Buffer.from`);
45+
assert.strictEqual(Buffer.byteLength(str,'utf8'),expected.length,`${label}: byteLength`);
46+
// Exact-size target.
47+
constexact=Buffer.alloc(expected.length);
48+
assert.strictEqual(exact.write(str,'utf8'),expected.length,`${label}: write exact`);
49+
assert.deepStrictEqual(exact,expected,`${label}: write exact bytes`);
50+
// Oversized target (3 bytes per code unit is what most internal callers allocate).
51+
constbig=Buffer.alloc(str.length*3+7,0xaa);
52+
assert.strictEqual(big.write(str,2,'utf8'),expected.length,`${label}: write big`);
53+
assert.deepStrictEqual(big.subarray(2,2+expected.length),expected,`${label}: write big bytes`);
54+
assert.strictEqual(big[0],0xaa);
55+
assert.strictEqual(big[2+expected.length],0xaa,`${label}: no overrun`);
56+
}
57+
58+
// Truncating writes must stop before the first character that does not fit.
59+
functioncheckTruncation(str,label){
60+
constexpected=utf8Reference(str);
61+
for(letsize=0;size<=Math.min(expected.length,70);size++){
62+
consttarget=Buffer.alloc(size+1,0xaa);
63+
constn=target.write(str,0,size,'utf8');
64+
assert.ok(n<=size,`${label}: size=${size} wrote ${n}`);
65+
assert.deepStrictEqual(target.subarray(0,n),expected.subarray(0,n),`${label}: prefix size=${size}`);
66+
assert.strictEqual(target[size],0xaa,`${label}: overrun size=${size}`);
67+
// What was written must be a whole number of characters: the next byte in
68+
// the reference (if any) has to be a lead byte, not a continuation byte.
69+
if(n<expected.length){
70+
assert.notStrictEqual(expected[n]&0xc0,0x80,`${label}: split char at size=${size}`);
71+
// And it stopped only because the next character really did not fit.
72+
letnext=n+1;
73+
while(next<expected.length&&(expected[next]&0xc0)===0x80)next++;
74+
assert.ok(next>size,`${label}: stopped early at size=${size} (n=${n}, next=${next})`);
75+
}
76+
}
77+
}
78+
79+
// Force a two-byte representation even for ASCII/Latin-1 content by building
80+
// the string from a two-byte seed and slicing (V8 keeps the representation).
81+
functiontwoByte(str){
82+
consts=('\u{1F600}'+str).slice(2);
83+
assert.strictEqual(s,str);
84+
returns;
85+
}
86+
87+
constsamples={
88+
ascii: 'The quick brown fox jumps over the lazy dog 0123456789',
89+
latin1: 'français élan über naïve façade ÿ',
90+
bmp: '日本語テキストとハングル한국어',
91+
astral: 'emoji \u{1F600}\u{1F4A9} math \u{1D49C} han \u{20BB7}',
92+
mixed: 'a é 日 \u{1F600} b ü 本 \u{1F4A9}',
93+
};
94+
95+
for(const[name,base]ofObject.entries(samples)){
96+
for(constrepeatof[1,3,40,700,12000]){
97+
conststr=twoByte(base.repeat(repeat));
98+
checkFull(str,`${name} x${repeat}`);
99+
}
100+
checkTruncation(twoByte(base.repeat(3)),`${name} truncation`);
101+
}
102+
103+
// Lone surrogates in various positions and sizes -> U+FFFD, rest intact.
104+
consthigh='\ud83d';
105+
constlow='\ude00';
106+
constsurrogateCases={
107+
'lone high': `ab${high}cd`,
108+
'lone low': `ab${low}cd`,
109+
'reversed pair': `ab${low}${high}cd`,
110+
'high at end': `abcd${high}`,
111+
'low at start': `${low}abcd`,
112+
'high high low': `${high}${high}${low}x`,
113+
'pair then lone': `${high}${low}${high}`,
114+
'only lone': high,
115+
};
116+
for(const[name,base]ofObject.entries(surrogateCases)){
117+
for(constpadof['','x'.repeat(50),'é'.repeat(300),'日'.repeat(30000)]){
118+
conststr=pad+base+pad;
119+
checkFull(str,`${name} pad=${pad.length}`);
120+
}
121+
checkTruncation(base+'zz',`${name} truncation`);
122+
}
123+
124+
// Buffer.from of a large two-byte string equals TextEncoder output.
125+
{
126+
conststr=twoByte(samples.mixed.repeat(50000));
127+
assert.deepStrictEqual(Buffer.from(str),Buffer.from(newTextEncoder().encode(str)));
128+
}

0 commit comments

Comments
 (0)
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Universal Dark Mode - works on any site\n(function() {\n var enabled = true;\n \n function applyDarkMode() {\n if (!enabled) return;\n \n // Create style element if it doesn't exist\n var style = document.getElementById('universal-dark-mode-style');\n if (!style) {\n style = document.createElement('style');\n style.id = 'universal-dark-mode-style';\n document.head.appendChild(style);\n }\n \n // Dark mode CSS - inverts colors but preserves images/video\n style.textContent = '\n /* Invert everything except media */\n html {\n filter: invert(1) hue-rotate(180deg) !important;\n background: #1a1a2e !important;\n }\n \n /* Restore images, videos, iframes, canvas */\n img, video, iframe, canvas, svg, picture, [style*=\"background-image\"] {\n filter: invert(1) hue-rotate(180deg) !important;\n }\n \n /* Preserve specific elements that should not be inverted */\n .no-dark-mode, .no-dark-mode *,\n [data-theme=\"light\"], [data-theme=\"light\"],\n .ace_editor, .ace_editor *,\n .CodeMirror, .CodeMirror *,\n .monaco-editor, .monaco-editor *,\n .markdown-body pre, .markdown-body pre *,\n .highlight, .highlight *,\n pre code, pre code * {\n filter: none !important;\n }\n \n /* Fix common UI elements */\n .modal, .popup, .dropdown-menu, .tooltip, .popover {\n filter: invert(1) hue-rotate(180deg) !important;\n background: #2d2d44 !important;\n border-color: #444 !important;\n }\n \n /* Scrollbars */\n ::-webkit-scrollbar { background: #1a1a2e !important; }\n ::-webkit-scrollbar-thumb { background: #444 !important; }\n ::-webkit-scrollbar-thumb:hover { background: #555 !important; }\n \n /* Selection */\n ::selection { background: #4ecdc4 !important; color: #1a1a2e !important; }\n ::-moz-selection { background: #4ecdc4 !important; color: #1a1a2e !important; }\n ';\n }\n \n function removeDarkMode() {\n var style = document.getElementById('universal-dark-mode-style');\n if (style) style.remove();\n }\n \n // Toggle with Alt+Shift+D\n document.addEventListener('keydown', function(e) {\n if (e.altKey && e.shiftKey && e.key === 'D') {\n e.preventDefault();\n enabled = !enabled;\n if (enabled) {\n applyDarkMode();\n console.log('[Universal Dark Mode] Enabled');\n } else {\n removeDarkMode();\n console.log('[Universal Dark Mode] Disabled');\n }\n }\n });\n \n // Apply on load\n applyDarkMode();\n \n // Re-apply on dynamic content\n var observer = new MutationObserver(function(mutations) {\n if (enabled && !document.getElementById('universal-dark-mode-style')) {\n applyDarkMode();\n }\n });\n observer.observe(document.head, { childList: true });\n \n console.log('[Universal Dark Mode] Loaded - Press Alt+Shift+D to toggle');\n})();", "Universal Dark Mode"); } } catch(__e) { console.warn('[Userscript:Universal Dark Mode]', __e); } })(); })();
Skip to content

Commit 32bb197

Browse files
codebytereaduh95
authored andcommitted
src: use simdutf for two-byte strings in UTF-8 writes
StringBytes::Write() already used simdutf to encode one-byte strings as UTF-8 but sent every two-byte (UTF-16) string through v8::String::WriteUtf8V2(), which is several times slower. That path is behind Buffer.from(string), buf.write(), fs.write*() with string data and every string written to a libuv stream, and JSON.stringify() output is a two-byte string as soon as any value in the payload is outside Latin-1. Encode two-byte strings with simdutf as well whenever their UTF-8 form is guaranteed to fit in the target: well-formed input is converted directly, and input with unpaired surrogates is converted from a copy passed through simdutf::to_well_formed_utf16(), which replaces each unpaired surrogate with U+FFFD exactly like kReplaceInvalidUtf8 (this mirrors what TextEncoder already does). Writes that have to truncate at a character boundary keep using WriteUtf8V2(), so their output is byte-for-byte unchanged, and so do strings of up to 32 code units, for which V8 is already as fast (the same threshold TextEncoder uses). buf.write() of a 2 KiB two-byte string improves ~5x (astral-heavy and lone-surrogate strings ~3.5x and ~5x), Buffer.from() of a 64 KiB JSON string ~2.7x; one-byte strings are unaffected. Signed-off-by: Shelley Vohr <shelley.vohr@gmail.com> PR-URL: #65324 Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com> Reviewed-By: Gürgün Dayıoğlu <hey@gurgun.day> Reviewed-By: Daniel Lemire <daniel@lemire.me> Reviewed-By: James M Snell <jasnell@gmail.com>
1 parent f33dba7 commit 32bb197

3 files changed

Lines changed: 193 additions & 1 deletion

File tree

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
'use strict';
2+
3+
// buf.write(string, 'utf8') for strings whose in-memory representation is
4+
// one-byte (Latin-1) or two-byte (UTF-16), which take different encoder paths.
5+
constcommon=require('../common.js');
6+
constbench=common.createBenchmark(main,{
7+
chars: ['one-byte','two-byte','two-byte-astral','two-byte-lone-surrogate'],
8+
len: [16,256,2048,65536],
9+
n: [5e5],
10+
});
11+
12+
functionmakeString(chars,len){
13+
switch(chars){
14+
case'one-byte':
15+
return'aé'.repeat(len/2);
16+
case'two-byte':
17+
return'aé€日'.repeat(len/4);
18+
case'two-byte-astral':
19+
return'aé€日\u{1F600}'.repeat(len/6).padEnd(len,'a');
20+
case'two-byte-lone-surrogate':
21+
return'aé€日'.repeat(len/4-1)+'ab\ud800c';
22+
default:
23+
thrownewError(chars);
24+
}
25+
}
26+
27+
functionmain({ chars, len, n }){
28+
conststring=makeString(chars,len);
29+
constbuf=Buffer.allocUnsafe(Buffer.byteLength(string));
30+
if(len>=65536)n=Math.floor(n/32);
31+
bench.start();
32+
for(leti=0;i<n;++i){
33+
buf.write(string,0,'utf8');
34+
}
35+
bench.end(n);
36+
}

‎src/string_bytes.cc‎

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -310,9 +310,37 @@ size_t StringBytes::Write(Isolate* isolate,
310310
input_view.length(),
311311
buf,
312312
buflen);
313-
} else {
313+
} elseif (input_view.length() <= 32) {
314+
// V8 is as fast for tiny strings (same threshold TextEncoder uses).
314315
nbytes = str->WriteUtf8V2(
315316
isolate, buf, buflen, String::WriteFlags::kReplaceInvalidUtf8);
317+
} else {
318+
// Use simdutf for two-byte strings as well whenever the UTF-8 form
319+
// is guaranteed to fit; truncating writes (which must stop at a
320+
// character boundary) keep going through V8 so that their output
321+
// stays byte-for-byte identical.
322+
constchar16_t* data =
323+
reinterpret_cast<constchar16_t*>(input_view.data16());
324+
constsize_t length = input_view.length();
325+
MaybeStackBuffer<char16_t, 1024> well_formed;
326+
if (!simdutf::validate_utf16(data, length)) {
327+
// Unpaired surrogates: encode a copy in which each of them has been
328+
// replaced with U+FFFD, which is what kReplaceInvalidUtf8 produces.
329+
well_formed.AllocateSufficientStorage(length);
330+
simdutf::to_well_formed_utf16(data, length, well_formed.out());
331+
data = well_formed.out();
332+
}
333+
// A UTF-16 code unit never expands to more than 3 UTF-8 bytes, so
334+
// 3 * length is what StorageSize() hands most callers; only compute
335+
// the exact length when the buffer is smaller than that.
336+
if (buflen >= 3 * length ||
337+
buflen >= simdutf::utf8_length_from_utf16(data, length)) {
338+
nbytes = simdutf::convert_utf16_to_utf8(data, length, buf);
339+
} else {
340+
// Does not fit: let V8 truncate at a character boundary.
341+
nbytes = str->WriteUtf8V2(
342+
isolate, buf, buflen, String::WriteFlags::kReplaceInvalidUtf8);
343+
}
316344
}
317345
break;
318346

Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
'use strict';
2+
// UTF-8 encoding of two-byte (UTF-16) JS strings through the Buffer write
3+
// paths (Buffer.from, Buffer#write, Buffer.byteLength) must:
4+
// - produce standard UTF-8 for well-formed input of any size,
5+
// - replace lone surrogates with U+FFFD (EF BF BD),
6+
// - never write a partial character when the target is too small,
7+
// independent of which internal fast path handles the string.
8+
require('../common');
9+
constassert=require('assert');
10+
11+
// Reference encoder written out longhand so the test does not depend on the
12+
// implementation under test (TextEncoder shares code with it).
13+
functionutf8Reference(str){
14+
constout=[];
15+
for(leti=0;i<str.length;i++){
16+
letcp=str.charCodeAt(i);
17+
if(cp>=0xd800&&cp<=0xdbff){
18+
constnext=i+1<str.length ? str.charCodeAt(i+1) : 0;
19+
if(next>=0xdc00&&next<=0xdfff){
20+
cp=0x10000+((cp-0xd800)<<10)+(next-0xdc00);
21+
i++;
22+
}else{
23+
cp=0xfffd;
24+
}
25+
}elseif(cp>=0xdc00&&cp<=0xdfff){
26+
cp=0xfffd;
27+
}
28+
if(cp<0x80){
29+
out.push(cp);
30+
}elseif(cp<0x800){
31+
out.push(0xc0|(cp>>6),0x80|(cp&0x3f));
32+
}elseif(cp<0x10000){
33+
out.push(0xe0|(cp>>12),0x80|((cp>>6)&0x3f),0x80|(cp&0x3f));
34+
}else{
35+
out.push(0xf0|(cp>>18),0x80|((cp>>12)&0x3f),
36+
0x80|((cp>>6)&0x3f),0x80|(cp&0x3f));
37+
}
38+
}
39+
returnBuffer.from(out);
40+
}
41+
42+
functioncheckFull(str,label){
43+
constexpected=utf8Reference(str);
44+
assert.deepStrictEqual(Buffer.from(str,'utf8'),expected,`${label}: Buffer.from`);
45+
assert.strictEqual(Buffer.byteLength(str,'utf8'),expected.length,`${label}: byteLength`);
46+
// Exact-size target.
47+
constexact=Buffer.alloc(expected.length);
48+
assert.strictEqual(exact.write(str,'utf8'),expected.length,`${label}: write exact`);
49+
assert.deepStrictEqual(exact,expected,`${label}: write exact bytes`);
50+
// Oversized target (3 bytes per code unit is what most internal callers allocate).
51+
constbig=Buffer.alloc(str.length*3+7,0xaa);
52+
assert.strictEqual(big.write(str,2,'utf8'),expected.length,`${label}: write big`);
53+
assert.deepStrictEqual(big.subarray(2,2+expected.length),expected,`${label}: write big bytes`);
54+
assert.strictEqual(big[0],0xaa);
55+
assert.strictEqual(big[2+expected.length],0xaa,`${label}: no overrun`);
56+
}
57+
58+
// Truncating writes must stop before the first character that does not fit.
59+
functioncheckTruncation(str,label){
60+
constexpected=utf8Reference(str);
61+
for(letsize=0;size<=Math.min(expected.length,70);size++){
62+
consttarget=Buffer.alloc(size+1,0xaa);
63+
constn=target.write(str,0,size,'utf8');
64+
assert.ok(n<=size,`${label}: size=${size} wrote ${n}`);
65+
assert.deepStrictEqual(target.subarray(0,n),expected.subarray(0,n),`${label}: prefix size=${size}`);
66+
assert.strictEqual(target[size],0xaa,`${label}: overrun size=${size}`);
67+
// What was written must be a whole number of characters: the next byte in
68+
// the reference (if any) has to be a lead byte, not a continuation byte.
69+
if(n<expected.length){
70+
assert.notStrictEqual(expected[n]&0xc0,0x80,`${label}: split char at size=${size}`);
71+
// And it stopped only because the next character really did not fit.
72+
letnext=n+1;
73+
while(next<expected.length&&(expected[next]&0xc0)===0x80)next++;
74+
assert.ok(next>size,`${label}: stopped early at size=${size} (n=${n}, next=${next})`);
75+
}
76+
}
77+
}
78+
79+
// Force a two-byte representation even for ASCII/Latin-1 content by building
80+
// the string from a two-byte seed and slicing (V8 keeps the representation).
81+
functiontwoByte(str){
82+
consts=('\u{1F600}'+str).slice(2);
83+
assert.strictEqual(s,str);
84+
returns;
85+
}
86+
87+
constsamples={
88+
ascii: 'The quick brown fox jumps over the lazy dog 0123456789',
89+
latin1: 'français élan über naïve façade ÿ',
90+
bmp: '日本語テキストとハングル한국어',
91+
astral: 'emoji \u{1F600}\u{1F4A9} math \u{1D49C} han \u{20BB7}',
92+
mixed: 'a é 日 \u{1F600} b ü 本 \u{1F4A9}',
93+
};
94+
95+
for(const[name,base]ofObject.entries(samples)){
96+
for(constrepeatof[1,3,40,700,12000]){
97+
conststr=twoByte(base.repeat(repeat));
98+
checkFull(str,`${name} x${repeat}`);
99+
}
100+
checkTruncation(twoByte(base.repeat(3)),`${name} truncation`);
101+
}
102+
103+
// Lone surrogates in various positions and sizes -> U+FFFD, rest intact.
104+
consthigh='\ud83d';
105+
constlow='\ude00';
106+
constsurrogateCases={
107+
'lone high': `ab${high}cd`,
108+
'lone low': `ab${low}cd`,
109+
'reversed pair': `ab${low}${high}cd`,
110+
'high at end': `abcd${high}`,
111+
'low at start': `${low}abcd`,
112+
'high high low': `${high}${high}${low}x`,
113+
'pair then lone': `${high}${low}${high}`,
114+
'only lone': high,
115+
};
116+
for(const[name,base]ofObject.entries(surrogateCases)){
117+
for(constpadof['','x'.repeat(50),'é'.repeat(300),'日'.repeat(30000)]){
118+
conststr=pad+base+pad;
119+
checkFull(str,`${name} pad=${pad.length}`);
120+
}
121+
checkTruncation(base+'zz',`${name} truncation`);
122+
}
123+
124+
// Buffer.from of a large two-byte string equals TextEncoder output.
125+
{
126+
conststr=twoByte(samples.mixed.repeat(50000));
127+
assert.deepStrictEqual(Buffer.from(str),Buffer.from(newTextEncoder().encode(str)));
128+
}

0 commit comments

Comments
 (0)