From f13b0ba35cecf2a764fea04f19bd89774f2b1da2 Mon Sep 17 00:00:00 2001 From: brianc Date: Sat, 1 Jan 2011 12:40:45 -0600 Subject: [PATCH] using same writer reference to improve performance --- lib/connection.js | 25 +++++++++++++------------ 1 file changed, 13 insertions(+), 12 deletions(-) diff --git a/lib/connection.js b/lib/connection.js index 46be8636..b2adbd87 100644 --- a/lib/connection.js +++ b/lib/connection.js @@ -16,6 +16,7 @@ var Connection = function(config) { this.offset = null; this.encoding = 'utf8'; this.parsedStatements = {}; + this.writer = new Writer(); }; sys.inherits(Connection, EventEmitter); @@ -53,14 +54,14 @@ p.connect = function(port, host) { }; p.startup = function(config) { - var bodyBuffer = new Writer() + var bodyBuffer = this.writer .addInt16(3) .addInt16(0) .addCString('user') .addCString(config.user) .addCString('database') .addCString(config.database) - .addCString('').join(); + .addCString('').flush(); //this message is sent without a code var length = bodyBuffer.length + 4; @@ -74,7 +75,7 @@ p.startup = function(config) { p.password = function(password) { //0x70 = 'p' - this.send(0x70, new Writer().addCString(password).join()); + this.send(0x70, this.writer.addCString(password).flush()); }; p.send = function(code, bodyBuffer) { @@ -97,7 +98,7 @@ p.end = function() { p.query = function(text) { //0x51 = Q - this.send(0x51, new Writer().addCString(text).join()); + this.send(0x51, this.writer.addCString(text).flush()); }; p.parse = function(query) { @@ -111,7 +112,7 @@ p.parse = function(query) { //normalize null type array query.types = query.types || []; var len = query.types.length; - var buffer = new Writer() + var buffer = this.writer .addCString(query.name) //name of query .addCString(query.text) //actual query text .addInt16(len); @@ -120,7 +121,7 @@ p.parse = function(query) { } //0x50 = 'P' - this.send(0x50, buffer.join()); + this.send(0x50, buffer.flush()); return this; }; @@ -132,7 +133,7 @@ p.bind = function(config) { config.statement = config.statement || ''; var values = config.values || []; var len = values.length; - var buffer = new Writer() + var buffer = this.writer .addCString(config.portal) .addCString(config.statement) .addInt16(0) //always use default text format @@ -144,22 +145,22 @@ p.bind = function(config) { } else { val = val.toString(); buffer.addInt32(Buffer.byteLength(val)); - buffer.add(Buffer(val,this.encoding)); + buffer.addString(val); } } buffer.addInt16(0); //no format codes, use text //0x42 = 'B' - this.send(0x42, buffer.join()); + this.send(0x42, buffer.flush()); }; p.execute = function(config) { config = config || {}; config.portal = config.portal || ''; config.rows = config.rows || ''; - var buffer = new Writer() + var buffer = this.writer .addCString(config.portal) .addInt32(config.rows) - .join(); + .flush(); //0x45 = 'E' this.send(0x45, buffer); @@ -181,7 +182,7 @@ p.end = function() { }; p.describe = function(msg) { - this.send(0x44, new Writer().addCString(msg.type + (msg.name || '')).join()); + this.send(0x44, this.writer.addCString(msg.type + (msg.name || '')).flush()); }; //parsing methods