mirror of
https://github.com/brianc/node-postgres.git
synced 2025-12-08 20:16:25 +00:00
swap writer for elasticbuffer for performance
This commit is contained in:
parent
cefe3ab019
commit
0458e8981f
114
lib/writer.js
114
lib/writer.js
@ -1,66 +1,74 @@
|
||||
var Writer = function() {
|
||||
this.buffers = [];
|
||||
var ElasticBuffer = function(size) {
|
||||
this.size = size || 1024;
|
||||
this.buffer = new Buffer(this.size);
|
||||
this.offset = 0;
|
||||
};
|
||||
|
||||
var p = Writer.prototype;
|
||||
var p = ElasticBuffer.prototype;
|
||||
|
||||
p.add = function(buffer) {
|
||||
this.buffers.push(buffer);
|
||||
return this;
|
||||
};
|
||||
p._remaining = function() {
|
||||
return this.buffer.length - this.offset;
|
||||
}
|
||||
|
||||
p.addInt16 = function(val, front) {
|
||||
return this.add(Buffer([
|
||||
(val >>> 8),
|
||||
(val >>> 0)
|
||||
]));
|
||||
};
|
||||
p._resize = function() {
|
||||
var oldBuffer = this.buffer;
|
||||
this.buffer = Buffer(oldBuffer.length + this.size);
|
||||
oldBuffer.copy(this.buffer);
|
||||
}
|
||||
|
||||
p.getByteLength = function(initial) {
|
||||
var totalBufferLength = 0;
|
||||
var buffers = this.buffers;
|
||||
for(var i = 0, len = buffers.length; i < len; i++) {
|
||||
totalBufferLength += buffers[i].length;
|
||||
//resizes internal buffer if not enough size left
|
||||
p._ensure = function(size) {
|
||||
if(this._remaining() < size) {
|
||||
this._resize()
|
||||
}
|
||||
return totalBufferLength;
|
||||
};
|
||||
}
|
||||
|
||||
p.addInt32 = function(val, first) {
|
||||
return this.add(Buffer([
|
||||
(val >>> 24 & 0xFF),
|
||||
(val >>> 16 & 0xFF),
|
||||
(val >>> 8 & 0xFF),
|
||||
(val >>> 0 & 0xFF)
|
||||
]));
|
||||
};
|
||||
p.addInt32 = function(num) {
|
||||
this._ensure(4)
|
||||
this.buffer[this.offset++] = (num >>> 24 & 0xFF)
|
||||
this.buffer[this.offset++] = (num >>> 16 & 0xFF)
|
||||
this.buffer[this.offset++] = (num >>> 8 & 0xFF)
|
||||
this.buffer[this.offset++] = (num >>> 0 & 0xFF)
|
||||
return this;
|
||||
}
|
||||
|
||||
p.addCString = function(val) {
|
||||
var len = Buffer.byteLength(val);
|
||||
var buffer = new Buffer(len+1);
|
||||
buffer.write(val);
|
||||
buffer[len] = 0;
|
||||
return this.add(buffer);
|
||||
};
|
||||
p.addInt16 = function(num) {
|
||||
this._ensure(2)
|
||||
this.buffer[this.offset++] = (num >>> 8 & 0xFF)
|
||||
this.buffer[this.offset++] = (num >>> 0 & 0xFF)
|
||||
return this;
|
||||
}
|
||||
|
||||
p.addChar = function(char, first) {
|
||||
return this.add(Buffer(char,'utf8'), first);
|
||||
};
|
||||
p.addCString = function(string) {
|
||||
var string = string || "";
|
||||
var len = Buffer.byteLength(string) + 1;
|
||||
this._ensure(len);
|
||||
this.buffer.write(string, this.offset);
|
||||
this.offset += len;
|
||||
this.buffer[this.offset] = 0; //add null terminator
|
||||
return this;
|
||||
}
|
||||
|
||||
p.addChar = function(char) {
|
||||
this._ensure(1);
|
||||
this.buffer.write(char, this.offset);
|
||||
this.offset++;
|
||||
return this;
|
||||
}
|
||||
|
||||
p.join = function() {
|
||||
var length = this.buffers.length;
|
||||
if(length===1) {
|
||||
return this.buffers[0]
|
||||
}
|
||||
var result = Buffer(this.getByteLength());
|
||||
var index = 0;
|
||||
var buffers = this.buffers;
|
||||
for(var i = 0; i < length; i ++) {
|
||||
var buffer = buffers[i];
|
||||
buffer.copy(result, index, 0);
|
||||
index += buffer.length;
|
||||
}
|
||||
return result;
|
||||
};
|
||||
return this.buffer.slice(0, this.offset);
|
||||
}
|
||||
|
||||
module.exports = Writer;
|
||||
p.getByteLength = function() {
|
||||
return this.offset;
|
||||
}
|
||||
|
||||
p.add = function(otherBuffer) {
|
||||
this._ensure(otherBuffer.length);
|
||||
otherBuffer.copy(this.buffer, this.offset);
|
||||
this.offset += otherBuffer.length;
|
||||
return this;
|
||||
}
|
||||
|
||||
module.exports = ElasticBuffer;
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user