Store tables data in compact form

This commit is contained in:
Vitaly Puzrin 2014-03-12 22:50:55 +04:00
parent 63ffa3baae
commit f09d27bc1e
3 changed files with 43 additions and 26 deletions

View file

@ -115,27 +115,23 @@ Code.prototype.clone = function() {
}; };
function CodeTable(length) { function CodeTable(length) {
this.op = length ? utils.arrayCreate(length) : null; // Packed data: [ bits, op, value], 1byte + 1byte +2 bytes
this.val = length ? utils.array16Create(length): null; this.data = utils.array32Create(length);
this.bits = length ? utils.arrayCreate(length) : null;
} }
CodeTable.prototype.fill = function(idx, code) { CodeTable.prototype.fill = function(idx, code) {
code.bits = this.bits[idx]; var packed = this.data[idx];
code.op = this.op[idx]; code.bits = packed >>> 24;
code.val = this.val[idx]; code.op = (packed >>> 16) & 0xff;
code.val = packed & 0xffff;
}; };
CodeTable.prototype.set = function(idx, code) { CodeTable.prototype.set = function(idx, code) {
this.bits[idx] = code.bits; this.data[idx] = (code.bits << 24) | (code.op << 16) | code.val;
this.op[idx] = code.op;
this.val[idx] = code.val;
}; };
CodeTable.prototype.copy = function(table) { CodeTable.prototype.copy = function(table) {
utils.arraySet(this.bits,table.bits,0,table.bits.length,0); utils.arraySet(this.data,table.data,0,table.data.length,0);
utils.arraySet(this.op,table.op,0,table.op.length,0);
utils.arraySet(this.val,table.val,0,table.val.length,0);
}; };
function InflateState() { function InflateState() {

View file

@ -116,13 +116,17 @@ module.exports = function inflate_table(opts)
root = max; root = max;
} }
if (max === 0) { /* no symbols to code at all */ if (max === 0) { /* no symbols to code at all */
table.op[opts.table_index] = 64; //here.op = (var char)64; /* invalid code marker */ //table.op[opts.table_index] = 64; //here.op = (var char)64; /* invalid code marker */
table.bits[opts.table_index] = 1; //here.bits = (var char)1; //table.bits[opts.table_index] = 1; //here.bits = (var char)1;
table.val[opts.table_index++] = 0; //here.val = (var short)0; //table.val[opts.table_index++] = 0; //here.val = (var short)0;
table.data[opts.table_index++] = (1 << 24) | (64 << 16) | 0;
//table.op[opts.table_index] = 64;
//table.bits[opts.table_index] = 1;
//table.val[opts.table_index++] = 0;
table.data[opts.table_index++] = (1 << 24) | (64 << 16) | 0;
table.op[opts.table_index] = 64;
table.bits[opts.table_index] = 1;
table.val[opts.table_index++] = 0;
opts.bits = 1; opts.bits = 1;
return 0; /* no symbols, but wait for decoding to report error */ return 0; /* no symbols, but wait for decoding to report error */
} }
@ -302,9 +306,10 @@ module.exports = function inflate_table(opts)
/* point entry in root table to sub-table */ /* point entry in root table to sub-table */
low = huff & mask; low = huff & mask;
table.op[low] = curr; /*table.op[low] = curr;
table.bits[low] = root; table.bits[low] = root;
table.val[low] = next - opts.table_index; table.val[low] = next - opts.table_index;*/
table.data[low] = (root << 24) | (curr << 16) | (next - opts.table_index);
} }
} }
@ -312,9 +317,10 @@ module.exports = function inflate_table(opts)
at most one remaining entry, since if the code is incomplete, the at most one remaining entry, since if the code is incomplete, the
maximum code length that was allowed to get this far is one bit) */ maximum code length that was allowed to get this far is one bit) */
if (huff !== 0) { if (huff !== 0) {
table.op[next + huff] = 64; /* invalid code marker */ //table.op[next + huff] = 64; /* invalid code marker */
table.bits[next + huff] = len - drop; //table.bits[next + huff] = len - drop;
table.val[next + huff] = 0; //table.val[next + huff] = 0;
table.data[next + huff] = ((len - drop) << 24) | (64 << 16) | 0;
} }
/* set return parameters */ /* set return parameters */

View file

@ -62,22 +62,37 @@ exports.arraySet = function (dest, src, src_offs, len, dest_offs) {
exports.arrayCreate = function (length) { exports.arrayCreate = function (length) {
if (typedOk()) { if (typedOk()) {
return new Uint8Array(length); return new Uint8Array(length);
} }
// Fallback to ordinary array // Fallback to ordinary array
return new Array(length); return new Array(length);
}; };
exports.array16Create = function (length) { exports.array16Create = function (length) {
if (typedOk()) { if (typedOk()) {
return new Uint16Array(length); return new Uint16Array(length);
} }
// Fallback to ordinary array
return new Array(length);
};
exports.array16Create = function (length) {
if (typedOk()) {
return new Uint16Array(length);
}
// Fallback to ordinary array
return new Array(length);
};
// (!) use signed ints for 32 bits, to avoid boxing
exports.array32Create = function (length) {
if (typedOk()) {
return new Int32Array(length);
}
// Fallback to ordinary array // Fallback to ordinary array
return new Array(length); return new Array(length);
}; };