Skip to content

Commit b900bd6

Browse files
author
Ruben Bridgewater
committed
Some small parser changes
The small_to_string was actually quite slow
1 parent 6958c18 commit b900bd6

File tree

3 files changed

+14
-32
lines changed

3 files changed

+14
-32
lines changed

index.js

+1-3
Original file line numberDiff line numberDiff line change
@@ -271,9 +271,7 @@ RedisClient.prototype.init_parser = function () {
271271

272272
// return_buffers sends back Buffers from parser to callback. detect_buffers sends back Buffers from parser, but
273273
// converts to Strings if the input arguments are not Buffers.
274-
this.reply_parser = new this.parser_module.Parser({
275-
return_buffers: self.options.return_buffers || self.options.detect_buffers || false
276-
});
274+
this.reply_parser = new this.parser_module.Parser(self.options.return_buffers || self.options.detect_buffers || false);
277275
// Important: Only send results / errors async.
278276
// That way the result / error won't stay in a try catch block and catch user things
279277
this.reply_parser.send_error = function (data) {

lib/parser/hiredis.js

+6-7
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,15 @@
22

33
var hiredis = require("hiredis");
44

5-
exports.name = "hiredis";
6-
7-
function HiredisReplyParser(options) {
5+
function HiredisReplyParser(return_buffers) {
86
this.name = exports.name;
9-
this.options = options;
7+
this.return_buffers = return_buffers;
108
this.reset();
119
}
1210

13-
exports.Parser = HiredisReplyParser;
14-
1511
HiredisReplyParser.prototype.reset = function () {
1612
this.reader = new hiredis.Reader({
17-
return_buffers: this.options.return_buffers || false
13+
return_buffers: this.return_buffers || false
1814
});
1915
};
2016

@@ -35,3 +31,6 @@ HiredisReplyParser.prototype.execute = function (data) {
3531
}
3632
}
3733
};
34+
35+
exports.Parser = HiredisReplyParser;
36+
exports.name = "hiredis";

lib/parser/javascript.js

+7-22
Original file line numberDiff line numberDiff line change
@@ -7,37 +7,21 @@ function Packet(type, size) {
77
this.size = +size;
88
}
99

10-
exports.name = "javascript";
11-
12-
function ReplyParser(options) {
10+
function ReplyParser(return_buffers) {
1311
this.name = exports.name;
14-
this.options = options;
12+
this.return_buffers = return_buffers;
1513

1614
this._buffer = null;
1715
this._offset = 0;
1816
this._encoding = "utf-8";
19-
this._reply_type = null;
2017
}
2118

22-
exports.Parser = ReplyParser;
23-
2419
function IncompleteReadBuffer(message) {
2520
this.name = "IncompleteReadBuffer";
2621
this.message = message;
2722
}
2823
util.inherits(IncompleteReadBuffer, Error);
2924

30-
// Buffer.toString() is quite slow for small strings
31-
function small_toString(buf, start, end) {
32-
var tmp = "", i;
33-
34-
for (i = start; i < end; i++) {
35-
tmp += String.fromCharCode(buf[i]);
36-
}
37-
38-
return tmp;
39-
}
40-
4125
ReplyParser.prototype._parseResult = function (type) {
4226
var start, end, offset, packetHeader;
4327

@@ -56,10 +40,8 @@ ReplyParser.prototype._parseResult = function (type) {
5640

5741
if (type === 45) {
5842
return new Error(this._buffer.toString(this._encoding, start, end));
59-
} else if (this.options.return_buffers) {
43+
} else if (this.return_buffers) {
6044
return this._buffer.slice(start, end);
61-
} else if (end - start < 65536) { // completely arbitrary
62-
return small_toString(this._buffer, start, end);
6345
}
6446
return this._buffer.toString(this._encoding, start, end);
6547
} else if (type === 58) { // :
@@ -100,7 +82,7 @@ ReplyParser.prototype._parseResult = function (type) {
10082
throw new IncompleteReadBuffer("Wait for more data.");
10183
}
10284

103-
if (this.options.return_buffers) {
85+
if (this.return_buffers) {
10486
return this._buffer.slice(start, end);
10587
}
10688
return this._buffer.toString(this._encoding, start, end);
@@ -234,3 +216,6 @@ ReplyParser.prototype._packetEndOffset = function () {
234216
ReplyParser.prototype._bytesRemaining = function () {
235217
return (this._buffer.length - this._offset) < 0 ? 0 : (this._buffer.length - this._offset);
236218
};
219+
220+
exports.Parser = ReplyParser;
221+
exports.name = "javascript";

0 commit comments

Comments
 (0)