Skip to content

Commit

Permalink
Make Packed.estimateLength more accurate.
Browse files Browse the repository at this point in the history
also fix tools/compare-size.js that was broken for quite a while.
  • Loading branch information
lifthrasiir committed Aug 22, 2021
1 parent b22c32c commit f7bf1a4
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 2 deletions.
8 changes: 7 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -1093,7 +1093,13 @@ class Packed {
}

estimateLength() {
return this.firstLineLengthInBytes + estimateDeflatedSize(this.secondLine);
// the first line is mostly 6-bit code, but there are additional characters
// so one of the 6-bit code literal has to be encoded in 7 bits.
// if the original data is N bytes the first line will contain about 4/3 N codes,
// 1/64 of which will have to use one additional bit.
// therefore the estimated overhead is 4/3 N * 1/64 [bits] = N/384 [bytes].
// the additional 35 bytes are experimentally derived from the Huffman tree coding.
return 35 + Math.ceil(this.firstLineLengthInBytes * (1 + 1/384)) + estimateDeflatedSize(this.secondLine);
}
}

2 changes: 1 addition & 1 deletion tools/compare-size.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ function twoPartDeflate(buf1, buf2, options) {
}

function analyze(buf) {
console.log([...analyzeDeflate(1, buf)].map(i => i[0] / 8));
console.log([...analyzeDeflate('zlib', buf)].map(i => [i[0] / 8, i.slice(1).reduce((x,[y]) => x + y, 0) / 8]));
return buf.length;
}

Expand Down

0 comments on commit f7bf1a4

Please sign in to comment.