@@ -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+
209220template <typename Char>
210221Utf8::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
0 commit comments