writer: alloc pool lazily.

This commit is contained in:
Christopher Jeffrey 2017-09-03 00:56:15 -07:00
parent a543648310
commit 45e41b1bb4
No known key found for this signature in database
GPG Key ID: 8962AB9DE6666BBD

View File

@ -10,8 +10,10 @@ const assert = require('assert');
const encoding = require('./encoding');
const digest = require('../crypto/digest');
const POOL = Buffer.allocUnsafeSlow(100 * 1024);
const EMPTY = Buffer.alloc(0);
const POOLSIZE = 100 << 10;
let POOL = null;
/**
* Statically allocated buffer writer.
@ -35,11 +37,15 @@ function StaticWriter(size) {
*/
StaticWriter.pool = function pool(size) {
if (size <= POOL.length) {
if (size <= POOLSIZE) {
if (!POOL)
POOL = Buffer.allocUnsafeSlow(POOLSIZE);
const bw = new StaticWriter(0);
bw.data = POOL.slice(0, size);
return bw;
}
return new StaticWriter(size);
};