Skip to content

buffer: reduce overhead of StringBytes::Encode for UCS2

Rodrigo Muino Tomonari requested to merge github/fork/joyeecheung/ucs2-copy into master

Currently calling StringBytes::Encode on a UCS2 buffer results in two copies of the buffer and the usage of std::vector::assign makes the memory usage unpredictable, and therefore hard to test against.

This patch makes the memory usage more predictable by allocating the memory using node::UncheckedMalloc and handles the memory allocation failure properly. Only one copy of the buffer will be created and it will be freed upon GC of the string.

This was discovered in https://github.com/nodejs/node/pull/19739 when ensuring twice of the memory taken up by the buffer still does not prevent malloc failures.

Checklist
  • make -j4 test (UNIX), or vcbuild test (Windows) passes
  • tests and/or benchmarks are included
  • documentation is changed or added
  • commit message follows commit guidelines

Refs: https://github.com/nodejs/node/pull/19739

Merge request reports

Loading