Skip to content

Commit ca5c10a

Browse files
committed
clean up connection slightly & add initial bench
1 parent 835f71a commit ca5c10a

File tree

2 files changed

+32
-25
lines changed

2 files changed

+32
-25
lines changed

benchmark/835f71a76f.txt

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
benchmark
2+
starting simple-query-parsing
3+
3703 ops/sec - (100/0.027)
4+
7299 ops/sec - (1000/0.137)
5+
8888 ops/sec - (10000/1.125)
6+
8733 ops/sec - (10000/1.145)
7+
8810 ops/sec - (10000/1.135)
8+
8771 ops/sec - (10000/1.14)
9+
starting prepared-statement-parsing
10+
3846 ops/sec - (100/0.026)
11+
7299 ops/sec - (1000/0.137)
12+
7225 ops/sec - (10000/1.384)
13+
7288 ops/sec - (10000/1.372)
14+
7225 ops/sec - (10000/1.384)
15+
7457 ops/sec - (10000/1.341)
16+
done
17+

lib/connection.js

+15-25
Original file line numberDiff line numberDiff line change
@@ -474,7 +474,7 @@ Connection.prototype.parseT = function(msg) {
474474
msg.fieldCount = this.parseInt16();
475475
var fields = [];
476476
for(var i = 0; i < msg.fieldCount; i++){
477-
fields[i] = this.parseField();
477+
fields.push(this.parseField());
478478
}
479479
msg.fields = fields;
480480
return msg;
@@ -498,7 +498,11 @@ Connection.prototype.parseD = function(msg) {
498498
var fields = [];
499499
for(var i = 0; i < fieldCount; i++) {
500500
var length = this.parseInt32();
501-
fields[i] = (length === -1 ? null : this.readBytes(length));
501+
var value = null;
502+
if(length !== -1) {
503+
value = this.readBytes(length);
504+
}
505+
fields.push(value);
502506
}
503507
msg.fieldCount = fieldCount;
504508
msg.fields = fields;
@@ -553,49 +557,35 @@ Connection.prototype.parseA = function(msg) {
553557
};
554558

555559
Connection.prototype.parseGH = function (msg) {
556-
msg.binary = Boolean(this.parseInt8());
560+
var isBinary = this.buffer[this.offset] !== 0;
561+
this.offset++;
562+
msg.binary = isBinary;
557563
var columnCount = this.parseInt16();
558564
msg.columnTypes = [];
559565
for(var i = 0; i<columnCount; i++) {
560-
msg.columnTypes[i] = this.parseInt16();
566+
msg.columnTypes.push(this.parseInt16());
561567
}
562568
return msg;
563569
};
564570

565-
Connection.prototype.parseInt8 = function () {
566-
var value = Number(this.buffer[this.offset]);
567-
this.offset++;
568-
return value;
569-
};
570-
571571
Connection.prototype.readChar = function() {
572572
return Buffer([this.buffer[this.offset++]]).toString(this.encoding);
573573
};
574574

575575
Connection.prototype.parseInt32 = function() {
576-
var value = this.peekInt32();
576+
var value = this.buffer.readInt32BE(this.offset, true);
577577
this.offset += 4;
578578
return value;
579579
};
580580

581-
Connection.prototype.peekInt32 = function(offset) {
582-
offset = offset || this.offset;
583-
var buffer = this.buffer;
584-
return ((buffer[offset++] << 24) +
585-
(buffer[offset++] << 16) +
586-
(buffer[offset++] << 8) +
587-
buffer[offset++]);
588-
};
589-
590-
591581
Connection.prototype.parseInt16 = function() {
592-
return ((this.buffer[this.offset++] << 8) +
593-
(this.buffer[this.offset++] << 0));
582+
var value = this.buffer.readInt16BE(this.offset, true);
583+
this.offset += 2;
584+
return value;
594585
};
595586

596587
Connection.prototype.readString = function(length) {
597-
return this.buffer.toString(this.encoding, this.offset,
598-
(this.offset += length));
588+
return this.buffer.toString(this.encoding, this.offset, (this.offset += length));
599589
};
600590

601591
Connection.prototype.readBytes = function(length) {

0 commit comments

Comments
 (0)