Skip to content

Commit

Permalink
src: improve utf8 string generation performance
Browse files Browse the repository at this point in the history
  • Loading branch information
anonrig committed Sep 10, 2024
1 parent 26b03c1 commit 34b530b
Showing 1 changed file with 20 additions and 3 deletions.
23 changes: 20 additions & 3 deletions src/util.cc
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@
#include <sys/types.h>
#endif

#include <simdutf.h>

#include <atomic>
#include <cstdio>
#include <cstring>
Expand Down Expand Up @@ -100,11 +102,26 @@ static void MakeUtf8String(Isolate* isolate,
MaybeStackBuffer<T>* target) {
Local<String> string;
if (!value->ToString(isolate->GetCurrentContext()).ToLocal(&string)) return;
String::ValueView value_view(isolate, string);

if (value_view.is_one_byte()) {
target->AllocateSufficientStorage(value_view.length() + 1);
target->SetLengthAndZeroTerminate(value_view.length());
memcpy(target->out(),
reinterpret_cast<const char*>(value_view.data8()),
value_view.length());
return;
}

size_t storage;
if (!StringBytes::StorageSize(isolate, string, UTF8).To(&storage)) return;
storage += 1;
// Add +1 for null termination.
auto storage = simdutf::utf8_length_from_utf16(
reinterpret_cast<const char16_t*>(value_view.data16()),
value_view.length()) +
1;
target->AllocateSufficientStorage(storage);

// TODO(@anonrig): Use simdutf to speed up non-one-byte strings once it's
// implemented
const int flags =
String::NO_NULL_TERMINATION | String::REPLACE_INVALID_UTF8;
const int length =
Expand Down

0 comments on commit 34b530b

Please sign in to comment.