From 45e41b1bb4b3919bdcfca4f15e0b0e2fac9721a3 Mon Sep 17 00:00:00 2001 From: Christopher Jeffrey Date: Sun, 3 Sep 2017 00:56:15 -0700 Subject: [PATCH] writer: alloc pool lazily. --- lib/utils/staticwriter.js | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/lib/utils/staticwriter.js b/lib/utils/staticwriter.js index 46040962..0530c096 100644 --- a/lib/utils/staticwriter.js +++ b/lib/utils/staticwriter.js @@ -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); };