Skip to content

Commit 0f926ca

Browse files
committed
refactored to increase performance
1 parent 0d2d6b5 commit 0f926ca

File tree

1 file changed

+37
-27
lines changed

1 file changed

+37
-27
lines changed

lib/connection.js

Lines changed: 37 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -53,48 +53,51 @@ p.connect = function(port, host) {
5353
};
5454

5555
p.startup = function(config) {
56-
var buffer = new Writer()
56+
var bodyBuffer = new Writer()
5757
.addInt16(3)
5858
.addInt16(0)
5959
.addCString('user')
6060
.addCString(config.user)
6161
.addCString('database')
6262
.addCString(config.database)
63-
.addCString('');
63+
.addCString('').join();
64+
//this message is sent without a code
65+
66+
var length = bodyBuffer.length + 4;
6467

65-
this.send(false, buffer.join());
68+
var buffer = new Writer()
69+
.addInt32(length)
70+
.add(bodyBuffer)
71+
.join();
72+
this.stream.write(buffer);
6673
};
6774

6875
p.password = function(password) {
69-
this.send('p', Buffer(password + '\0', this.encoding));
76+
//0x70 = 'p'
77+
this.send(0x70, Buffer(password + '\0', this.encoding));
7078
};
7179

7280
p.send = function(code, bodyBuffer) {
7381
var length = bodyBuffer.length + 4;
74-
var buffer = Buffer(length + (code ? 1 : 0));
82+
var buffer = Buffer(length + 1);
7583
var offset = 0;
76-
if(code) {
77-
buffer[offset++] = Buffer(code, this.encoding) [0];
78-
}
79-
this.writeInt32(buffer, offset, length);
80-
bodyBuffer.copy(buffer, offset+4, 0);
84+
buffer[offset++] = code;
85+
buffer[offset++] = length >>> 24 & 0xFF;
86+
buffer[offset++] = length >>> 16 & 0xFF;
87+
buffer[offset++] = length >>> 8 & 0xFF;
88+
buffer[offset++] = length >>> 0 & 0xFF;
89+
bodyBuffer.copy(buffer, offset, 0);
8190
return this.stream.write(buffer);
8291
};
8392

84-
p.writeInt32 = function(buffer, offset, value) {
85-
buffer[offset++] = value >>> 24 & 0xFF;
86-
buffer[offset++] = value >>> 16 & 0xFF;
87-
buffer[offset++] = value >>> 8 & 0xFF;
88-
buffer[offset++] = value >>> 0 & 0xFF;
89-
};
90-
93+
var termBuffer = new Buffer([0x58, 0, 0, 0, 4]);
9194
p.end = function() {
92-
var terminationBuffer = new Buffer([0x58,0,0,0,4]);
93-
var wrote = this.stream.write(terminationBuffer);
95+
var wrote = this.stream.write(termBuffer);
9496
};
9597

9698
p.query = function(text) {
97-
this.send('Q', new Buffer(text + '\0', this.encoding));
99+
//0x51 = Q
100+
this.send(0x51, new Buffer(text + '\0', this.encoding));
98101
};
99102

100103
p.parse = function(query) {
@@ -116,7 +119,8 @@ p.parse = function(query) {
116119
buffer.addInt32(query.types[i]);
117120
}
118121

119-
this.send('P', buffer.join());
122+
//0x50 = 'P'
123+
this.send(0x50, buffer.join());
120124

121125
return this;
122126
};
@@ -144,7 +148,8 @@ p.bind = function(config) {
144148
}
145149
}
146150
buffer.addInt16(0); //no format codes, use text
147-
this.send('B', buffer.join());
151+
//0x42 = 'B'
152+
this.send(0x42, buffer.join());
148153
};
149154

150155
p.execute = function(config) {
@@ -155,25 +160,30 @@ p.execute = function(config) {
155160
.addCString(config.portal)
156161
.addInt32(config.rows)
157162
.join();
158-
this.send('E', buffer);
163+
164+
//0x45 = 'E'
165+
this.send(0x45, buffer);
159166
};
160167

161168
p.flush = function() {
162-
this.send('H',Buffer(0));
169+
//0x48 = 'H'
170+
this.send(0x48,Buffer(0));
163171
}
164172

165173
p.sync = function() {
166-
this.send('S', Buffer(0));
174+
//0x53 = 'S'
175+
this.send(0x53, Buffer(0));
167176
};
168177

169178
p.end = function() {
170-
this.send('X', Buffer(0));
179+
//0x58 = 'X'
180+
this.send(0x58, Buffer(0));
171181
};
172182

173183
p.describe = function(msg) {
174184
var str = msg.type + (msg.name || "" ) + '\0';
175185
var buffer = Buffer(str, this.encoding);
176-
this.send('D', buffer);
186+
this.send(0x44, buffer);
177187
};
178188

179189
//parsing methods

0 commit comments

Comments
 (0)