Skip to content

Commit 34b530b

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

File tree

1 file changed

+20
-3
lines changed

1 file changed

+20
-3
lines changed

src/util.cc

+20-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,26 @@ 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);
106+
107+
if (value_view.is_one_byte()) {
108+
target->AllocateSufficientStorage(value_view.length() + 1);
109+
target->SetLengthAndZeroTerminate(value_view.length());
110+
memcpy(target->out(),
111+
reinterpret_cast<const char*>(value_view.data8()),
112+
value_view.length());
113+
return;
114+
}
103115

104-
size_t storage;
105-
if (!StringBytes::StorageSize(isolate, string, UTF8).To(&storage)) return;
106-
storage += 1;
116+
// Add +1 for null termination.
117+
auto storage = simdutf::utf8_length_from_utf16(
118+
reinterpret_cast<const char16_t*>(value_view.data16()),
119+
value_view.length()) +
120+
1;
107121
target->AllocateSufficientStorage(storage);
122+
123+
// TODO(@anonrig): Use simdutf to speed up non-one-byte strings once it's
124+
// implemented
108125
const int flags =
109126
String::NO_NULL_TERMINATION | String::REPLACE_INVALID_UTF8;
110127
const int length =

0 commit comments

Comments
 (0)