Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

lib: revert primordials in a hot path #38246

Closed
wants to merge 8 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
16 changes: 6 additions & 10 deletions lib/internal/streams/buffer_list.js
@@ -1,9 +1,7 @@
'use strict';

const {
StringPrototypeSlice,
SymbolIterator,
TypedArrayPrototypeSet,
Uint8Array,
} = primordials;

Expand Down Expand Up @@ -69,7 +67,7 @@ module.exports = class BufferList {
let p = this.head;
let i = 0;
while (p) {
TypedArrayPrototypeSet(ret, p.data, i);
ret.set(p.data, i);
i += p.data.length;
p = p.next;
}
Expand Down Expand Up @@ -122,9 +120,9 @@ module.exports = class BufferList {
else
this.head = this.tail = null;
} else {
ret += StringPrototypeSlice(str, 0, n);
ret += str.slice(0, n);
this.head = p;
p.data = StringPrototypeSlice(str, n);
p.data = str.slice(n);
}
break;
}
Expand All @@ -143,20 +141,18 @@ module.exports = class BufferList {
do {
const buf = p.data;
if (n > buf.length) {
TypedArrayPrototypeSet(ret, buf, retLen - n);
ret.set(buf, retLen - n);
n -= buf.length;
} else {
if (n === buf.length) {
TypedArrayPrototypeSet(ret, buf, retLen - n);
ret.set(buf, retLen - n);
++c;
if (p.next)
this.head = p.next;
else
this.head = this.tail = null;
} else {
TypedArrayPrototypeSet(ret,
new Uint8Array(buf.buffer, buf.byteOffset, n),
retLen - n);
ret.set(new Uint8Array(buf.buffer, buf.byteOffset, n), retLen - n);
this.head = p;
p.data = buf.slice(n);
}
Expand Down
10 changes: 2 additions & 8 deletions lib/internal/streams/destroy.js
Expand Up @@ -6,10 +6,6 @@ const {
ERR_MULTIPLE_CALLBACK,
},
} = require('internal/errors');
const {
FunctionPrototypeCall,
Symbol,
} = primordials;

const kDestroy = Symbol('kDestroy');
const kConstruct = Symbol('kConstruct');
Expand Down Expand Up @@ -99,8 +95,7 @@ function _destroy(self, err, cb) {
try {
const then = result.then;
if (typeof then === 'function') {
FunctionPrototypeCall(
then,
then.call(
result,
function() {
if (called)
Expand Down Expand Up @@ -318,8 +313,7 @@ function constructNT(stream) {
try {
const then = result.then;
if (typeof then === 'function') {
FunctionPrototypeCall(
then,
then.call(
result,
function() {
// If the callback was invoked, do nothing further.
Expand Down
25 changes: 9 additions & 16 deletions lib/internal/streams/end-of-stream.js
Expand Up @@ -3,11 +3,6 @@

'use strict';

const {
FunctionPrototype,
FunctionPrototypeCall,
ReflectApply,
} = primordials;
const {
AbortError,
codes,
Expand Down Expand Up @@ -55,7 +50,7 @@ function isWritableFinished(stream) {
return wState.finished || (wState.ended && wState.length === 0);
}

const nop = FunctionPrototype;
function nop() {}

function isReadableEnded(stream) {
if (stream.readableEnded) return true;
Expand Down Expand Up @@ -113,7 +108,7 @@ function eos(stream, options, callback) {
if (stream.destroyed) willEmitClose = false;

if (willEmitClose && (!stream.readable || readable)) return;
if (!readable || readableEnded) FunctionPrototypeCall(callback, stream);
if (!readable || readableEnded) callback.call(stream);
};

let readableEnded = stream.readableEnded ||
Expand All @@ -126,25 +121,23 @@ function eos(stream, options, callback) {
if (stream.destroyed) willEmitClose = false;

if (willEmitClose && (!stream.writable || writable)) return;
if (!writable || writableFinished) FunctionPrototypeCall(callback, stream);
if (!writable || writableFinished) callback.call(stream);
};

const onerror = (err) => {
FunctionPrototypeCall(callback, stream, err);
callback.call(stream, err);
};

const onclose = () => {
if (readable && !readableEnded) {
if (!isReadableEnded(stream))
return FunctionPrototypeCall(callback, stream,
new ERR_STREAM_PREMATURE_CLOSE());
return callback.call(stream, new ERR_STREAM_PREMATURE_CLOSE());
}
if (writable && !writableFinished) {
if (!isWritableFinished(stream))
return FunctionPrototypeCall(callback, stream,
new ERR_STREAM_PREMATURE_CLOSE());
return callback.call(stream, new ERR_STREAM_PREMATURE_CLOSE());
}
FunctionPrototypeCall(callback, stream);
callback.call(stream);
};

const onrequest = () => {
Expand Down Expand Up @@ -218,15 +211,15 @@ function eos(stream, options, callback) {
// Keep it because cleanup removes it.
const endCallback = callback;
cleanup();
FunctionPrototypeCall(endCallback, stream, new AbortError());
endCallback.call(stream, new AbortError());
};
if (options.signal.aborted) {
process.nextTick(abort);
} else {
const originalCallback = callback;
callback = once((...args) => {
options.signal.removeEventListener('abort', abort);
ReflectApply(originalCallback, stream, args);
Reflect.apply(originalCallback, stream, args);
});
options.signal.addEventListener('abort', abort);
}
Expand Down
8 changes: 3 additions & 5 deletions lib/internal/streams/from.js
@@ -1,7 +1,6 @@
'use strict';

const {
PromisePrototypeThen,
SymbolAsyncIterator,
SymbolIterator,
} = primordials;
Expand Down Expand Up @@ -55,10 +54,9 @@ function from(Readable, iterable, opts) {
};

readable._destroy = function(error, cb) {
PromisePrototypeThen(
close(error),
() => process.nextTick(cb, error), // nextTick is here in case cb throws
(e) => process.nextTick(cb, e || error),
close(error).then(
() => process.nextTick(cb, error),
(e) => process.nextTick(cb, error || e),
);
};

Expand Down
3 changes: 1 addition & 2 deletions lib/internal/streams/lazy_transform.js
Expand Up @@ -4,7 +4,6 @@
'use strict';

const {
FunctionPrototypeCall,
ObjectDefineProperties,
ObjectDefineProperty,
ObjectSetPrototypeOf,
Expand All @@ -26,7 +25,7 @@ ObjectSetPrototypeOf(LazyTransform, stream.Transform);

function makeGetter(name) {
return function() {
FunctionPrototypeCall(stream.Transform, this, this._options);
stream.Transform.call(this, this._options);
this._writableState.decodeStrings = false;

if (!this._options || !this._options.defaultEncoding) {
Expand Down
6 changes: 2 additions & 4 deletions lib/internal/streams/legacy.js
Expand Up @@ -2,15 +2,13 @@

const {
ArrayIsArray,
ArrayPrototypeUnshift,
FunctionPrototypeCall,
ObjectSetPrototypeOf,
} = primordials;

const EE = require('events');

function Stream(opts) {
FunctionPrototypeCall(EE, this, opts);
EE.call(this, opts);
}
ObjectSetPrototypeOf(Stream.prototype, EE.prototype);
ObjectSetPrototypeOf(Stream, EE);
Expand Down Expand Up @@ -108,7 +106,7 @@ function prependListener(emitter, event, fn) {
if (!emitter._events || !emitter._events[event])
emitter.on(event, fn);
else if (ArrayIsArray(emitter._events[event]))
ArrayPrototypeUnshift(emitter._events[event], fn);
emitter._events[event].unshift(fn);
else
emitter._events[event] = [fn, emitter._events[event]];
}
Expand Down
3 changes: 1 addition & 2 deletions lib/internal/streams/passthrough.js
Expand Up @@ -26,7 +26,6 @@
'use strict';

const {
FunctionPrototypeCall,
ObjectSetPrototypeOf,
} = primordials;

Expand All @@ -40,7 +39,7 @@ function PassThrough(options) {
if (!(this instanceof PassThrough))
return new PassThrough(options);

FunctionPrototypeCall(Transform, this, options);
Transform.call(this, options);
}

PassThrough.prototype._transform = function(chunk, encoding, cb) {
Expand Down
14 changes: 5 additions & 9 deletions lib/internal/streams/pipeline.js
Expand Up @@ -5,10 +5,6 @@

const {
ArrayIsArray,
ArrayPrototypePop,
ArrayPrototypePush,
ArrayPrototypeShift,
FunctionPrototypeCall,
ReflectApply,
SymbolAsyncIterator,
} = primordials;
Expand Down Expand Up @@ -84,7 +80,7 @@ function popCallback(streams) {
// a single stream. Therefore optimize for the average case instead of
// checking for length === 0 as well.
validateCallback(streams[streams.length - 1]);
return ArrayPrototypePop(streams);
return streams.pop();
}

function makeAsyncIterable(val) {
Expand All @@ -103,7 +99,7 @@ async function* fromReadable(val) {
Readable = require('internal/streams/readable');
}

yield* FunctionPrototypeCall(Readable.prototype[SymbolAsyncIterator], val);
yield* Readable.prototype[SymbolAsyncIterator].call(val);
}

async function pump(iterable, writable, finish) {
Expand Down Expand Up @@ -160,7 +156,7 @@ function pipeline(...streams) {
}

while (destroys.length) {
ArrayPrototypeShift(destroys)(error);
destroys.shift()(error);
}

if (final) {
Expand All @@ -176,7 +172,7 @@ function pipeline(...streams) {

if (isStream(stream)) {
finishCount++;
ArrayPrototypePush(destroys, destroyer(stream, reading, writing, finish));
destroys.push(destroyer(stream, reading, writing, finish));
}

if (i === 0) {
Expand Down Expand Up @@ -239,7 +235,7 @@ function pipeline(...streams) {
ret = pt;

finishCount++;
ArrayPrototypePush(destroys, destroyer(ret, false, true, finish));
destroys.push(destroyer(ret, false, true, finish));
}
} else if (isStream(stream)) {
if (isReadable(ret)) {
Expand Down