Skip to content
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
child_process: use primordials
Suggested by @himself65 and @anonrig
  • Loading branch information
CaramelFur committed Jun 15, 2022
commit abd50a083fb939e13f2f208080520f29f2f585ed
7 changes: 5 additions & 2 deletions lib/internal/child_process/serialization.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ const {
JSONParse,
JSONStringify,
StringPrototypeSplit,
ArrayPrototypePush,
ReflectApply,
Symbol,
TypedArrayPrototypeSubarray,
} = primordials;
Expand All @@ -13,6 +15,7 @@ const v8 = require('v8');
const { isArrayBufferView } = require('internal/util/types');
const assert = require('internal/assert');
const { streamBaseState, kLastWriteWasAsync } = internalBinding('stream_wrap');
const { readUInt32BE } = require('internal/buffer');

const kMessageBuffer = Symbol('kMessageBuffer');
const kMessageBufferSize = Symbol('kMessageBufferSize');
Expand Down Expand Up @@ -60,15 +63,15 @@ const advanced = {
*parseChannelMessages(channel, readData) {
if (readData.length === 0) return;

channel[kMessageBuffer].push(readData);
ArrayPrototypePush(channel[kMessageBuffer], readData);
channel[kMessageBufferSize] += readData.length;

// Index 0 should always be present because we just pushed data into it.
let messageBufferHead = channel[kMessageBuffer][0];
while (messageBufferHead.length >= 4) {
// We call `readUInt32BE` manually here, because this is faster than first converting
// it to a buffer and using `readUInt32BE` on that.
const fullMessageSize = Buffer.prototype.readUInt32BE.call(messageBufferHead, 0) + 4;
const fullMessageSize = ReflectApply(readUInt32BE, messageBufferHead, [0]) + 4;
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Drive-by suggestion: read the int inline, that's both faster and shorter (and arguably easier to read)

Suggested change
const fullMessageSize = ReflectApply(readUInt32BE, messageBufferHead, [0]) + 4;
const b = messageBufferHead;
const fullMessageSize = b[0] * 0x1000000 + b[1] * 65536 + b[2] * 256 + b[3];


if (channel[kMessageBufferSize] < fullMessageSize) break;

Expand Down