writer: use 100kb pool for sighashing.

This commit is contained in:
Christopher Jeffrey 2017-09-03 00:05:47 -07:00
parent e0eb1cdbe3
commit a543648310
No known key found for this signature in database
GPG Key ID: 8962AB9DE6666BBD
5 changed files with 33 additions and 22 deletions

View File

@ -8,7 +8,7 @@
const assert = require('assert');
const util = require('../utils/util');
const StaticWriter = require('../utils/writer');
const StaticWriter = require('../utils/staticwriter');
const BufferReader = require('../utils/reader');
const encoding = require('../utils/encoding');

View File

@ -451,7 +451,7 @@ TX.prototype.signatureHashV0 = function signatureHashV0(index, prev, type) {
// Calculate buffer size.
const size = this.hashSize(index, prev, type);
const bw = new StaticWriter(size);
const bw = StaticWriter.pool(size);
bw.writeU32(this.version);
@ -615,7 +615,7 @@ TX.prototype.signatureHashV1 = function signatureHashV1(index, prev, value, type
if (this._hashPrevouts) {
prevouts = this._hashPrevouts;
} else {
const bw = new StaticWriter(this.inputs.length * 36);
const bw = StaticWriter.pool(this.inputs.length * 36);
for (const input of this.inputs)
input.prevout.toWriter(bw);
@ -633,7 +633,7 @@ TX.prototype.signatureHashV1 = function signatureHashV1(index, prev, value, type
if (this._hashSequence) {
sequences = this._hashSequence;
} else {
const bw = new StaticWriter(this.inputs.length * 4);
const bw = StaticWriter.pool(this.inputs.length * 4);
for (const input of this.inputs)
bw.writeU32(input.sequence);
@ -655,7 +655,7 @@ TX.prototype.signatureHashV1 = function signatureHashV1(index, prev, value, type
for (const output of this.outputs)
size += output.getSize();
const bw = new StaticWriter(size);
const bw = StaticWriter.pool(size);
for (const output of this.outputs)
output.toWriter(bw);
@ -673,7 +673,7 @@ TX.prototype.signatureHashV1 = function signatureHashV1(index, prev, value, type
}
const size = 156 + prev.getVarSize();
const bw = new StaticWriter(size);
const bw = StaticWriter.pool(size);
bw.writeU32(this.version);
bw.writeBytes(prevouts);

View File

@ -10,7 +10,8 @@
const assert = require('assert');
const encoding = require('./encoding');
const digest = require('../crypto/digest');
const EMPTY_BUFFER = Buffer.alloc(0);
const EMPTY = Buffer.alloc(0);
/**
* An object that allows reading of buffers in a sane manner.
@ -145,7 +146,7 @@ BufferReader.prototype.endData = function endData(zeroCopy) {
*/
BufferReader.prototype.destroy = function destroy() {
this.data = EMPTY_BUFFER;
this.data = EMPTY;
this.offset = 0;
this.stack.length = 0;
};

View File

@ -9,7 +9,9 @@
const assert = require('assert');
const encoding = require('./encoding');
const digest = require('../crypto/digest');
const EMPTY_BUFFER = Buffer.alloc(0);
const POOL = Buffer.allocUnsafeSlow(100 * 1024);
const EMPTY = Buffer.alloc(0);
/**
* Statically allocated buffer writer.
@ -22,24 +24,34 @@ function StaticWriter(size) {
if (!(this instanceof StaticWriter))
return new StaticWriter(size);
this.data = Buffer.allocUnsafe(size);
this.data = size ? Buffer.allocUnsafe(size) : EMPTY;
this.offset = 0;
}
/**
* Allocate writer from preallocated 100kb pool.
* @param {Number} size
* @returns {StaticWriter}
*/
StaticWriter.pool = function pool(size) {
if (size <= POOL.length) {
const bw = new StaticWriter(0);
bw.data = POOL.slice(0, size);
return bw;
}
return new StaticWriter(size);
};
/**
* Allocate and render the final buffer.
* @param {Boolean?} keep - Do not destroy the writer.
* @returns {Buffer} Rendered buffer.
*/
StaticWriter.prototype.render = function render(keep) {
StaticWriter.prototype.render = function render() {
const data = this.data;
assert(this.offset === data.length);
if (!keep)
this.destroy();
this.destroy();
return data;
};
@ -66,7 +78,7 @@ StaticWriter.prototype.seek = function seek(offset) {
*/
StaticWriter.prototype.destroy = function destroy() {
this.data = EMPTY_BUFFER;
this.data = EMPTY;
this.offset = 0;
};

View File

@ -71,11 +71,10 @@ function BufferWriter() {
/**
* Allocate and render the final buffer.
* @param {Boolean?} keep - Do not destroy the writer.
* @returns {Buffer} Rendered buffer.
*/
BufferWriter.prototype.render = function render(keep) {
BufferWriter.prototype.render = function render() {
const data = Buffer.allocUnsafe(this.offset);
let off = 0;
@ -183,8 +182,7 @@ BufferWriter.prototype.render = function render(keep) {
assert(off === data.length);
if (!keep)
this.destroy();
this.destroy();
return data;
};