Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/coverage-windows.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion common.gypi
Original file line number Diff line number Diff line change
Expand Up @@ -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 #####

Expand Down
256 changes: 128 additions & 128 deletions deps/v8/src/json/json-stringifier.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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 <typename SrcChar, typename PackedT>
constexpr bool NeedsEscape(PackedT input) {
static_assert(sizeof(PackedT) == sizeof(SrcChar) * 4);
constexpr PackedT mask_0x01 =
std::numeric_limits<PackedT>::max() / std::numeric_limits<SrcChar>::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)
Expand All @@ -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<const PackedT*>(chars + i);
if (V8_UNLIKELY(NeedsEscape(packed))) break;
PackedT packed =
base::ReadUnalignedValue<PackedT>(reinterpret_cast<Address>(chars + i));
if (V8_UNLIKELY(NeedsEscape<SrcChar>(packed))) break;
}
for (; i < length; i++) {
no_escape = no_escape && DoNotEscape(chars[i]);
Expand Down Expand Up @@ -2295,7 +2309,6 @@ class FastJsonStringifier {
}

template <typename SrcChar>
requires(sizeof(SrcChar) == sizeof(uint8_t))
V8_INLINE bool AppendString(const SrcChar* chars, size_t length,
const DisallowGarbageCollection& no_gc);

Expand All @@ -2304,27 +2317,23 @@ class FastJsonStringifier {
const DisallowGarbageCollection& no_gc);

template <typename SrcChar>
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 <typename SrcChar>
bool AppendStringScalar(const SrcChar* chars, size_t length, size_t start,
size_t uncopied_src_index,
const DisallowGarbageCollection& no_gc);

template <typename SrcChar>
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 <typename SrcChar>
requires(sizeof(SrcChar) == sizeof(uint8_t))
V8_INLINE bool AppendStringSIMD(const SrcChar* chars, size_t length,
const DisallowGarbageCollection& no_gc);

template <typename SrcChar>
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;
Expand Down Expand Up @@ -3329,11 +3338,10 @@ bool FastJsonStringifier<Char>::CheckCycle() {

template <typename Char>
template <typename SrcChar>
requires(sizeof(SrcChar) == sizeof(uint8_t))
bool FastJsonStringifier<Char>::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);
}
Expand All @@ -3350,18 +3358,62 @@ void FastJsonStringifier<Char>::AppendStringNoEscapes(

template <typename Char>
template <typename SrcChar>
requires(sizeof(SrcChar) == sizeof(uint8_t))
size_t FastJsonStringifier<Char>::AppendEscapedChar(const SrcChar* chars,
size_t length, size_t i,
size_t uncopied_src_index) {
SrcChar c = chars[i];
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<SrcChar>(0xD800),
static_cast<SrcChar>(0xDFFF))) {
char double_to_radix_chars[kDoubleToRadixMaxChars];
base::Vector<char> 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<SrcChar>(0xDC00),
static_cast<SrcChar>(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);
const char* escape = &JsonEscapeTable[c * kJsonEscapeTableEntrySize];
if (escape[1] == 'u') {
AppendCStringUnchecked(escape, 6);
} else {
AppendCStringUnchecked(escape, 2);
}
return 1;
}

template <typename Char>
template <typename SrcChar>
bool FastJsonStringifier<Char>::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);
Expand All @@ -3371,24 +3423,23 @@ bool FastJsonStringifier<Char>::AppendStringScalar(

template <typename Char>
template <typename SrcChar>
requires(sizeof(SrcChar) == sizeof(uint8_t))
V8_CLANG_NO_SANITIZE("alignment")
bool FastJsonStringifier<Char>::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<sizeof(SrcChar) == sizeof(uint8_t),
uint32_t, uint64_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<const PackedT*>(chars + i);
if (V8_UNLIKELY(NeedsEscape(packed))) break;
PackedT packed =
base::ReadUnalignedValue<PackedT>(reinterpret_cast<Address>(chars + i));
if (V8_UNLIKELY(NeedsEscape<SrcChar>(packed))) break;
}
return AppendStringScalar(chars, length, i, uncopied_src_index, no_gc);
}

template <typename Char>
template <typename SrcChar>
requires(sizeof(SrcChar) == sizeof(uint8_t))
bool FastJsonStringifier<Char>::AppendStringSIMD(
const SrcChar* chars, size_t length,
const DisallowGarbageCollection& no_gc) {
Expand All @@ -3398,39 +3449,60 @@ bool FastJsonStringifier<Char>::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<SrcChar, 16> tag;
hw::CappedTag<SrcChar, 16 / sizeof(SrcChar)> 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<SrcChar>(0xf800));
[[maybe_unused]] const auto mask_0xd800 =
hw::Set(tag, static_cast<SrcChar>(0xd800));

for (; block + (stride - 1) < end; block += stride) {
while (static_cast<size_t>(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;
if (V8_LIKELY(hw::AllFalse(tag, result))) {
block += stride;
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;
// Advance to character after the one that was found to need escaping.
block += index + 1;
// Subtract stride as it will be added again at the beginning of the loop.
block -= stride;
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;
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 / 2) {
while (i < length && i - uncopied_src_index < stride / 2) {
if (DoNotEscape(chars[i])) {
++i;
} else {
i += AppendEscapedChar(chars, length, i, uncopied_src_index);
uncopied_src_index = i;
}
}
}
}
block = chars + i;
}

// Handle remaining characters.
Expand All @@ -3440,78 +3512,6 @@ bool FastJsonStringifier<Char>::AppendStringSIMD(
needs_escaping;
}

template <typename Char>
template <typename SrcChar>
requires(sizeof(SrcChar) == sizeof(base::uc16))
bool FastJsonStringifier<Char>::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<SrcChar>(0xD800),
static_cast<SrcChar>(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<char> 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<SrcChar>(0xDC00),
static_cast<SrcChar>(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<JSAny> replacer,
Expand Down
Loading