Skip to content

Commit def9c45

Browse files
committed
src: improve utf8 string generation performance
1 parent 26b03c1 commit def9c45

File tree

1 file changed

+14
-3
lines changed

1 file changed

+14
-3
lines changed

src/util.cc

+14-3
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@
4848
#include <sys/types.h>
4949
#endif
5050

51+
#include <simdutf.h>
52+
5153
#include <atomic>
5254
#include <cstdio>
5355
#include <cstring>
@@ -100,11 +102,20 @@ static void MakeUtf8String(Isolate* isolate,
100102
MaybeStackBuffer<T>* target) {
101103
Local<String> string;
102104
if (!value->ToString(isolate->GetCurrentContext()).ToLocal(&string)) return;
105+
String::ValueView value_view(isolate, string);
103106

104-
size_t storage;
105-
if (!StringBytes::StorageSize(isolate, string, UTF8).To(&storage)) return;
106-
storage += 1;
107+
if (value_view.is_one_byte()) {
108+
target->AllocateSufficientStorage(value_view.length() + 1);
109+
target->SetLengthAndZeroTerminate(value_view.length());
110+
memcpy(target->out(), reinterpret_cast<const char*>(value_view.data8()), value_view.length());
111+
return;
112+
}
113+
114+
// Add +1 for null termination.
115+
auto storage = simdutf::utf8_length_from_utf16(reinterpret_cast<const char16_t*>(value_view.data16()), value_view.length()) + 1;
107116
target->AllocateSufficientStorage(storage);
117+
118+
// TODO(@anonrig): Use simdutf to speed up non-one-byte strings once it's implemented
108119
const int flags =
109120
String::NO_NULL_TERMINATION | String::REPLACE_INVALID_UTF8;
110121
const int length =

0 commit comments

Comments
 (0)