From a3dee0695db8de07eec4ca6ffad6f46bcb82b499 Mon Sep 17 00:00:00 2001 From: Braydon Fuller Date: Fri, 26 Jun 2015 14:02:26 -0400 Subject: [PATCH] Improved performance of bufferReader.readUInt64LEBN() --- benchmark/serialization.js | 39 ++++++++++++++++++++++++++++++++++++ lib/encoding/bufferreader.js | 7 ++----- 2 files changed, 41 insertions(+), 5 deletions(-) diff --git a/benchmark/serialization.js b/benchmark/serialization.js index 7b45cc7..d0024bd 100644 --- a/benchmark/serialization.js +++ b/benchmark/serialization.js @@ -14,6 +14,45 @@ console.log('Benchmarking Block/Transaction Serialization'); console.log('---------------------------------------'); async.series([ + function(next) { + + var buffers = []; + console.log('Generating Random Test Data...'); + for (var i = 0; i < 100; i++) { + var br = new bitcore.encoding.BufferWriter(); + var num = Math.round(Math.random() * 10000000000000); + br.writeUInt64LEBN(new bitcore.crypto.BN(num)); + buffers.push(br.toBuffer()); + } + + var c = 0; + var bn; + + function readUInt64LEBN() { + if (c >= buffers.length) { + c = 0; + } + var buf = buffers[c]; + var br = new bitcore.encoding.BufferReader(buf); + bn = br.readUInt64LEBN(); + c++; + } + + console.log('Starting benchmark...'); + + var suite = new benchmark.Suite(); + suite.add('bufferReader.readUInt64LEBN()', readUInt64LEBN, {maxTime: maxTime}); + suite + .on('cycle', function(event) { + console.log(String(event.target)); + }) + .on('complete', function() { + console.log('Done'); + console.log('----------------------------------------------------------------------'); + next(); + }) + .run(); + }, function(next) { var block1; diff --git a/lib/encoding/bufferreader.js b/lib/encoding/bufferreader.js index bb881fb..19c6f81 100644 --- a/lib/encoding/bufferreader.js +++ b/lib/encoding/bufferreader.js @@ -91,11 +91,8 @@ BufferReader.prototype.readUInt64BEBN = function() { }; BufferReader.prototype.readUInt64LEBN = function() { - var buf = this.buf.slice(this.pos, this.pos + 8); - var reversebuf = BufferReader({ - buf: buf - }).readReverse(); - var bn = BN.fromBuffer(reversebuf); + var data = this.buf.slice(this.pos, this.pos + 8).toJSON().data; + var bn = new BN(data, 10, 'le'); this.pos = this.pos + 8; return bn; };