Skip to content

Commit 626edae

Browse files
author
Ruben Bridgewater
committed
fix(jsparser): minor memory consumtion overhead
Chunked data might have used more bufferPool memory than required
1 parent 4a3e6e7 commit 626edae

File tree

4 files changed

+24
-18
lines changed

4 files changed

+24
-18
lines changed

benchmark/index.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,9 +68,10 @@ for (i = 0; i < bigArraySize; i++) {
6868
// A chunk has a maximum size of 2^16 bytes.
6969
size = 65000 + i
7070
if (i % 2) {
71-
bigArrayChunks.push(new Buffer('\r\n$' + size + '\r\n' + Array(size + 1).join('a') + '\r\n:' + (Math.random() * 1000000 | 0)))
71+
// The "x" in the beginning is important to prevent benchmark manipulation due to a minor jsparser optimization
72+
bigArrayChunks.push(new Buffer('x\r\n$' + size + '\r\n' + Array(size + 1).join('a') + '\r\n:' + (Math.random() * 1000000 | 0)))
7273
} else {
73-
bigArrayChunks.push(new Buffer('\r\n+this is some short text about nothing\r\n:' + size + '\r\n$' + size + '\r\n' + Array(size + 1).join('b')))
74+
bigArrayChunks.push(new Buffer('\r\n+this is some short text about nothing\r\n:' + size + '\r\n$' + size + '\r\n' + Array(size).join('b')))
7475
}
7576
}
7677
bigArrayChunks.push(new Buffer('\r\n'))

changelog.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
## v.2.4.1 - 05 Feb, 2017
2+
3+
Bugfixes
4+
5+
- Fixed minimal memory consumtion overhead for chunked buffers
6+
17
## v.2.4.0 - 25 Jan, 2017
28

39
Features

lib/parser.js

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -450,11 +450,12 @@ function concatBulkString (parser) {
450450
var chunks = list.length
451451
var offset = parser.bigStrSize - parser.totalChunkSize
452452
parser.offset = offset
453-
if (offset === 1) {
453+
if (offset <= 2) {
454454
if (chunks === 2) {
455-
return list[0].toString('utf8', parser.bigOffset, list[0].length - 1)
455+
return list[0].toString('utf8', parser.bigOffset, list[0].length + offset - 2)
456456
}
457457
chunks--
458+
offset = list[list.length - 2].length + offset
458459
}
459460
var res = decoder.write(list[0].slice(parser.bigOffset))
460461
for (var i = 1; i < chunks - 1; i++) {
@@ -478,25 +479,24 @@ function concatBulkBuffer (parser) {
478479
var length = parser.bigStrSize - parser.bigOffset - 2
479480
var offset = parser.bigStrSize - parser.totalChunkSize
480481
parser.offset = offset
481-
if (offset === 1) {
482+
if (offset <= 2) {
482483
if (chunks === 2) {
483-
return list[0].slice(parser.bigOffset, list[0].length - 1)
484+
return list[0].slice(parser.bigOffset, list[0].length + offset - 2)
484485
}
485486
chunks--
486-
offset = list[list.length - 1].length + 1
487+
offset = list[list.length - 2].length + offset
487488
}
488489
resizeBuffer(length)
489-
var pos = bufferOffset
490-
list[0].copy(bufferPool, pos, parser.bigOffset, list[0].length)
491-
pos += list[0].length - parser.bigOffset
492-
for (var i = 1; i < list.length - 1; i++) {
493-
list[i].copy(bufferPool, pos)
494-
pos += list[i].length
490+
var start = bufferOffset
491+
list[0].copy(bufferPool, start, parser.bigOffset, list[0].length)
492+
bufferOffset += list[0].length - parser.bigOffset
493+
for (var i = 1; i < chunks - 1; i++) {
494+
list[i].copy(bufferPool, bufferOffset)
495+
bufferOffset += list[i].length
495496
}
496-
list[i].copy(bufferPool, pos, 0, offset - 2)
497-
var buffer = bufferPool.slice(bufferOffset, length + bufferOffset)
498-
bufferOffset += length
499-
return buffer
497+
list[i].copy(bufferPool, bufferOffset, 0, offset - 2)
498+
bufferOffset += offset - 2
499+
return bufferPool.slice(start, bufferOffset)
500500
}
501501

502502
/**

test/parsers.spec.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -574,7 +574,6 @@ describe('parsers', function () {
574574
parser.execute(new Buffer(':'))
575575
assert.strictEqual(replyCount, 0)
576576
parser.execute(new Buffer('1'))
577-
assert.strictEqual(replyCount, 0)
578577
parser.execute(new Buffer('\r'))
579578
assert.strictEqual(replyCount, 0)
580579
parser.execute(new Buffer('\n'))

0 commit comments

Comments
 (0)