[Pkg-javascript-commits] [node-sha.js] 108/237: Several Memory Related Performance Improvements

Bastien Roucariès rouca at moszumanska.debian.org
Fri May 5 09:03:44 UTC 2017


This is an automated email from the git hooks/post-receive script.

rouca pushed a commit to branch master
in repository node-sha.js.

commit 9b9badccae5585d0a1f563ce171635404f97108d
Author: Dominic Tarr <dominic.tarr at gmail.com>
Date:   Fri Jan 17 00:32:50 2014 +0700

    Several Memory Related Performance Improvements
    
    1) reuse working memory. Each hash algorithm has a buffer of working
    memory that they process data in. Since javascript is single threaded
    and this operation is sync, we only need one buffer for the class, not per thread.
    This saves a lot of time allocating memory.
    
    2) add Hash.init() method to reinitialize a hash algorithm.
    This makes it possible to reuse an instance again, so there is less objects
    created and less GC. This makes a difference in iterated hashing (i.e. pbkdf2)
    Although, this doesn't make such a big difference after 1), it's still measurable.
    
    3) pooling hash instances. Save created hash instances in an array, and when the user
    is finished, keep it for next time. This makes a difference to the crypto-bench/bench-hash
    benchmark, which was going very weirdly... doing lots of large hashes after each other
    was slow, but just doing the largest hash was 3x faster??? now performance is same
    no matter how many large hashes you do.
    I'm still seeing a little bit of wobble there. maybe it's related to the encoding stage?
---
 hash.js   |  7 ++++++-
 sha1.js   | 25 +++++++++++++++++--------
 sha256.js | 27 +++++++++++++++++++++------
 3 files changed, 44 insertions(+), 15 deletions(-)

diff --git a/hash.js b/hash.js
index a022df2..29dc76d 100644
--- a/hash.js
+++ b/hash.js
@@ -10,7 +10,12 @@ module.exports = function (Buffer) {
     this._finalSize = finalSize
     this._blockSize = blockSize
     this._len = 0
-    this._l = 0
+    this._s = 0
+  }
+
+  Hash.prototype.init = function () {
+    this._s = 0
+    this._len = 0
   }
 
   function lengthOf(data, enc) {
diff --git a/sha1.js b/sha1.js
index 10b4fdb..bde58ae 100644
--- a/sha1.js
+++ b/sha1.js
@@ -10,8 +10,6 @@ module.exports = function (Buffer, Hash) {
 
   var inherits = require('util').inherits
 
-  var hexpp = require('./hexpp')
-
   inherits(Sha1, Hash)
 
   var A = 0|0
@@ -23,23 +21,34 @@ module.exports = function (Buffer, Hash) {
   var BE = false
   var LE = true
 
+  var W = new Int32Array(80)
+
+  var POOL = []
+
   function Sha1 () {
-    if(!(this instanceof Sha1)) return new Sha1()
+    if(POOL.length)
+      return POOL.pop().init()
 
-    this._w = new Int32Array(80)
+    if(!(this instanceof Sha1)) return new Sha1()
+    this._w = W
     Hash.call(this, 16*4, 14*4)
   
-    this._h = new Buffer(20)
+    this._h = null
+    this.init()
+  }
 
+  Sha1.prototype.init = function () {
     this._a = 0x67452301
     this._b = 0xefcdab89
     this._c = 0x98badcfe
     this._d = 0x10325476
     this._e = 0xc3d2e1f0
 
-    this._len = 0
+    Hash.prototype.init.call(this)
+    return this
   }
 
+  Sha1.prototype._POOL = POOL
 
   // assume that array is a Uint32Array with length=16,
   // and that if it is the last block, it already has the length and the 1 bit appended.
@@ -95,8 +104,8 @@ module.exports = function (Buffer, Hash) {
   }
 
   Sha1.prototype._hash = function () {
-    var H = this._h
-
+    if(POOL.length < 100) POOL.push(this)
+    var H = new Buffer(20)
     //console.log(this._a|0, this._b|0, this._c|0, this._d|0, this._e|0)
     H.writeInt32BE(this._a|0, A)
     H.writeInt32BE(this._b|0, B)
diff --git a/sha256.js b/sha256.js
index 2faf574..0902dcd 100644
--- a/sha256.js
+++ b/sha256.js
@@ -35,9 +35,22 @@ module.exports = function (Buffer, Hash) {
     ]
 
   inherits(Sha256, Hash)
-
+  var W = new Array(64)
+  var POOL = []
   function Sha256() {
-    this._data = new Buffer(32)
+    if(POOL.length) {
+      //return POOL.shift().init()
+    }
+    //this._data = new Buffer(32)
+
+    this.init()
+
+    this._w = W //new Array(64)
+
+    Hash.call(this, 16*4, 14*4)
+  };
+
+  Sha256.prototype.init = function () {
 
     this._a = 0x6a09e667|0
     this._b = 0xbb67ae85|0
@@ -48,10 +61,10 @@ module.exports = function (Buffer, Hash) {
     this._g = 0x1f83d9ab|0
     this._h = 0x5be0cd19|0
 
-    this._w = new Array(64)
+    this._len = this._s = 0
 
-    Hash.call(this, 16*4, 14*4)
-  };
+    return this
+  }
 
   var safe_add = function(x, y) {
     var lsw = (x & 0xFFFF) + (y & 0xFFFF);
@@ -129,8 +142,10 @@ module.exports = function (Buffer, Hash) {
   };
 
   Sha256.prototype._hash = function () {
+    if(POOL.length < 10)
+      POOL.push(this)
 
-    var H = this._data
+    var H = new Buffer(32)
 
     H.writeInt32BE(this._a,  0)
     H.writeInt32BE(this._b,  4)

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-javascript/node-sha.js.git



More information about the Pkg-javascript-commits mailing list