increase speed of javascript parser ~5%

This commit is contained in:
bmc 2013-04-08 19:04:17 -05:00
parent f16eaa8555
commit b33c266734
2 changed files with 35 additions and 4 deletions

17
benchmark/4e822a1.txt Normal file
View File

@ -0,0 +1,17 @@
benchmark
starting simple-query-parsing
4166 ops/sec - (100/0.024)
8333 ops/sec - (1000/0.12)
10405 ops/sec - (10000/0.961)
10515 ops/sec - (10000/0.951)
10638 ops/sec - (10000/0.94)
10460 ops/sec - (10000/0.956)
starting prepared-statement-parsing
4166 ops/sec - (100/0.024)
8264 ops/sec - (1000/0.121)
7530 ops/sec - (10000/1.328)
8250 ops/sec - (10000/1.212)
8156 ops/sec - (10000/1.226)
8110 ops/sec - (10000/1.233)
done

View File

@ -6,6 +6,8 @@ var util = require('util');
var utils = require(__dirname + '/utils'); var utils = require(__dirname + '/utils');
var Writer = require('buffer-writer'); var Writer = require('buffer-writer');
var TEXT_MODE = 0;
var BINARY_MODE = 1;
var Connection = function(config) { var Connection = function(config) {
EventEmitter.call(this); EventEmitter.call(this);
config = config || {}; config = config || {};
@ -19,6 +21,7 @@ var Connection = function(config) {
this.writer = new Writer(); this.writer = new Writer();
this.ssl = config.ssl || false; this.ssl = config.ssl || false;
this._ending = false; this._ending = false;
this._mode = TEXT_MODE;
}; };
util.inherits(Connection, EventEmitter); util.inherits(Connection, EventEmitter);
@ -488,8 +491,15 @@ Connection.prototype.parseField = function() {
dataTypeID: this.parseInt32(), dataTypeID: this.parseInt32(),
dataTypeSize: this.parseInt16(), dataTypeSize: this.parseInt16(),
dataTypeModifier: this.parseInt32(), dataTypeModifier: this.parseInt32(),
format: this.parseInt16() === 0 ? 'text' : 'binary' format: undefined
}; };
if(this.parseInt16() === TEXT_MODE) {
this._mode = TEXT_MODE;
field.format = 'text';
} else {
this._mode = BINARY_MODE;
field.format = 'binary';
}
return field; return field;
}; };
@ -500,7 +510,11 @@ Connection.prototype.parseD = function(msg) {
var length = this.parseInt32(); var length = this.parseInt32();
var value = null; var value = null;
if(length !== -1) { if(length !== -1) {
value = this.readBytes(length); if(this._mode === TEXT_MODE) {
value = this.readString(length);
} else {
value = this.readBytes(length);
}
} }
fields.push(value); fields.push(value);
} }
@ -569,7 +583,7 @@ Connection.prototype.parseGH = function (msg) {
}; };
Connection.prototype.readChar = function() { Connection.prototype.readChar = function() {
return Buffer([this.buffer[this.offset++]]).toString(this.encoding); return this.readString(1);
}; };
Connection.prototype.parseInt32 = function() { Connection.prototype.parseInt32 = function() {
@ -594,7 +608,7 @@ Connection.prototype.readBytes = function(length) {
Connection.prototype.parseCString = function() { Connection.prototype.parseCString = function() {
var start = this.offset; var start = this.offset;
while(this.buffer[this.offset++]) { } while(this.buffer[this.offset++] !== 0) { }
return this.buffer.toString(this.encoding, start, this.offset - 1); return this.buffer.toString(this.encoding, start, this.offset - 1);
}; };