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 73a8b66
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 29 deletions.
14 changes: 7 additions & 7 deletions benchmark/buffers/buffer-from.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,16 @@ const common = require('../common.js');
const assert = require('assert');
const bench = common.createBenchmark(main, {
source: [
'array',
'arraybuffer',
'arraybuffer-middle',
'buffer',
// 'array',
// 'arraybuffer',
// 'arraybuffer-middle',
// 'buffer',
'string',
'string-utf8',
'string-base64',
'object',
'uint8array',
'uint16array',
// 'object',
// 'uint8array',
// 'uint16array',
],
len: [100, 2048],
n: [8e5],
Expand Down
10 changes: 1 addition & 9 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 @@ -444,19 +443,12 @@ function allocate(size) {
function fromStringFast(string, ops) {
const length = ops.byteLength(string);

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

if (length > (poolSize - poolOffset))
createPool();
let b = new FastBuffer(allocPool, poolOffset, length);
let b = allocate(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 73a8b66

Please sign in to comment.