Skip to content

Commit

Permalink
Optimize loops for V8 Smi
Browse files Browse the repository at this point in the history
  • Loading branch information
101arrowz committed Feb 11, 2021
1 parent b40011c commit 00ae70e
Show file tree
Hide file tree
Showing 6 changed files with 92 additions and 13 deletions.
6 changes: 3 additions & 3 deletions adler32.flow.js
Expand Up @@ -36,7 +36,7 @@ function adler32_bstr(bstr/*:string*/, seed/*:?ADLER32Type*/)/*:ADLER32Type*/ {
var a = 1, b = 0, L = bstr.length, M = 0;
if(typeof seed === 'number') { a = seed & 0xFFFF; b = seed >>> 16; }
for(var i = 0; i < L;) {
M = Math.min(L-i, 3850)+i;
M = Math.min(L-i, 2654)+i;
for(;i<M;i++) {
a += bstr.charCodeAt(i)&0xFF;
b += a;
Expand All @@ -51,7 +51,7 @@ function adler32_buf(buf/*:ABuf*/, seed/*:?ADLER32Type*/)/*:ADLER32Type*/ {
var a = 1, b = 0, L = buf.length, M = 0;
if(typeof seed === 'number') { a = seed & 0xFFFF; b = (seed >>> 16) & 0xFFFF; }
for(var i = 0; i < L;) {
M = Math.min(L-i, 3850)+i;
M = Math.min(L-i, 2654)+i;
for(;i<M;i++) {
a += buf[i]&0xFF;
b += a;
Expand All @@ -67,7 +67,7 @@ function adler32_str(str/*:string*/, seed/*:?ADLER32Type*/)/*:ADLER32Type*/ {
var a = 1, b = 0, L = str.length, M = 0, c = 0, d = 0;
if(typeof seed === 'number') { a = seed & 0xFFFF; b = seed >>> 16; }
for(var i = 0; i < L;) {
M = Math.min(L-i, 3850);
M = Math.min(L-i, 2918);
while(M>0) {
c = str.charCodeAt(i++);
if(c < 0x80) { a += c; }
Expand Down
6 changes: 3 additions & 3 deletions adler32.js
Expand Up @@ -28,7 +28,7 @@ function adler32_bstr(bstr, seed) {
var a = 1, b = 0, L = bstr.length, M = 0;
if(typeof seed === 'number') { a = seed & 0xFFFF; b = seed >>> 16; }
for(var i = 0; i < L;) {
M = Math.min(L-i, 3850)+i;
M = Math.min(L-i, 2654)+i;
for(;i<M;i++) {
a += bstr.charCodeAt(i)&0xFF;
b += a;
Expand All @@ -43,7 +43,7 @@ function adler32_buf(buf, seed) {
var a = 1, b = 0, L = buf.length, M = 0;
if(typeof seed === 'number') { a = seed & 0xFFFF; b = (seed >>> 16) & 0xFFFF; }
for(var i = 0; i < L;) {
M = Math.min(L-i, 3850)+i;
M = Math.min(L-i, 2654)+i;
for(;i<M;i++) {
a += buf[i]&0xFF;
b += a;
Expand All @@ -58,7 +58,7 @@ function adler32_str(str, seed) {
var a = 1, b = 0, L = str.length, M = 0, c = 0, d = 0;
if(typeof seed === 'number') { a = seed & 0xFFFF; b = seed >>> 16; }
for(var i = 0; i < L;) {
M = Math.min(L-i, 3850);
M = Math.min(L-i, 2918);
while(M>0) {
c = str.charCodeAt(i++);
if(c < 0x80) { a += c; }
Expand Down
6 changes: 3 additions & 3 deletions bits/40_adler.js
Expand Up @@ -4,7 +4,7 @@ function adler32_bstr(bstr/*:string*/, seed/*:?ADLER32Type*/)/*:ADLER32Type*/ {
var a = 1, b = 0, L = bstr.length, M = 0;
if(typeof seed === 'number') { a = seed & 0xFFFF; b = seed >>> 16; }
for(var i = 0; i < L;) {
M = Math.min(L-i, 3850)+i;
M = Math.min(L-i, 2654)+i;
for(;i<M;i++) {
a += bstr.charCodeAt(i)&0xFF;
b += a;
Expand All @@ -19,7 +19,7 @@ function adler32_buf(buf/*:ABuf*/, seed/*:?ADLER32Type*/)/*:ADLER32Type*/ {
var a = 1, b = 0, L = buf.length, M = 0;
if(typeof seed === 'number') { a = seed & 0xFFFF; b = (seed >>> 16) & 0xFFFF; }
for(var i = 0; i < L;) {
M = Math.min(L-i, 3850)+i;
M = Math.min(L-i, 2654)+i;
for(;i<M;i++) {
a += buf[i]&0xFF;
b += a;
Expand All @@ -35,7 +35,7 @@ function adler32_str(str/*:string*/, seed/*:?ADLER32Type*/)/*:ADLER32Type*/ {
var a = 1, b = 0, L = str.length, M = 0, c = 0, d = 0;
if(typeof seed === 'number') { a = seed & 0xFFFF; b = seed >>> 16; }
for(var i = 0; i < L;) {
M = Math.min(L-i, 3850);
M = Math.min(L-i, 2918);
while(M>0) {
c = str.charCodeAt(i++);
if(c < 0x80) { a += c; }
Expand Down
6 changes: 3 additions & 3 deletions ctest/adler32.js
Expand Up @@ -28,7 +28,7 @@ function adler32_bstr(bstr, seed) {
var a = 1, b = 0, L = bstr.length, M = 0;
if(typeof seed === 'number') { a = seed & 0xFFFF; b = seed >>> 16; }
for(var i = 0; i < L;) {
M = Math.min(L-i, 3850)+i;
M = Math.min(L-i, 2654)+i;
for(;i<M;i++) {
a += bstr.charCodeAt(i)&0xFF;
b += a;
Expand All @@ -43,7 +43,7 @@ function adler32_buf(buf, seed) {
var a = 1, b = 0, L = buf.length, M = 0;
if(typeof seed === 'number') { a = seed & 0xFFFF; b = (seed >>> 16) & 0xFFFF; }
for(var i = 0; i < L;) {
M = Math.min(L-i, 3850)+i;
M = Math.min(L-i, 2654)+i;
for(;i<M;i++) {
a += buf[i]&0xFF;
b += a;
Expand All @@ -58,7 +58,7 @@ function adler32_str(str, seed) {
var a = 1, b = 0, L = str.length, M = 0, c = 0, d = 0;
if(typeof seed === 'number') { a = seed & 0xFFFF; b = seed >>> 16; }
for(var i = 0; i < L;) {
M = Math.min(L-i, 3850);
M = Math.min(L-i, 2918);
while(M>0) {
c = str.charCodeAt(i++);
if(c < 0x80) { a += c; }
Expand Down
18 changes: 17 additions & 1 deletion perf/bstr.js
Expand Up @@ -15,7 +15,7 @@ function sheetjs1(bstr) {
function sheetjs2(bstr) {
var a = 1, b = 0, L = bstr.length, M = 0;
for(var i = 0; i < L;) {
M = Math.min(L-i, 3850)+i;
M = Math.min(L-i, 2654)+i;
for(;i<M;i++) {
a += bstr.charCodeAt(i);
b += a;
Expand All @@ -27,6 +27,20 @@ function sheetjs2(bstr) {
}

function sheetjs3(bstr) {
var a = 1, b = 0, L = bstr.length, M = 0;
for(var i = 0; i < L;) {
M = Math.min(L-i, 3850)+i;
for(;i<M;i++) {
a += bstr.charCodeAt(i);
b += a;
}
a = (15*(a>>>16)+(a&65535))
b = (15*(b>>>16)+(b&65535))
}
return ((b%65521) << 16) | (a%65521);
}

function sheetjs4(bstr) {
var a = 1, b = 0, L = bstr.length, M = 0;
for(var i = 0; i < L;) {
M = Math.min(L-i, 5552);
Expand Down Expand Up @@ -58,11 +72,13 @@ for(var i = 6; i != 14; ++i) {
assert.equal(res, sheetjs1(foobar));
assert.equal(res, sheetjs2(foobar));
assert.equal(res, sheetjs3(foobar));
assert.equal(res, sheetjs4(foobar));

var suite = new BM('binary string (' + foobar.length + ')');
if(i<3) suite.add('sheetjs 1', function() { for(var j = 0; j != m; ++j) sheetjs1(foobar); });
suite.add('sheetjs 2', function() { for(var j = 0; j != m; ++j) sheetjs2(foobar); });
suite.add('sheetjs 3', function() { for(var j = 0; j != m; ++j) sheetjs3(foobar); });
suite.add('sheetjs 4', function() { for(var j = 0; j != m; ++j) sheetjs4(foobar); });
suite.add('last vers', function() { for(var j = 0; j != m; ++j) old(foobar); });
suite.add('current v', function() { for(var j = 0; j != m; ++j) cur(foobar); });
suite.run();
Expand Down
63 changes: 63 additions & 0 deletions perf/utf8.js
Expand Up @@ -17,6 +17,64 @@ function sheetjs1(utf8) {
}

function sheetjs2(utf8) {
var a = 1, b = 0, L = utf8.length, M, c, d;
for(var i = 0; i < L;) {
M = Math.min(L-i, 2654);
while(M>0) {
c = utf8.charCodeAt(i++);
if(c < 0x80) {
a += c; b += a; --M;
} else if(c < 0x800) {
a += 192|((c>>6)&31); b += a; --M;
a += 128|(c&63); b += a; --M;
} else if(c >= 0xD800 && c < 0xE000) {
c = (c&1023)+64; d = utf8.charCodeAt(i++) & 1023;
a += 240|((c>>8)&7); b += a; --M;
a += 128|((c>>2)&63); b += a; --M;
a += 128|((d>>6)&15)|((c&3)<<4); b += a; --M;
a += 128|(d&63); b += a; --M;
} else {
a += 224|((c>>12)&15); b += a; --M;
a += 128|((c>>6)&63); b += a; --M;
a += 128|(c&63); b += a; --M;
}
}
a %= 65521;
b %= 65521;
}
return (b << 16) | a;
}

function sheetjs3(utf8) {
var a = 1, b = 0, L = utf8.length, M, c, d;
for(var i = 0; i < L;) {
M = Math.min(L-i, 2918);
while(M>0) {
c = utf8.charCodeAt(i++);
if(c < 0x80) {
a += c; b += a; --M;
} else if(c < 0x800) {
a += 192|((c>>6)&31); b += a; --M;
a += 128|(c&63); b += a; --M;
} else if(c >= 0xD800 && c < 0xE000) {
c = (c&1023)+64; d = utf8.charCodeAt(i++) & 1023;
a += 240|((c>>8)&7); b += a; --M;
a += 128|((c>>2)&63); b += a; --M;
a += 128|((d>>6)&15)|((c&3)<<4); b += a; --M;
a += 128|(d&63); b += a; --M;
} else {
a += 224|((c>>12)&15); b += a; --M;
a += 128|((c>>6)&63); b += a; --M;
a += 128|(c&63); b += a; --M;
}
}
a %= 65521;
b %= 65521;
}
return (b << 16) | a;
}

function sheetjs4(utf8) {
var a = 1, b = 0, L = utf8.length, M, c, d;
for(var i = 0; i < L;) {
M = Math.min(L-i, 3850);
Expand Down Expand Up @@ -53,11 +111,16 @@ var res = old(foobar);
assert.equal(res, cur(foobar));
assert.equal(res, sheetjs1(foobar));
assert.equal(res, sheetjs2(foobar));
assert.equal(res, sheetjs3(foobar));
assert.equal(res, sheetjs4(foobar));


var BM = require('./bm');
var suite = new BM('unicode string');
suite.add('sheetjs 1', function() { for(var j = 0; j != 1000; ++j) sheetjs1(foobar); });
suite.add('sheetjs 2', function() { for(var j = 0; j != 1000; ++j) sheetjs2(foobar); });
suite.add('sheetjs 3', function() { for(var j = 0; j != 1000; ++j) sheetjs3(foobar); });
suite.add('sheetjs 4', function() { for(var j = 0; j != 1000; ++j) sheetjs4(foobar); });
suite.add('last vers', function() { for(var j = 0; j != 1000; ++j) old(foobar); });
suite.add('current v', function() { for(var j = 0; j != 1000; ++j) cur(foobar); });
suite.run();

0 comments on commit 00ae70e

Please sign in to comment.