Skip to content

Commit 7ae7635

Browse files
committed
Use a simple buffer list to improve performance
fixes #566 fixes #1590
1 parent 1da81f4 commit 7ae7635

File tree

3 files changed

+54
-24
lines changed

3 files changed

+54
-24
lines changed

Changes.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ you spot any mistakes.
1313
* Fix edge cases when determing Query result packets #1547
1414
* Fix memory leak when using long-running domains #1619 #1620
1515
* Update `bignumber.js` to 3.1.2
16+
* Use a simple buffer list to improve performance #566 #1590
1617

1718
## v2.12.0 (2016-11-02)
1819

lib/protocol/BufferList.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
2+
module.exports = BufferList;
3+
function BufferList() {
4+
this.bufs = [];
5+
this.size = 0;
6+
}
7+
8+
BufferList.prototype.shift = function shift() {
9+
var buf = this.bufs.shift();
10+
11+
if (buf) {
12+
this.size -= buf.length;
13+
}
14+
15+
return buf;
16+
};
17+
18+
BufferList.prototype.push = function push(buf) {
19+
if (!buf || !buf.length) {
20+
return;
21+
}
22+
23+
this.bufs.push(buf);
24+
this.size += buf.length;
25+
};

lib/protocol/Parser.js

Lines changed: 28 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,16 @@ var MAX_PACKET_LENGTH = Math.pow(2, 24) - 1;
22
var MUL_32BIT = Math.pow(2, 32);
33
var PacketHeader = require('./PacketHeader');
44
var BigNumber = require('bignumber.js');
5+
var BufferList = require('./BufferList');
56

67
module.exports = Parser;
78
function Parser(options) {
89
options = options || {};
910

1011
this._supportBigNumbers = options.config && options.config.supportBigNumbers;
1112
this._buffer = new Buffer(0);
12-
this._nextBuffers = [];
13-
this._longPacketBuffers = [];
13+
this._nextBuffers = new BufferList();
14+
this._longPacketBuffers = new BufferList();
1415
this._offset = 0;
1516
this._packetEnd = null;
1617
this._packetHeader = null;
@@ -374,53 +375,56 @@ Parser.prototype.resetPacketNumber = function() {
374375
this._nextPacketNumber = 0;
375376
};
376377

377-
Parser.prototype.packetLength = function() {
378-
return this._longPacketBuffers.reduce(function(length, buffer) {
379-
return length + buffer.length;
380-
}, this._packetHeader.length);
378+
Parser.prototype.packetLength = function packetLength() {
379+
if (!this._packetHeader) {
380+
return null;
381+
}
382+
383+
return this._packetHeader.length + this._longPacketBuffers.size;
381384
};
382385

383386
Parser.prototype._combineNextBuffers = function _combineNextBuffers(bytes) {
384387
if ((this._buffer.length - this._offset) >= bytes) {
385388
return true;
386389
}
387390

388-
if (!this._nextBuffers.length) {
391+
if (!this._nextBuffers.size) {
389392
return false;
390393
}
391394

392-
while (this._nextBuffers.length && (this._buffer.length - this._offset) < bytes) {
395+
while (this._nextBuffers.size && (this._buffer.length - this._offset) < bytes) {
393396
this.append(this._nextBuffers.shift());
394397
}
395398

396399
return (this._buffer.length - this._offset) >= bytes;
397400
};
398401

399402
Parser.prototype._combineLongPacketBuffers = function _combineLongPacketBuffers() {
400-
if (!this._longPacketBuffers.length) {
403+
if (!this._longPacketBuffers.size) {
401404
return;
402405
}
403406

407+
// Calculate bytes
408+
var remainingBytes = this._buffer.length - this._offset;
404409
var trailingPacketBytes = this._buffer.length - this._packetEnd;
405410

406-
var length = this._longPacketBuffers.reduce(function(length, buffer) {
407-
return length + buffer.length;
408-
}, (this._buffer.length - this._offset));
409-
410-
var combinedBuffer = new Buffer(length);
411+
// Create buffer
412+
var buf = null;
413+
var buffer = new Buffer(remainingBytes + this._longPacketBuffers.size);
414+
var offset = 0;
411415

412-
var offset = this._longPacketBuffers.reduce(function(offset, buffer) {
413-
buffer.copy(combinedBuffer, offset);
414-
return offset + buffer.length;
415-
}, 0);
416+
// Copy long buffers
417+
while ((buf = this._longPacketBuffers.shift())) {
418+
offset += buf.copy(buffer, offset);
419+
}
416420

417-
this._buffer.copy(combinedBuffer, offset, this._offset);
421+
// Copy remaining bytes
422+
this._buffer.copy(buffer, offset, this._offset);
418423

419-
this._buffer = combinedBuffer;
420-
this._longPacketBuffers = [];
421-
this._offset = 0;
422-
this._packetEnd = this._buffer.length - trailingPacketBytes;
423-
this._packetOffset = 0;
424+
this._buffer = buffer;
425+
this._offset = 0;
426+
this._packetEnd = this._buffer.length - trailingPacketBytes;
427+
this._packetOffset = 0;
424428
};
425429

426430
Parser.prototype._advanceToNextPacket = function() {

0 commit comments

Comments
 (0)