From f4c1a86ed9c72bf359b6a688f1fb2a0e5a483197 Mon Sep 17 00:00:00 2001 From: Vitaly Puzrin Date: Fri, 14 Feb 2014 02:51:38 +0400 Subject: [PATCH] Fixed spped degradation: set arrays sizes & data types --- lib/zlib/deflate.js | 14 ++++++++++---- lib/zlib/utils.js | 15 ++++++++++++--- 2 files changed, 22 insertions(+), 7 deletions(-) diff --git a/lib/zlib/deflate.js b/lib/zlib/deflate.js index 27933cd..7691a91 100644 --- a/lib/zlib/deflate.js +++ b/lib/zlib/deflate.js @@ -171,7 +171,7 @@ function fill_window(s) { zlib, so we don't care about this pathological case.) */ n = s.hash_size; - p = s.head[n]; + p = n; do { m = s.head[--p]; s.head[p] = m >= wsize ? m - wsize : 0; @@ -179,7 +179,7 @@ function fill_window(s) { n = wsize; if (FASTEST) { - p = s.prev[n]; + p = n; do { m = s.head[--p]; s.head[p] = m >= wsize ? m - wsize : 0; @@ -499,13 +499,13 @@ function DeflateState() { * is directly used as sliding window. */ - this.prev = Z_NULL; + this.prev = null; /* Link to older string with same hash index. To limit the size of this * array to 64K, this link is maintained only for the last 32K strings. * An index in this array is thus a window index modulo 32K. */ - this.head = Z_NULL; + this.head = null; /* Heads of the hash chains or NIL. */ this.ins_h = 0; @@ -659,6 +659,12 @@ function deflateInit2(strm, level, method, windowBits, memLevel, strategy) { s.strategy = strategy; s.method = method; + // precreate & prefill head/prev arrays to optimize v8 types + s.head = new Array(s.hash_size); + s.prev = new Array(s.w_size); + utils.fill(s.head, 0); + utils.fill(s.prev, 0); + return deflateReset(strm); } diff --git a/lib/zlib/utils.js b/lib/zlib/utils.js index a2f996c..f1546f5 100644 --- a/lib/zlib/utils.js +++ b/lib/zlib/utils.js @@ -1,6 +1,6 @@ 'use strict'; -exports.assign = function(obj /*from1, from2, from3, ...*/) { +exports.assign = function (obj /*from1, from2, from3, ...*/) { var sources = Array.prototype.slice.call(arguments, 1); while (sources.length) { var source = sources.shift(); @@ -21,7 +21,7 @@ exports.assign = function(obj /*from1, from2, from3, ...*/) { }; -exports.arraySet = function(dest, src, src_offs, len, dest_offs) { +exports.arraySet = function (dest, src, src_offs, len, dest_offs) { // Suppose, that with typed array support destination is // always typed - don't check it @@ -45,7 +45,7 @@ exports.arraySet = function(dest, src, src_offs, len, dest_offs) { }; -exports.arrayCreate = function(length) { +exports.arrayCreate = function (length) { if ((typeof Uint8Array !== 'undefined')) { return new Uint8Array(length); @@ -54,3 +54,12 @@ exports.arrayCreate = function(length) { // Fallback to ordinary array return new Array(length); }; + + +exports.fill = function (buf, val) { + var len = buf.length; + + if (!len) { return;} + + while (--len) { buf[len] = val} +} \ No newline at end of file