From 59888c0f8596ee0418b1080481a46566eb585a37 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ren=C3=A9?= Date: Wed, 18 Nov 2026 22:27:35 +0000 Subject: [PATCH 1/5] tools: fix incorrect environment variable in coverage-windows MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: René PR-URL: https://github.com/nodejs/node/pull/67591 Refs: https://github.com/nodejs/node/issues/67589 Reviewed-By: Mike McCready <66998419+MikeMcC399@users.noreply.github.com> Reviewed-By: Antoine du Hamel --- .github/workflows/coverage-windows.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/coverage-windows.yml b/.github/workflows/coverage-windows.yml index 749884c61382..f42a96e96e7c 100644 --- a/.github/workflows/coverage-windows.yml +++ b/.github/workflows/coverage-windows.yml @@ -84,7 +84,7 @@ jobs: run: choco install nasm - name: Install Rust ${{ env.RUSTC_VERSION }} run: | - rustup override set "$RUSTC_VERSION" + rustup override set "$Env:RUSTC_VERSION" rustup --version - name: Build run: ./vcbuild.bat clang-cl v8temporal From c48cde05d4bafe436ecaae192331d8e36f5bf5ee Mon Sep 17 00:00:00 2001 From: marcpems Date: Fri, 27 Nov 2026 18:30:46 +0000 Subject: [PATCH 2/5] deps: backport V8 two-byte JSON scanning to v26.x Backport the JSON portions of V8 d4966a986bbe, including the parser prerequisite 2990d03d9851, unaligned reads from fc3551409efe and capped Highway tags from 7e56c4c6066f. Omit the general mixed-width string comparison change. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- common.gypi | 2 +- deps/v8/src/json/json-parser.cc | 68 ++++++- deps/v8/src/json/json-stringifier.cc | 224 ++++++++++----------- deps/v8/test/mjsunit/json-two-byte-simd.js | 17 ++ 4 files changed, 181 insertions(+), 130 deletions(-) create mode 100644 deps/v8/test/mjsunit/json-two-byte-simd.js diff --git a/common.gypi b/common.gypi index 71840b13acd3..8b9baddd073f 100644 --- a/common.gypi +++ b/common.gypi @@ -50,7 +50,7 @@ # Reset this number to 0 on major V8 upgrades. # Increment by one for each non-official patch applied to deps/v8. - 'v8_embedder_string': '-node.58', + 'v8_embedder_string': '-node.59', ##### V8 defaults for Node.js ##### diff --git a/deps/v8/src/json/json-parser.cc b/deps/v8/src/json/json-parser.cc index 30710e97728f..ea13f3d309fa 100644 --- a/deps/v8/src/json/json-parser.cc +++ b/deps/v8/src/json/json-parser.cc @@ -6,6 +6,7 @@ #include +#include "hwy/highway.h" #include "src/base/small-vector.h" #include "src/base/strings.h" #include "src/builtins/builtins.h" @@ -2514,20 +2515,75 @@ void JsonParser::DecodeString(SinkChar* sink, uint32_t start, template JsonString JsonParser::ScanJsonString(bool needs_internalization) { + namespace hw = hwy::HWY_NAMESPACE; + DisallowGarbageCollection no_gc; uint32_t start = position(); uint32_t offset = start; bool has_escape = false; base::uc32 bits = 0; + // SIMD constants for string scanning, hoisted out of the loop. + hw::CappedTag tag; + const size_t stride = hw::Lanes(tag); + const auto mask_0x20 = hw::Set(tag, 0x20); + const auto mask_quote = hw::Set(tag, '"'); + const auto mask_backslash = hw::Set(tag, '\\'); + [[maybe_unused]] const auto mask_0xff = + hw::Set(tag, unibrow::Latin1::kMaxChar); + while (true) { - cursor_ = std::find_if(cursor_, end_, [&bits](Char c) { - if (sizeof(Char) == 2 && V8_UNLIKELY(c > unibrow::Latin1::kMaxChar)) { - bits |= c; - return false; + if constexpr (sizeof(Char) == 2) { + // While all characters seen so far are Latin1 (<= 0xff), also check for + // characters > 0xff so `bits` tracks whether the string can be one-byte + // converted. + if (V8_LIKELY(bits <= unibrow::Latin1::kMaxChar)) { + for (; cursor_ + (stride - 1) < end_; cursor_ += stride) { + const auto input = hw::LoadU(tag, cursor_); + // TODO(floitsch): use operators for the comparisons when they are + // available on RISC-V. + const auto result = hw::Or( + hw::Or(hw::Lt(input, mask_0x20), hw::Eq(input, mask_quote)), + hw::Or(hw::Eq(input, mask_backslash), hw::Gt(input, mask_0xff))); + if (V8_LIKELY(hw::AllFalse(tag, result))) continue; + cursor_ += hw::FindKnownFirstTrue(tag, result); + break; + } + if (cursor_ < end_ && + V8_UNLIKELY(*cursor_ > unibrow::Latin1::kMaxChar)) { + bits |= *cursor_; + ++cursor_; + } + } + } + // For one-byte strings, or two-byte strings already known to contain a + // non-Latin1 character (bits > 0xff), scan for '"', '\', or control chars. + if (sizeof(Char) == 1 || V8_UNLIKELY(bits > unibrow::Latin1::kMaxChar)) { + for (; cursor_ + (stride - 1) < end_; cursor_ += stride) { + const auto input = hw::LoadU(tag, cursor_); + // TODO(floitsch): use operators for the comparisons when they are + // available on RISC-V. + const auto result = + hw::Or(hw::Or(hw::Lt(input, mask_0x20), hw::Eq(input, mask_quote)), + hw::Eq(input, mask_backslash)); + if (V8_LIKELY(hw::AllFalse(tag, result))) continue; + cursor_ += hw::FindKnownFirstTrue(tag, result); + break; } - return MayTerminateJsonString(character_json_scan_flags[c]); - }); + } + // Scalar fallback for remaining tail characters after the last full SIMD + // block. When the SIMD loop found a match, *cursor_ is already a special + // character and this immediately breaks. + for (; cursor_ < end_; ++cursor_) { + Char c = *cursor_; + if constexpr (sizeof(Char) == 2) { + if (V8_UNLIKELY(c > unibrow::Latin1::kMaxChar)) { + bits |= c; + continue; + } + } + if (MayTerminateJsonString(character_json_scan_flags[c])) break; + } if (V8_UNLIKELY(is_at_end())) { AllowGarbageCollection allow_before_exception; diff --git a/deps/v8/src/json/json-stringifier.cc b/deps/v8/src/json/json-stringifier.cc index 431016bb1779..c125cf7741ab 100644 --- a/deps/v8/src/json/json-stringifier.cc +++ b/deps/v8/src/json/json-stringifier.cc @@ -8,6 +8,7 @@ #include "absl/functional/overload.h" #include "hwy/highway.h" +#include "src/base/memory.h" #include "src/base/strings.h" #include "src/common/assert-scope.h" #include "src/common/globals.h" @@ -519,22 +520,34 @@ constexpr bool DoNotEscape(uint16_t c) { (c >= 0x23 && c != 0x5C && (c < 0xD800 || c > 0xDFFF)); } -// Checks if characters need escaping in a packed input (4 bytes in uint32_t). -constexpr bool NeedsEscape(uint32_t input) { - constexpr uint32_t mask_0x20 = 0x20202020u; - constexpr uint32_t mask_0x22 = 0x22222222u; - constexpr uint32_t mask_0x5c = 0x5C5C5C5Cu; - constexpr uint32_t mask_0x01 = 0x01010101u; - constexpr uint32_t mask_msb = 0x80808080u; +// Checks if characters need escaping in a packed input. +template +constexpr bool NeedsEscape(PackedT input) { + static_assert(sizeof(PackedT) == sizeof(SrcChar) * 4); + constexpr PackedT mask_0x01 = + std::numeric_limits::max() / std::numeric_limits::max(); + constexpr PackedT mask_0x20 = mask_0x01 * 0x20; + constexpr PackedT mask_0x22 = mask_0x01 * 0x22; + constexpr PackedT mask_0x5c = mask_0x01 * 0x5c; + constexpr PackedT mask_msb = mask_0x01 + << (sizeof(SrcChar) * kBitsPerByte - 1); // Escape control characters (< 0x20). - const uint32_t has_lt_0x20 = input - mask_0x20; + const PackedT has_lt_0x20 = input - mask_0x20; // Escape double quotation mark (0x22). - const uint32_t has_0x22 = (input ^ mask_0x22) - mask_0x01; + const PackedT has_0x22 = (input ^ mask_0x22) - mask_0x01; // Escape backslash (0x5C). - const uint32_t has_0x5c = (input ^ mask_0x5c) - mask_0x01; - // Chars >= 0x7F don't need escaping. - const uint32_t result_mask = ~input & mask_msb; - const uint32_t result = ((has_lt_0x20 | has_0x22 | has_0x5c) & result_mask); + const PackedT has_0x5c = (input ^ mask_0x5c) - mask_0x01; + // Chars with MSB set don't match ASCII escape characters. + const PackedT result_mask = ~input & mask_msb; + PackedT result = (has_lt_0x20 | has_0x22 | has_0x5c) & result_mask; + if constexpr (sizeof(SrcChar) == sizeof(base::uc16)) { + // Escape UTF-16 surrogates (0xD800 <= c <= 0xDFFF). + constexpr PackedT mask_0xd800 = mask_0x01 * 0xd800; + constexpr PackedT mask_0xf800 = mask_0x01 * 0xf800; + const PackedT has_surrogate = + (((input & mask_0xf800) ^ mask_0xd800) - mask_0x01) & input & mask_msb; + result |= has_surrogate; + } return result != 0; } // LINT.ThenChange(/src/objects/string.cc:StringDoesNotContainEscapeCharacters) @@ -548,8 +561,9 @@ bool DoNotEscape(const SrcChar* chars, size_t length, static constexpr size_t stride = sizeof(PackedT); size_t i = 0; for (; i + (stride - 1) < length; i += stride) { - PackedT packed = *reinterpret_cast(chars + i); - if (V8_UNLIKELY(NeedsEscape(packed))) break; + PackedT packed = + base::ReadUnalignedValue(reinterpret_cast
(chars + i)); + if (V8_UNLIKELY(NeedsEscape(packed))) break; } for (; i < length; i++) { no_escape = no_escape && DoNotEscape(chars[i]); @@ -2295,7 +2309,6 @@ class FastJsonStringifier { } template - requires(sizeof(SrcChar) == sizeof(uint8_t)) V8_INLINE bool AppendString(const SrcChar* chars, size_t length, const DisallowGarbageCollection& no_gc); @@ -2304,27 +2317,23 @@ class FastJsonStringifier { const DisallowGarbageCollection& no_gc); template - requires(sizeof(SrcChar) == sizeof(uint8_t)) + V8_INLINE size_t AppendEscapedChar(const SrcChar* chars, size_t length, + size_t i, size_t uncopied_src_index); + + template bool AppendStringScalar(const SrcChar* chars, size_t length, size_t start, size_t uncopied_src_index, const DisallowGarbageCollection& no_gc); template - requires(sizeof(SrcChar) == sizeof(uint8_t)) V8_INLINE bool AppendStringSWAR(const SrcChar* chars, size_t length, size_t start, size_t uncopied_src_index, const DisallowGarbageCollection& no_gc); template - requires(sizeof(SrcChar) == sizeof(uint8_t)) V8_INLINE bool AppendStringSIMD(const SrcChar* chars, size_t length, const DisallowGarbageCollection& no_gc); - template - requires(sizeof(SrcChar) == sizeof(base::uc16)) - V8_INLINE bool AppendString(const SrcChar* chars, size_t length, - const DisallowGarbageCollection& no_gc); - using FastIterableState = DescriptorArray::FastIterableState; static constexpr uint32_t kGlobalInterruptBudget = 200000; static constexpr uint32_t kArrayInterruptLength = 4000; @@ -3329,11 +3338,10 @@ bool FastJsonStringifier::CheckCycle() { template template - requires(sizeof(SrcChar) == sizeof(uint8_t)) bool FastJsonStringifier::AppendString( const SrcChar* chars, size_t length, const DisallowGarbageCollection& no_gc) { - constexpr int kUseSimdLengthThreshold = 32; + constexpr int kUseSimdLengthThreshold = 32 / sizeof(SrcChar); if (length >= kUseSimdLengthThreshold) { return AppendStringSIMD(chars, length, no_gc); } @@ -3350,18 +3358,55 @@ void FastJsonStringifier::AppendStringNoEscapes( template template - requires(sizeof(SrcChar) == sizeof(uint8_t)) +size_t FastJsonStringifier::AppendEscapedChar(const SrcChar* chars, + size_t length, size_t i, + size_t uncopied_src_index) { + SrcChar c = chars[i]; + buffer_.Append(chars + uncopied_src_index, i - uncopied_src_index); + if constexpr (sizeof(SrcChar) == sizeof(base::uc16)) { + if (base::IsInRange(c, static_cast(0xD800), + static_cast(0xDFFF))) { + char double_to_radix_chars[kDoubleToRadixMaxChars]; + base::Vector double_to_radix_buffer = + base::ArrayVector(double_to_radix_chars); + if (c <= 0xDBFF && i + 1 < length) { + SrcChar next = chars[i + 1]; + if (base::IsInRange(next, static_cast(0xDC00), + static_cast(0xDFFF))) { + // The current and next characters form a surrogate pair. + AppendCharacterUnchecked(c); + AppendCharacterUnchecked(next); + return 2; + } + } + // Lone surrogate. + AppendCStringLiteralUnchecked("\\u"); + std::string_view hex = + DoubleToRadixStringView(c, 16, double_to_radix_buffer); + AppendStringUnchecked(hex); + return 1; + } + } + SBXCHECK_LT(c, 0x60); + AppendCStringUnchecked(&JsonEscapeTable[c * kJsonEscapeTableEntrySize]); + return 1; +} + +template +template bool FastJsonStringifier::AppendStringScalar( const SrcChar* chars, size_t length, size_t start, size_t uncopied_src_index, const DisallowGarbageCollection& no_gc) { bool needs_escaping = false; - for (size_t i = start; i < length; i++) { - SrcChar c = chars[i]; - if (V8_LIKELY(DoNotEscape(c))) continue; + for (size_t i = start; i < length;) { + if (V8_LIKELY(DoNotEscape(chars[i]))) { + i++; + continue; + } needs_escaping = true; - buffer_.Append(chars + uncopied_src_index, i - uncopied_src_index); - AppendCStringUnchecked(&JsonEscapeTable[c * kJsonEscapeTableEntrySize]); - uncopied_src_index = i + 1; + size_t advance = AppendEscapedChar(chars, length, i, uncopied_src_index); + i += advance; + uncopied_src_index = i; } if (V8_LIKELY(uncopied_src_index < length)) { buffer_.Append(chars + uncopied_src_index, length - uncopied_src_index); @@ -3371,24 +3416,23 @@ bool FastJsonStringifier::AppendStringScalar( template template - requires(sizeof(SrcChar) == sizeof(uint8_t)) -V8_CLANG_NO_SANITIZE("alignment") bool FastJsonStringifier::AppendStringSWAR( const SrcChar* chars, size_t length, size_t start, size_t uncopied_src_index, const DisallowGarbageCollection& no_gc) { - using PackedT = uint32_t; - static constexpr size_t stride = sizeof(PackedT); + using PackedT = std::conditional_t; + static constexpr size_t stride = sizeof(PackedT) / sizeof(SrcChar); size_t i = start; for (; i + (stride - 1) < length; i += stride) { - PackedT packed = *reinterpret_cast(chars + i); - if (V8_UNLIKELY(NeedsEscape(packed))) break; + PackedT packed = + base::ReadUnalignedValue(reinterpret_cast
(chars + i)); + if (V8_UNLIKELY(NeedsEscape(packed))) break; } return AppendStringScalar(chars, length, i, uncopied_src_index, no_gc); } template template - requires(sizeof(SrcChar) == sizeof(uint8_t)) bool FastJsonStringifier::AppendStringSIMD( const SrcChar* chars, size_t length, const DisallowGarbageCollection& no_gc) { @@ -3398,37 +3442,43 @@ bool FastJsonStringifier::AppendStringSIMD( size_t uncopied_src_index = 0; // Index of first char not copied yet. const SrcChar* block = chars; const SrcChar* end = chars + length; - hw::FixedTag tag; + hw::CappedTag tag; static const size_t stride = hw::Lanes(tag); const auto mask_0x20 = hw::Set(tag, 0x20); const auto mask_0x22 = hw::Set(tag, 0x22); const auto mask_0x5c = hw::Set(tag, 0x5c); + [[maybe_unused]] const auto mask_0xf800 = + hw::Set(tag, static_cast(0xf800)); + [[maybe_unused]] const auto mask_0xd800 = + hw::Set(tag, static_cast(0xd800)); for (; block + (stride - 1) < end; block += stride) { const auto input = hw::LoadU(tag, block); // TODO(floitsch): use operators for the comparisons when they are available // on RISC-V. - const auto has_lower_than_0x20 = hw::Lt(input, mask_0x20); + const auto has_lt_0x20 = hw::Lt(input, mask_0x20); const auto has_0x22 = hw::Eq(input, mask_0x22); const auto has_0x5c = hw::Eq(input, mask_0x5c); - const auto result = hw::Or(hw::Or(has_lower_than_0x20, has_0x22), has_0x5c); + auto result = hw::Or(hw::Or(has_lt_0x20, has_0x22), has_0x5c); + if constexpr (sizeof(SrcChar) == sizeof(base::uc16)) { + // Checks input >= 0xd800 && input <= 0xdfff. + const auto has_surrogate = + hw::Eq(hw::And(input, mask_0xf800), mask_0xd800); + result = hw::Or(result, has_surrogate); + } // No character that needs escaping found in block. if (V8_LIKELY(hw::AllFalse(tag, result))) continue; needs_escaping = true; size_t index = hw::FindKnownFirstTrue(tag, result); - Char found_char = block[index]; const size_t char_index = block - chars + index; - const size_t copy_length = char_index - uncopied_src_index; - buffer_.Append(chars + uncopied_src_index, copy_length); - SBXCHECK_LT(found_char, 0x60); - AppendCStringUnchecked( - &JsonEscapeTable[found_char * kJsonEscapeTableEntrySize]); - uncopied_src_index = char_index + 1; + const size_t advance = + AppendEscapedChar(chars, length, char_index, uncopied_src_index); + uncopied_src_index = char_index + advance; // Advance to character after the one that was found to need escaping. - block += index + 1; + block += index + advance; // Subtract stride as it will be added again at the beginning of the loop. block -= stride; } @@ -3440,78 +3490,6 @@ bool FastJsonStringifier::AppendStringSIMD( needs_escaping; } -template -template - requires(sizeof(SrcChar) == sizeof(base::uc16)) -bool FastJsonStringifier::AppendString( - const SrcChar* chars, size_t length, - const DisallowGarbageCollection& no_gc) { - bool needs_escaping = false; - uint32_t uncopied_src_index = 0; // Index of first char not copied yet. - // TODO(pthier): Add SIMD version. - for (uint32_t i = 0; i < length; i++) { - SrcChar c = chars[i]; - if (V8_LIKELY(DoNotEscape(c))) continue; - needs_escaping = true; - if (sizeof(SrcChar) != 1 && base::IsInRange(c, static_cast(0xD800), - static_cast(0xDFFF))) { - // The current character is a surrogate. - buffer_.Append(chars + uncopied_src_index, i - uncopied_src_index); - char double_to_radix_chars[kDoubleToRadixMaxChars]; - base::Vector double_to_radix_buffer = - base::ArrayVector(double_to_radix_chars); - if (c <= 0xDBFF) { - // The current character is a leading surrogate. - if (i + 1 < length) { - // There is a next character. - SrcChar next = chars[i + 1]; - if (base::IsInRange(next, static_cast(0xDC00), - static_cast(0xDFFF))) { - // The next character is a trailing surrogate, meaning this is a - // surrogate pair. - AppendCharacterUnchecked(c); - AppendCharacterUnchecked(next); - i++; - } else { - // The next character is not a trailing surrogate. Thus, the - // current character is a lone leading surrogate. - AppendCStringLiteralUnchecked("\\u"); - std::string_view hex = - DoubleToRadixStringView(c, 16, double_to_radix_buffer); - AppendStringUnchecked(hex); - } - } else { - // There is no next character. Thus, the current character is a lone - // leading surrogate. - AppendCStringLiteralUnchecked("\\u"); - std::string_view hex = - DoubleToRadixStringView(c, 16, double_to_radix_buffer); - AppendStringUnchecked(hex); - } - } else { - // The current character is a lone trailing surrogate. (If it had been - // preceded by a leading surrogate, we would've ended up in the other - // branch earlier on, and the current character would've been handled - // as part of the surrogate pair already.) - AppendCStringLiteralUnchecked("\\u"); - std::string_view hex = - DoubleToRadixStringView(c, 16, double_to_radix_buffer); - AppendStringUnchecked(hex); - } - uncopied_src_index = i + 1; - } else { - buffer_.Append(chars + uncopied_src_index, i - uncopied_src_index); - DCHECK_LT(c, 0x60); - AppendCStringUnchecked(&JsonEscapeTable[c * kJsonEscapeTableEntrySize]); - uncopied_src_index = i + 1; - } - } - if (uncopied_src_index < length) { - buffer_.Append(chars + uncopied_src_index, length - uncopied_src_index); - } - return needs_escaping; -} - namespace { bool CanUseFastStringifier(DirectHandle replacer, diff --git a/deps/v8/test/mjsunit/json-two-byte-simd.js b/deps/v8/test/mjsunit/json-two-byte-simd.js new file mode 100644 index 000000000000..b9b4bb8b9ce1 --- /dev/null +++ b/deps/v8/test/mjsunit/json-two-byte-simd.js @@ -0,0 +1,17 @@ +// Copyright 2026 the V8 project authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +for (const n of [7, 8, 9, 15, 16, 17]) { + const prefix = 'a'.repeat(n) + '\u0100'.repeat(32); + for (const [value, encoded] of [ + ['"', '\\"'], ['\\', '\\\\'], ['\n', '\\n'], + ['\ud800', '\\ud800'], ['\udfff', '\\udfff'], ['\ud83d\ude00', '\ud83d\ude00'], + ]) { + const text = prefix + value + '\u0101'.repeat(5); + const json = '"' + prefix + encoded + '\u0101'.repeat(5) + '"'; + assertEquals(json, JSON.stringify(text)); + assertEquals(text, JSON.parse(json)); + } + assertThrows(() => JSON.parse('"' + prefix + '\n"'), SyntaxError); +} From cf0b3f58ce89c2aae4782c3939be963d60ebf4f2 Mon Sep 17 00:00:00 2001 From: marcpems Date: Fri, 27 Nov 2026 19:46:48 +0000 Subject: [PATCH 3/5] deps: avoid repeated SIMD scans on escape-heavy JSON strings Resume vector scanning after a clean run. Avoid empty copies and use the known encoded widths for JSON escape table entries. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- deps/v8/src/json/json-stringifier.cc | 38 +++++++++++++++++----- deps/v8/test/mjsunit/json-two-byte-simd.js | 9 +++++ 2 files changed, 39 insertions(+), 8 deletions(-) diff --git a/deps/v8/src/json/json-stringifier.cc b/deps/v8/src/json/json-stringifier.cc index c125cf7741ab..531bd52aba72 100644 --- a/deps/v8/src/json/json-stringifier.cc +++ b/deps/v8/src/json/json-stringifier.cc @@ -3362,7 +3362,9 @@ size_t FastJsonStringifier::AppendEscapedChar(const SrcChar* chars, size_t length, size_t i, size_t uncopied_src_index) { SrcChar c = chars[i]; - buffer_.Append(chars + uncopied_src_index, i - uncopied_src_index); + if (uncopied_src_index != i) { + buffer_.Append(chars + uncopied_src_index, i - uncopied_src_index); + } if constexpr (sizeof(SrcChar) == sizeof(base::uc16)) { if (base::IsInRange(c, static_cast(0xD800), static_cast(0xDFFF))) { @@ -3388,7 +3390,12 @@ size_t FastJsonStringifier::AppendEscapedChar(const SrcChar* chars, } } SBXCHECK_LT(c, 0x60); - AppendCStringUnchecked(&JsonEscapeTable[c * kJsonEscapeTableEntrySize]); + const char* escape = &JsonEscapeTable[c * kJsonEscapeTableEntrySize]; + if (escape[1] == 'u') { + AppendCStringUnchecked(escape, 6); + } else { + AppendCStringUnchecked(escape, 2); + } return 1; } @@ -3453,7 +3460,7 @@ bool FastJsonStringifier::AppendStringSIMD( [[maybe_unused]] const auto mask_0xd800 = hw::Set(tag, static_cast(0xd800)); - for (; block + (stride - 1) < end; block += stride) { + while (static_cast(end - block) >= stride) { const auto input = hw::LoadU(tag, block); // TODO(floitsch): use operators for the comparisons when they are available // on RISC-V. @@ -3469,18 +3476,33 @@ bool FastJsonStringifier::AppendStringSIMD( } // No character that needs escaping found in block. - if (V8_LIKELY(hw::AllFalse(tag, result))) continue; + if (V8_LIKELY(hw::AllFalse(tag, result))) { + block += stride; + continue; + } needs_escaping = true; size_t index = hw::FindKnownFirstTrue(tag, result); const size_t char_index = block - chars + index; + const size_t gap = char_index - uncopied_src_index; const size_t advance = AppendEscapedChar(chars, length, char_index, uncopied_src_index); uncopied_src_index = char_index + advance; - // Advance to character after the one that was found to need escaping. - block += index + advance; - // Subtract stride as it will be added again at the beginning of the loop. - block -= stride; + size_t i = uncopied_src_index; + if constexpr (sizeof(SrcChar) == sizeof(base::uc16)) { + // Avoid repeatedly scanning overlapping vectors in an escape-heavy run. + if (gap < stride) { + while (i < length && i - uncopied_src_index < stride) { + if (DoNotEscape(chars[i])) { + ++i; + } else { + i += AppendEscapedChar(chars, length, i, uncopied_src_index); + uncopied_src_index = i; + } + } + } + } + block = chars + i; } // Handle remaining characters. diff --git a/deps/v8/test/mjsunit/json-two-byte-simd.js b/deps/v8/test/mjsunit/json-two-byte-simd.js index b9b4bb8b9ce1..fddf0f4adcd4 100644 --- a/deps/v8/test/mjsunit/json-two-byte-simd.js +++ b/deps/v8/test/mjsunit/json-two-byte-simd.js @@ -15,3 +15,12 @@ for (const n of [7, 8, 9, 15, 16, 17]) { } assertThrows(() => JSON.parse('"' + prefix + '\n"'), SyntaxError); } + +// Exercise the transition from adjacent escapes back to an unescaped run. +for (let n = 0; n < 16; n++) { + const head = '\u0100'.repeat(32 + n); + const tail = '\u0101'.repeat(32); + const text = head + '\n"\\\u000b\ud83d\ude00\udfff' + tail; + assertEquals('"' + head + '\\n\\"\\\\\\u000b\ud83d\ude00\\udfff' + tail + '"', + JSON.stringify(text)); +} From 26ae61067b7eac9a81ed827db08c16f90266cde1 Mon Sep 17 00:00:00 2001 From: marcpems Date: Sat, 28 Nov 2026 05:43:22 +0000 Subject: [PATCH 4/5] deps: resume SIMD sooner when stringifying two-byte JSON Limit scalar recovery to gaps below half a vector so a quote every eight UTF-16 code units does not keep the stringifier in scalar mode. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- deps/v8/src/json/json-stringifier.cc | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/deps/v8/src/json/json-stringifier.cc b/deps/v8/src/json/json-stringifier.cc index 531bd52aba72..ecb1073a571f 100644 --- a/deps/v8/src/json/json-stringifier.cc +++ b/deps/v8/src/json/json-stringifier.cc @@ -3491,8 +3491,8 @@ bool FastJsonStringifier::AppendStringSIMD( size_t i = uncopied_src_index; if constexpr (sizeof(SrcChar) == sizeof(base::uc16)) { // Avoid repeatedly scanning overlapping vectors in an escape-heavy run. - if (gap < stride) { - while (i < length && i - uncopied_src_index < stride) { + if (gap < stride / 2) { + while (i < length && i - uncopied_src_index < stride / 2) { if (DoNotEscape(chars[i])) { ++i; } else { From b6255e84cec2dcb53709b68df46c789074b21054 Mon Sep 17 00:00:00 2001 From: Marcus Perryman Date: Sat, 28 Nov 2026 13:42:07 +0000 Subject: [PATCH 5/5] deps: limit two-byte JSON backport to stringification Keep the v26 parser unchanged rather than trade dense-escape parsing regressions for sparse-input throughput. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- deps/v8/src/json/json-parser.cc | 68 ++-------------------- deps/v8/test/mjsunit/json-two-byte-simd.js | 2 - 2 files changed, 6 insertions(+), 64 deletions(-) diff --git a/deps/v8/src/json/json-parser.cc b/deps/v8/src/json/json-parser.cc index ea13f3d309fa..30710e97728f 100644 --- a/deps/v8/src/json/json-parser.cc +++ b/deps/v8/src/json/json-parser.cc @@ -6,7 +6,6 @@ #include -#include "hwy/highway.h" #include "src/base/small-vector.h" #include "src/base/strings.h" #include "src/builtins/builtins.h" @@ -2515,75 +2514,20 @@ void JsonParser::DecodeString(SinkChar* sink, uint32_t start, template JsonString JsonParser::ScanJsonString(bool needs_internalization) { - namespace hw = hwy::HWY_NAMESPACE; - DisallowGarbageCollection no_gc; uint32_t start = position(); uint32_t offset = start; bool has_escape = false; base::uc32 bits = 0; - // SIMD constants for string scanning, hoisted out of the loop. - hw::CappedTag tag; - const size_t stride = hw::Lanes(tag); - const auto mask_0x20 = hw::Set(tag, 0x20); - const auto mask_quote = hw::Set(tag, '"'); - const auto mask_backslash = hw::Set(tag, '\\'); - [[maybe_unused]] const auto mask_0xff = - hw::Set(tag, unibrow::Latin1::kMaxChar); - while (true) { - if constexpr (sizeof(Char) == 2) { - // While all characters seen so far are Latin1 (<= 0xff), also check for - // characters > 0xff so `bits` tracks whether the string can be one-byte - // converted. - if (V8_LIKELY(bits <= unibrow::Latin1::kMaxChar)) { - for (; cursor_ + (stride - 1) < end_; cursor_ += stride) { - const auto input = hw::LoadU(tag, cursor_); - // TODO(floitsch): use operators for the comparisons when they are - // available on RISC-V. - const auto result = hw::Or( - hw::Or(hw::Lt(input, mask_0x20), hw::Eq(input, mask_quote)), - hw::Or(hw::Eq(input, mask_backslash), hw::Gt(input, mask_0xff))); - if (V8_LIKELY(hw::AllFalse(tag, result))) continue; - cursor_ += hw::FindKnownFirstTrue(tag, result); - break; - } - if (cursor_ < end_ && - V8_UNLIKELY(*cursor_ > unibrow::Latin1::kMaxChar)) { - bits |= *cursor_; - ++cursor_; - } - } - } - // For one-byte strings, or two-byte strings already known to contain a - // non-Latin1 character (bits > 0xff), scan for '"', '\', or control chars. - if (sizeof(Char) == 1 || V8_UNLIKELY(bits > unibrow::Latin1::kMaxChar)) { - for (; cursor_ + (stride - 1) < end_; cursor_ += stride) { - const auto input = hw::LoadU(tag, cursor_); - // TODO(floitsch): use operators for the comparisons when they are - // available on RISC-V. - const auto result = - hw::Or(hw::Or(hw::Lt(input, mask_0x20), hw::Eq(input, mask_quote)), - hw::Eq(input, mask_backslash)); - if (V8_LIKELY(hw::AllFalse(tag, result))) continue; - cursor_ += hw::FindKnownFirstTrue(tag, result); - break; - } - } - // Scalar fallback for remaining tail characters after the last full SIMD - // block. When the SIMD loop found a match, *cursor_ is already a special - // character and this immediately breaks. - for (; cursor_ < end_; ++cursor_) { - Char c = *cursor_; - if constexpr (sizeof(Char) == 2) { - if (V8_UNLIKELY(c > unibrow::Latin1::kMaxChar)) { - bits |= c; - continue; - } + cursor_ = std::find_if(cursor_, end_, [&bits](Char c) { + if (sizeof(Char) == 2 && V8_UNLIKELY(c > unibrow::Latin1::kMaxChar)) { + bits |= c; + return false; } - if (MayTerminateJsonString(character_json_scan_flags[c])) break; - } + return MayTerminateJsonString(character_json_scan_flags[c]); + }); if (V8_UNLIKELY(is_at_end())) { AllowGarbageCollection allow_before_exception; diff --git a/deps/v8/test/mjsunit/json-two-byte-simd.js b/deps/v8/test/mjsunit/json-two-byte-simd.js index fddf0f4adcd4..9b2462ae78e7 100644 --- a/deps/v8/test/mjsunit/json-two-byte-simd.js +++ b/deps/v8/test/mjsunit/json-two-byte-simd.js @@ -11,9 +11,7 @@ for (const n of [7, 8, 9, 15, 16, 17]) { const text = prefix + value + '\u0101'.repeat(5); const json = '"' + prefix + encoded + '\u0101'.repeat(5) + '"'; assertEquals(json, JSON.stringify(text)); - assertEquals(text, JSON.parse(json)); } - assertThrows(() => JSON.parse('"' + prefix + '\n"'), SyntaxError); } // Exercise the transition from adjacent escapes back to an unescaped run.