Skip to content

Commit f4fd340

Browse files
author
Ruben Bridgewater
committed
Refactor some code to reduce code size
1 parent 16d7e7a commit f4fd340

File tree

2 files changed

+12
-17
lines changed

2 files changed

+12
-17
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ To handle protocol errors (this is very unlikely to happen) gracefully you shoul
9090

9191
## Contribute
9292

93-
The parser is highly optimized but there may still be further optimizations possible.
93+
The parser is highly optimized but there may still be further optimizations possible.
9494

9595
```
9696
npm install

lib/parser.js

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ function parseStringNumbers (parser) {
6565
*/
6666
function convertBufferRange (parser, start, end) {
6767
// If returnBuffers is active, all return values are returned as buffers besides numbers and errors
68+
parser.offset = end + 2
6869
if (parser.optionReturnBuffers === true) {
6970
return parser.buffer.slice(start, end)
7071
}
@@ -82,10 +83,10 @@ function parseSimpleStringViaOffset (parser) {
8283
var start = parser.offset
8384
var offset = parser.offset
8485
var length = parser.buffer.length
86+
var buffer = parser.buffer
8587

8688
while (offset < length) {
87-
if (parser.buffer[offset++] === 10) { // \r\n
88-
parser.offset = offset
89+
if (buffer[offset++] === 10) { // \r\n
8990
return convertBufferRange(parser, start, offset - 2)
9091
}
9192
}
@@ -140,10 +141,7 @@ function parseBulkString (parser) {
140141
return
141142
}
142143

143-
var offsetBegin = parser.offset
144-
parser.offset = offsetEnd + 2
145-
146-
return convertBufferRange(parser, offsetBegin, offsetEnd)
144+
return convertBufferRange(parser, parser.offset, offsetEnd)
147145
}
148146

149147
/**
@@ -284,23 +282,22 @@ function JavascriptRedisParser (options) {
284282
function concatBulkString (parser) {
285283
var list = parser.bufferCache
286284
// The first chunk might contain the whole bulk string including the \r
287-
var end = parser.totalChunkSize - parser.bigStrSize === -1
288285
var chunks = list.length
289-
if (end) {
290-
parser.offset = 1
286+
var offset = parser.bigStrSize - parser.totalChunkSize
287+
parser.offset = offset
288+
if (offset === 1) {
291289
if (chunks === 2) {
292290
return list[0].toString('utf8', parser.bigOffset, list[0].length - 1)
293291
}
294292
} else {
295-
parser.offset = parser.bigStrSize - parser.totalChunkSize
296293
chunks++
297294
}
298295
var res = list[0].toString('utf8', parser.bigOffset)
299296
for (var i = 1; i < chunks - 2; i++) {
300297
// We are only safe to fully add up elements that are neither the first nor any of the last two elements
301298
res += list[i].toString()
302299
}
303-
res += list[i].toString('utf8', 0, end ? list[i].length - 1 : parser.offset - 2)
300+
res += list[i].toString('utf8', 0, offset === 1 ? list[i].length - 1 : offset - 2)
304301
return res
305302
}
306303

@@ -357,17 +354,15 @@ JavascriptRedisParser.prototype.execute = function (buffer) {
357354
if (bufferPool.length < newLength) { // We can't rely on the chunk size
358355
bufferPool = new Buffer(newLength)
359356
}
360-
var newBuffer = bufferPool
361-
this.buffer.copy(newBuffer, 0, this.offset, oldLength)
362-
buffer.copy(newBuffer, remainingLength, 0, buffer.length)
363-
this.buffer = newBuffer.slice(0, newLength)
357+
this.buffer.copy(bufferPool, 0, this.offset, oldLength)
358+
buffer.copy(bufferPool, remainingLength, 0, buffer.length)
359+
this.buffer = bufferPool.slice(0, newLength)
364360
this.offset = 0
365361
} else if (this.totalChunkSize + buffer.length >= this.bigStrSize) {
366362
this.bufferCache.push(buffer)
367363
// The returned type might be Array * (42) and in that case we can't improve the parsing currently
368364
if (this.optionReturnBuffers === false && this.buffer[this.offset] === 36) {
369365
this.returnReply(concatBulkString(this))
370-
this.bigOffset = 0
371366
this.buffer = buffer
372367
} else { // This applies for arrays too
373368
this.buffer = concatBuffer(this, this.totalChunkSize + buffer.length)

0 commit comments

Comments
 (0)