mempool: accurate memory usage.

This commit is contained in:
Christopher Jeffrey 2016-08-16 20:14:19 -07:00
parent bd9a41dcbe
commit 72188e42c2
No known key found for this signature in database
GPG Key ID: 8962AB9DE6666BBD

View File

@ -1472,6 +1472,11 @@ Mempool.prototype._removeSpenders = function _removeSpenders(entry) {
/** /**
* Calculate the memory usage of a transaction. * 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 * @param {TX} tx
* @returns {Number} Usage in bytes. * @returns {Number} Usage in bytes.
*/ */
@ -1480,38 +1485,60 @@ Mempool.prototype.memUsage = function memUsage(tx) {
var mem = 0; var mem = 0;
var i, j, input, output; var i, j, input, output;
mem += 8; mem += 264; // tx
mem += 8; mem += 80; // _hash
mem += 8; mem += 80; // _raw
mem += 32; // input array
for (i = 0; i < tx.inputs.length; i++) { for (i = 0; i < tx.inputs.length; i++) {
input = tx.inputs[i]; input = tx.inputs[i];
mem += 144; // input
if (input.coin) { if (input.coin) {
mem += 93; mem += 144; // coin
mem += input.coin.script.getSize() * 2; 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; mem += 104; // prevout
for (j = 0; j < input.witness.items.length; j++) mem += 88; // prevout hash
mem += input.witness.items[j].length;
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++) { for (i = 0; i < tx.outputs.length; i++) {
output = tx.outputs[i]; output = tx.outputs[i];
mem += 8; mem += 120; // output
mem += output.script.getSize() * 2; 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 // Note: normally we could add 176 (88 * 2)
mem += 40; // bytes for the spent and tx hash table
// entries, but v8 seems to store these
// Map entry // strings off the heap, so it shouldn't
mem += 64; // be too much of a problem.
// Spent entry
mem += 64;
return mem; return mem;
}; };