Skip to content

Commit 24079f4

Browse files
committed
deps: float v8 patch for WriteUtf8V2
Address performance regression in WriteUtf8V2 Refs: https://chromium-review.googlesource.com/c/v8/v8/+/7124103
1 parent 525c4fb commit 24079f4

File tree

2 files changed

+31
-1
lines changed

2 files changed

+31
-1
lines changed

deps/v8/src/strings/unicode-inl.h

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,17 @@ bool Utf8::IsValidCharacter(uchar c) {
206206
c != kBadChar);
207207
}
208208

209+
template <>
210+
bool Utf8::IsAsciiOneByteString<uint8_t>(const uint8_t* buffer, size_t size) {
211+
return simdutf::validate_ascii(reinterpret_cast<const char*>(buffer), size);
212+
}
213+
214+
template <>
215+
bool Utf8::IsAsciiOneByteString<uint16_t>(const uint16_t* buffer, size_t size) {
216+
// Necessary for template instantiation.
217+
UNREACHABLE();
218+
}
219+
209220
template <typename Char>
210221
Utf8::EncodingResult Utf8::Encode(v8::base::Vector<const Char> string,
211222
char* buffer, size_t capacity,
@@ -221,8 +232,17 @@ Utf8::EncodingResult Utf8::Encode(v8::base::Vector<const Char> string,
221232
const Char* characters = string.begin();
222233
size_t content_capacity = capacity - write_null;
223234
CHECK_LE(content_capacity, capacity);
224-
uint16_t last = Utf16::kNoPreviousCharacter;
225235
size_t read_index = 0;
236+
if (kSourceIsOneByte && string.size() > 0 &&
237+
Utf8::IsAsciiOneByteString(characters,
238+
std::min(string.size(), content_capacity))) {
239+
size_t to_write = std::min(string.size(), content_capacity);
240+
// Just memcpy when possible.
241+
memcpy(buffer, characters, to_write);
242+
read_index = to_write;
243+
write_index = to_write;
244+
}
245+
uint16_t last = Utf16::kNoPreviousCharacter;
226246
for (; read_index < string.size(); read_index++) {
227247
Char character = characters[read_index];
228248

deps/v8/src/strings/unicode.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,16 @@ class V8_EXPORT_PRIVATE Utf8 {
212212
// - valid code point range.
213213
static bool ValidateEncoding(const uint8_t* str, size_t length);
214214

215+
template <typename Char>
216+
static bool IsAsciiOneByteString(const Char* buffer, size_t size);
217+
218+
template <>
219+
inline bool IsAsciiOneByteString<uint8_t>(const uint8_t* buffer, size_t size);
220+
221+
template <>
222+
inline bool IsAsciiOneByteString<uint16_t>(const uint16_t* buffer,
223+
size_t size);
224+
215225
// Encode the given characters as Utf8 into the provided output buffer.
216226
struct EncodingResult {
217227
size_t bytes_written;

0 commit comments

Comments
 (0)