Skip to content

Commit 4641add

Browse files
author
Your Name
committed
Eliminate base64 allocation hotspots in Socket.js paused data handling
- Store decoded Buffer objects directly in _pausedDataEvents instead of base64 strings - Avoid repeated base64 decode/encode cycles when recovering paused data - Update type annotations to reflect buffer storage - Maintain base64 conversion only for final bridge communication This eliminates GC pressure from base64 string allocations during pause/resume cycles
1 parent 0f3a4c1 commit 4641add

File tree

1 file changed

+9
-5
lines changed

1 file changed

+9
-5
lines changed

src/Socket.js

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ export default class Socket extends EventEmitter {
106106
// TODO: Add readOnly and writeOnly states
107107
/** @type {'opening' | 'open' | 'readOnly' | 'writeOnly'} @private */
108108
this._readyState = 'open'; // Incorrect, but matches NodeJS behavior
109-
/** @type {{ id: number; data: string; }[]} @private */
109+
/** @type {{ id: number; buffer: Buffer; }[]} @private */
110110
this._pausedDataEvents = [];
111111
this.readableHighWaterMark = 16384;
112112
this.writableHighWaterMark = 16384;
@@ -471,14 +471,15 @@ export default class Socket extends EventEmitter {
471471
let readBytes = 0;
472472
let i = 0;
473473
for (; i < this._pausedDataEvents.length; i++) {
474-
const evtData = Buffer.from(this._pausedDataEvents[i].data, 'base64');
474+
const evtData = this._pausedDataEvents[i].buffer;
475475
readBytes += evtData.byteLength;
476476
if (readBytes <= this.readableHighWaterMark) {
477477
buffArray.push(evtData);
478478
} else {
479479
const buffOffset = this.readableHighWaterMark - readBytes;
480480
buffArray.push(evtData.slice(0, buffOffset));
481-
this._pausedDataEvents[i].data = evtData.slice(buffOffset).toString('base64');
481+
// Store remaining buffer directly (no base64 conversion)
482+
this._pausedDataEvents[i].buffer = evtData.slice(buffOffset);
482483
break;
483484
}
484485
}
@@ -533,8 +534,11 @@ export default class Socket extends EventEmitter {
533534
this.emit('data', finalData);
534535
}
535536
} else {
536-
// If the socket is paused, save the data events for later
537-
this._pausedDataEvents.push(evt);
537+
// If the socket is paused, save the decoded buffer to avoid repeated base64 decoding
538+
this._pausedDataEvents.push({
539+
id: evt.id,
540+
buffer: Buffer.from(evt.data, 'base64')
541+
});
538542
}
539543
};
540544

0 commit comments

Comments
 (0)