Skip to content

Commit

Permalink
Refactor some code to reduce code size
Browse files Browse the repository at this point in the history
  • Loading branch information
Ruben Bridgewater committed May 25, 2016
1 parent 16d7e7a commit f4fd340
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 17 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ To handle protocol errors (this is very unlikely to happen) gracefully you shoul

## Contribute

The parser is highly optimized but there may still be further optimizations possible.
The parser is highly optimized but there may still be further optimizations possible.

```
npm install
Expand Down
27 changes: 11 additions & 16 deletions lib/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ function parseStringNumbers (parser) {
*/
function convertBufferRange (parser, start, end) {
// If returnBuffers is active, all return values are returned as buffers besides numbers and errors
parser.offset = end + 2
if (parser.optionReturnBuffers === true) {
return parser.buffer.slice(start, end)
}
Expand All @@ -82,10 +83,10 @@ function parseSimpleStringViaOffset (parser) {
var start = parser.offset
var offset = parser.offset
var length = parser.buffer.length
var buffer = parser.buffer

while (offset < length) {
if (parser.buffer[offset++] === 10) { // \r\n
parser.offset = offset
if (buffer[offset++] === 10) { // \r\n
return convertBufferRange(parser, start, offset - 2)
}
}
Expand Down Expand Up @@ -140,10 +141,7 @@ function parseBulkString (parser) {
return
}

var offsetBegin = parser.offset
parser.offset = offsetEnd + 2

return convertBufferRange(parser, offsetBegin, offsetEnd)
return convertBufferRange(parser, parser.offset, offsetEnd)
}

/**
Expand Down Expand Up @@ -284,23 +282,22 @@ function JavascriptRedisParser (options) {
function concatBulkString (parser) {
var list = parser.bufferCache
// The first chunk might contain the whole bulk string including the \r
var end = parser.totalChunkSize - parser.bigStrSize === -1
var chunks = list.length
if (end) {
parser.offset = 1
var offset = parser.bigStrSize - parser.totalChunkSize
parser.offset = offset
if (offset === 1) {
if (chunks === 2) {
return list[0].toString('utf8', parser.bigOffset, list[0].length - 1)
}
} else {
parser.offset = parser.bigStrSize - parser.totalChunkSize
chunks++
}
var res = list[0].toString('utf8', parser.bigOffset)
for (var i = 1; i < chunks - 2; i++) {
// We are only safe to fully add up elements that are neither the first nor any of the last two elements
res += list[i].toString()
}
res += list[i].toString('utf8', 0, end ? list[i].length - 1 : parser.offset - 2)
res += list[i].toString('utf8', 0, offset === 1 ? list[i].length - 1 : offset - 2)
return res
}

Expand Down Expand Up @@ -357,17 +354,15 @@ JavascriptRedisParser.prototype.execute = function (buffer) {
if (bufferPool.length < newLength) { // We can't rely on the chunk size
bufferPool = new Buffer(newLength)
}
var newBuffer = bufferPool
this.buffer.copy(newBuffer, 0, this.offset, oldLength)
buffer.copy(newBuffer, remainingLength, 0, buffer.length)
this.buffer = newBuffer.slice(0, newLength)
this.buffer.copy(bufferPool, 0, this.offset, oldLength)
buffer.copy(bufferPool, remainingLength, 0, buffer.length)
this.buffer = bufferPool.slice(0, newLength)
this.offset = 0
} else if (this.totalChunkSize + buffer.length >= this.bigStrSize) {
this.bufferCache.push(buffer)
// The returned type might be Array * (42) and in that case we can't improve the parsing currently
if (this.optionReturnBuffers === false && this.buffer[this.offset] === 36) {
this.returnReply(concatBulkString(this))
this.bigOffset = 0
this.buffer = buffer
} else { // This applies for arrays too
this.buffer = concatBuffer(this, this.totalChunkSize + buffer.length)
Expand Down

0 comments on commit f4fd340

Please sign in to comment.