Skip to content

Commit 204efe4

Browse files
author
Ruben Bridgewater
committed
Ugly parser fix for multiple concurrent parsers using the bufferPool
1 parent 98aa1f2 commit 204efe4

File tree

1 file changed

+17
-7
lines changed

1 file changed

+17
-7
lines changed

lib/parser.js

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,6 @@ function parseBulkString (parser) {
137137
parser.bigStrSize = offsetEnd + 2
138138
parser.bigOffset = parser.offset
139139
parser.totalChunkSize = parser.buffer.length
140-
parser.bufferCache.push(parser.buffer)
141140
return
142141
}
143142

@@ -348,20 +347,20 @@ JavascriptRedisParser.prototype.execute = function (buffer) {
348347
this.offset = 0
349348
} else if (this.bigStrSize === 0) {
350349
var oldLength = this.buffer.length
351-
var remainingLength = oldLength - this.offset
352-
var newLength = remainingLength + buffer.length
350+
var newLength = oldLength + buffer.length
353351
// ~ 5% speed increase over using new Buffer(length) all the time
354352
if (bufferPool.length < newLength) { // We can't rely on the chunk size
355353
bufferPool = new Buffer(newLength)
356354
}
357-
this.buffer.copy(bufferPool, 0, this.offset, oldLength)
358-
buffer.copy(bufferPool, remainingLength, 0, buffer.length)
355+
this.buffer.copy(bufferPool, 0, 0, oldLength)
356+
buffer.copy(bufferPool, oldLength, 0, buffer.length)
359357
this.buffer = bufferPool.slice(0, newLength)
360358
this.offset = 0
361359
} else if (this.totalChunkSize + buffer.length >= this.bigStrSize) {
360+
this.bufferCache.unshift(this.buffer)
362361
this.bufferCache.push(buffer)
363362
// The returned type might be Array * (42) and in that case we can't improve the parsing currently
364-
if (this.optionReturnBuffers === false && this.buffer[this.offset] === 36) {
363+
if (this.optionReturnBuffers === false && this.buffer[0] === 36) {
365364
this.returnReply(concatBulkString(this))
366365
this.buffer = buffer
367366
} else { // This applies for arrays too
@@ -382,7 +381,18 @@ JavascriptRedisParser.prototype.execute = function (buffer) {
382381
var type = this.buffer[this.offset++]
383382
var response = parseType(this, type)
384383
if (response === undefined) {
385-
this.offset = offset
384+
if (this.buffer === null) {
385+
return
386+
}
387+
var tempBuffer = new Buffer(this.buffer.length - offset)
388+
this.buffer.copy(tempBuffer, 0, offset, this.buffer.length)
389+
this.buffer = tempBuffer
390+
if (this.bigStrSize !== 0) {
391+
this.bigStrSize -= offset
392+
this.bigOffset -= offset
393+
this.totalChunkSize -= offset
394+
}
395+
this.offset = 0
386396
return
387397
}
388398

0 commit comments

Comments
 (0)