Skip to content

Commit

Permalink
buffer: rm createFromString and use fast Buffer.write
Browse files Browse the repository at this point in the history
  • Loading branch information
ronag committed Aug 12, 2024
1 parent 298ff4f commit 19a3db1
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 19 deletions.
27 changes: 21 additions & 6 deletions lib/buffer.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@ const {
compare: _compare,
compareOffset,
copy: _copy,
createFromString,
fill: bindingFill,
isAscii: bindingIsAscii,
isUtf8: bindingIsUtf8,
Expand Down Expand Up @@ -442,21 +441,37 @@ function allocate(size) {
}

function fromStringFast(string, ops) {
const length = ops.byteLength(string);
const maxLength = string.length * 4; // max utf8 length

if (length >= (Buffer.poolSize >>> 1))
return createFromString(string, ops.encodingVal);
if (maxLength >= (Buffer.poolSize >>> 1))
return fromStringSlow(string, ops);

if (length > (poolSize - poolOffset))
if (maxLength > (poolSize - poolOffset))
createPool();
let b = new FastBuffer(allocPool, poolOffset, length);

let b = new FastBuffer(allocPool, poolOffset, maxLength);

Check failure on line 452 in lib/buffer.js

View workflow job for this annotation

GitHub Actions / lint-js-and-md

'b' is never reassigned. Use 'const' instead
const actual = ops.write(b, string, 0, maxLength);

poolOffset += actual;
alignPool();

return new FastBuffer(allocPool, poolOffset, actual);
}

function fromStringSlow(string, ops) {
const length = ops.byteLength(string);

let b = createUnsafeBuffer(length);

const actual = ops.write(b, string, 0, length);
if (actual !== length) {
// byteLength() may overestimate. That's a rare case, though.
b = new FastBuffer(allocPool, poolOffset, actual);
}

poolOffset += actual;
alignPool();

return b;
}

Expand Down
13 changes: 0 additions & 13 deletions src/node_buffer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -530,17 +530,6 @@ MaybeLocal<Object> New(Environment* env,

namespace {

void CreateFromString(const FunctionCallbackInfo<Value>& args) {
CHECK(args[0]->IsString());
CHECK(args[1]->IsInt32());

enum encoding enc = static_cast<enum encoding>(args[1].As<Int32>()->Value());
Local<Object> buf;
if (New(args.GetIsolate(), args[0].As<String>(), enc).ToLocal(&buf))
args.GetReturnValue().Set(buf);
}


template <encoding encoding>
void StringSlice(const FunctionCallbackInfo<Value>& args) {
Environment* env = Environment::GetCurrent(args);
Expand Down Expand Up @@ -1436,7 +1425,6 @@ void Initialize(Local<Object> target,
SetMethodNoSideEffect(context, target, "btoa", Btoa);

SetMethod(context, target, "setBufferPrototype", SetBufferPrototype);
SetMethodNoSideEffect(context, target, "createFromString", CreateFromString);

SetFastMethodNoSideEffect(context,
target,
Expand Down Expand Up @@ -1501,7 +1489,6 @@ void Initialize(Local<Object> target,

void RegisterExternalReferences(ExternalReferenceRegistry* registry) {
registry->Register(SetBufferPrototype);
registry->Register(CreateFromString);

registry->Register(SlowByteLengthUtf8);
registry->Register(fast_byte_length_utf8.GetTypeInfo());
Expand Down

0 comments on commit 19a3db1

Please sign in to comment.