diff --git a/lib/bcoin/mempool.js b/lib/bcoin/mempool.js index f550c91f..8aa25dce 100644 --- a/lib/bcoin/mempool.js +++ b/lib/bcoin/mempool.js @@ -1472,6 +1472,11 @@ Mempool.prototype._removeSpenders = function _removeSpenders(entry) { /** * Calculate the memory usage of a transaction. + * Note that this only calculates the JS heap + * size. Sizes of buffers are ignored (the v8 + * heap is what we care most about). All numbers + * are based on the output of v8 heap snapshots + * of TX objects. * @param {TX} tx * @returns {Number} Usage in bytes. */ @@ -1480,38 +1485,60 @@ Mempool.prototype.memUsage = function memUsage(tx) { var mem = 0; var i, j, input, output; - mem += 8; - mem += 8; - mem += 8; + mem += 264; // tx + mem += 80; // _hash + mem += 80; // _raw + + mem += 32; // input array for (i = 0; i < tx.inputs.length; i++) { input = tx.inputs[i]; + + mem += 144; // input + if (input.coin) { - mem += 93; - mem += input.coin.script.getSize() * 2; + mem += 144; // coin + mem += 88; // coin hash + mem += 40; // script + mem += 80; // script raw buffer + mem += 32; // script code array + mem += input.coin.script.code.length * 40; // opcodes + mem += 80; // add a code buffer (assume p2pkh) } - mem += 76; - mem += input.script.getSize() * 2; - for (j = 0; j < input.witness.items.length; j++) - mem += input.witness.items[j].length; + + mem += 104; // prevout + mem += 88; // prevout hash + + mem += 40; // script + mem += 80; // script raw buffer + mem += 32; // script code array + mem += input.script.code.length * 40; // opcodes + mem += 160; // add two code buffers (assume p2pkh) + + mem += 96; // witness + mem += 32; // witness items + mem += input.witness.items.length * 80; // witness buffers } + mem += 32; // output array + for (i = 0; i < tx.outputs.length; i++) { output = tx.outputs[i]; - mem += 8; - mem += output.script.getSize() * 2; + mem += 120; // output + mem += 40; // script + mem += 80; // script raw buffer + mem += 32; // script code array + mem += output.script.code.length * 40; // opcodes + mem += 80; // add a code buffer (assume p2pkh) } - mem += 8; + mem += 176; // mempool entry - // Mempool Entry - mem += 40; - - // Map entry - mem += 64; - - // Spent entry - mem += 64; + // Note: normally we could add 176 (88 * 2) + // bytes for the spent and tx hash table + // entries, but v8 seems to store these + // strings off the heap, so it shouldn't + // be too much of a problem. return mem; };