Simplified Adler32() until deopt traces investigated - now fast enougth

This commit is contained in:
Vitaly Puzrin 2014-02-14 23:58:16 +04:00
parent c19d9ce8aa
commit aa4ddcac50

View file

@ -1,55 +1,28 @@
'use strict';
function adler32(adler, buf, len, start)
{
var BASE = 65521; /* largest prime smaller than 65536 */
var NMAX = 5552;
// TODO: investigate why --trace_deopt shows something
// (though speed doesn't seems to reduce)
//
function adler32(adler, buf, len, pos) {
var s1 = adler & 0xffff
, s2 = (adler >>> 16) & 0xffff
, n = 0;
var i = start;
/* split Adler-32 into component sums */
var sum2 = (adler >> 16) & 0xffff;
adler &= 0xffff;
while (len !== 0) {
n = len > 5552 ? 5552 : len;
len -= n;
/* in case user likes doing a byte at a time, keep it fast */
if (len === 1) {
adler += buf[0];
if (adler >= BASE) {
adler -= BASE;
}
sum2 += adler;
if (sum2 >= BASE) {
sum2 -= BASE;
}
return (adler | (sum2 << 16)) >>> 0;
do {
s1 += buf[pos++];
s2 += s1;
} while (--n);
s1 %= 65521;
s2 %= 65521;
}
/* in case short lengths are provided, keep it somewhat fast */
if (len < 16) {
for(i=0;i<len;i++) {
adler += buf[i];
sum2 += adler;
}
if (adler >= BASE) {
adler -= BASE;
}
sum2 %= BASE; /* only added so many BASE's */
return (adler | (sum2 << 16)) >>> 0;
}
while (len>0) {
var cursor = len > NMAX ? NMAX : len;
len -= cursor;
for(var j=0;j<cursor;j++) {
adler += buf[i++];
sum2 += adler;
}
adler %= BASE;
sum2 %= BASE;
}
/* return recombined sums */
return (adler | (sum2 << 16)) >>> 0;
return (s1 | (s2 << 16));
}
module.exports = adler32;