Skip to content

Commit 19a3db1

Browse files
committed
buffer: rm createFromString and use fast Buffer.write
1 parent 298ff4f commit 19a3db1

File tree

2 files changed

+21
-19
lines changed

2 files changed

+21
-19
lines changed

lib/buffer.js

+21-6
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,6 @@ const {
5959
compare: _compare,
6060
compareOffset,
6161
copy: _copy,
62-
createFromString,
6362
fill: bindingFill,
6463
isAscii: bindingIsAscii,
6564
isUtf8: bindingIsUtf8,
@@ -442,21 +441,37 @@ function allocate(size) {
442441
}
443442

444443
function fromStringFast(string, ops) {
445-
const length = ops.byteLength(string);
444+
const maxLength = string.length * 4; // max utf8 length
446445

447-
if (length >= (Buffer.poolSize >>> 1))
448-
return createFromString(string, ops.encodingVal);
446+
if (maxLength >= (Buffer.poolSize >>> 1))
447+
return fromStringSlow(string, ops);
449448

450-
if (length > (poolSize - poolOffset))
449+
if (maxLength > (poolSize - poolOffset))
451450
createPool();
452-
let b = new FastBuffer(allocPool, poolOffset, length);
451+
452+
let b = new FastBuffer(allocPool, poolOffset, maxLength);
453+
const actual = ops.write(b, string, 0, maxLength);
454+
455+
poolOffset += actual;
456+
alignPool();
457+
458+
return new FastBuffer(allocPool, poolOffset, actual);
459+
}
460+
461+
function fromStringSlow(string, ops) {
462+
const length = ops.byteLength(string);
463+
464+
let b = createUnsafeBuffer(length);
465+
453466
const actual = ops.write(b, string, 0, length);
454467
if (actual !== length) {
455468
// byteLength() may overestimate. That's a rare case, though.
456469
b = new FastBuffer(allocPool, poolOffset, actual);
457470
}
471+
458472
poolOffset += actual;
459473
alignPool();
474+
460475
return b;
461476
}
462477

src/node_buffer.cc

-13
Original file line numberDiff line numberDiff line change
@@ -530,17 +530,6 @@ MaybeLocal<Object> New(Environment* env,
530530

531531
namespace {
532532

533-
void CreateFromString(const FunctionCallbackInfo<Value>& args) {
534-
CHECK(args[0]->IsString());
535-
CHECK(args[1]->IsInt32());
536-
537-
enum encoding enc = static_cast<enum encoding>(args[1].As<Int32>()->Value());
538-
Local<Object> buf;
539-
if (New(args.GetIsolate(), args[0].As<String>(), enc).ToLocal(&buf))
540-
args.GetReturnValue().Set(buf);
541-
}
542-
543-
544533
template <encoding encoding>
545534
void StringSlice(const FunctionCallbackInfo<Value>& args) {
546535
Environment* env = Environment::GetCurrent(args);
@@ -1436,7 +1425,6 @@ void Initialize(Local<Object> target,
14361425
SetMethodNoSideEffect(context, target, "btoa", Btoa);
14371426

14381427
SetMethod(context, target, "setBufferPrototype", SetBufferPrototype);
1439-
SetMethodNoSideEffect(context, target, "createFromString", CreateFromString);
14401428

14411429
SetFastMethodNoSideEffect(context,
14421430
target,
@@ -1501,7 +1489,6 @@ void Initialize(Local<Object> target,
15011489

15021490
void RegisterExternalReferences(ExternalReferenceRegistry* registry) {
15031491
registry->Register(SetBufferPrototype);
1504-
registry->Register(CreateFromString);
15051492

15061493
registry->Register(SlowByteLengthUtf8);
15071494
registry->Register(fast_byte_length_utf8.GetTypeInfo());

0 commit comments

Comments
 (0)