Skip to content

Commit 9f9733a

Browse files
committed
fixup! util: improve textencoder encodeInto performance
1 parent f993411 commit 9f9733a

File tree

1 file changed

+16
-8
lines changed

1 file changed

+16
-8
lines changed

src/encoding_binding.cc

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -203,15 +203,23 @@ void BindingData::EncodeInto(const FunctionCallbackInfo<Value>& args) {
203203
if (view.is_one_byte()) {
204204
auto data = reinterpret_cast<const char*>(view.data8());
205205
simdutf::result result = simdutf::validate_ascii_with_errors(data, length);
206-
// Only copy what fits in the destination
207-
written = read = std::min(result.count, dest_length);
208-
if (read > 0) {
209-
memcpy(write_result, data, read);
210-
write_result += read;
211-
data += read;
212-
length -= read;
213-
dest_length -= read;
206+
written = read = result.count;
207+
208+
// Check if ASCII portion exceeds destination size
209+
if (result.count > dest_length) {
210+
written = read = dest_length;
211+
memcpy(write_result, data, dest_length);
212+
binding_data->encode_into_results_buffer_[0] = static_cast<double>(read);
213+
binding_data->encode_into_results_buffer_[1] =
214+
static_cast<double>(written);
215+
return;
214216
}
217+
218+
memcpy(write_result, data, read);
219+
write_result += read;
220+
data += read;
221+
length -= read;
222+
dest_length -= read;
215223
if (length != 0 && dest_length != 0) {
216224
size_t rest = findBestFit(data, length, dest_length);
217225
if (rest != 0) {

0 commit comments

Comments
 (0)