diff options
author | juj <jujjyl@gmail.com> | 2020-02-09 18:23:34 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-02-09 18:23:34 +0200 |
commit | 64760b10911d4dfc828be1fd9ffc74fdd82998ed (patch) | |
tree | 7ea7821d9f5ada5c6500b79d1f2dc063686f2326 /test/wasm2js/emscripten.2asm.js.opt | |
parent | 11c6d59843967640081d7da32f0c43f9fd6ddce6 (diff) | |
download | binaryen-64760b10911d4dfc828be1fd9ffc74fdd82998ed.tar.gz binaryen-64760b10911d4dfc828be1fd9ffc74fdd82998ed.tar.bz2 binaryen-64760b10911d4dfc828be1fd9ffc74fdd82998ed.zip |
Optimize base64 decoding (#2623)
* Optimize base64 decoding (about 7x-10x faster and temporary garbage-free compared to the original version)
* new Uint8Array
* Reuse Uint8Array view
* Fix end handling
* Code format
* Update tests
Diffstat (limited to 'test/wasm2js/emscripten.2asm.js.opt')
-rw-r--r-- | test/wasm2js/emscripten.2asm.js.opt | 64 |
1 files changed, 34 insertions, 30 deletions
diff --git a/test/wasm2js/emscripten.2asm.js.opt b/test/wasm2js/emscripten.2asm.js.opt index 84e33bb6f..f554f869a 100644 --- a/test/wasm2js/emscripten.2asm.js.opt +++ b/test/wasm2js/emscripten.2asm.js.opt @@ -183,37 +183,41 @@ function asmFunc(global, env, buffer) { }; } -var writeSegment = ( - function(mem) { - var _mem = new Uint8Array(mem); - return function(offset, s) { - var bytes, i; - if (typeof Buffer === 'undefined') { - bytes = atob(s); - for (i = 0; i < bytes.length; i++) - _mem[offset + i] = bytes.charCodeAt(i); - } else { - bytes = Buffer.from(s, 'base64'); - for (i = 0; i < bytes.length; i++) - _mem[offset + i] = bytes[i]; - } - } +for (var base64ReverseLookup = new Uint8Array(123/*'z'+1*/), i = 25; i >= 0; --i) { + base64ReverseLookup[48+i] = 52+i; // '0-9' + base64ReverseLookup[65+i] = i; // 'A-Z' + base64ReverseLookup[97+i] = 26+i; // 'a-z' + } + base64ReverseLookup[43] = 62; // '+' + base64ReverseLookup[47] = 63; // '/' + /** @noinline Inlining this function would mean expanding the base64 string 4x times in the source code, which Closure seems to be happy to do. */ + function base64DecodeToExistingUint8Array(uint8Array, offset, b64) { + var b1, b2, i = 0, j = offset, bLength = b64.length, end = offset + (bLength*3>>2); + if (b64[bLength-2] == '=') --end; + if (b64[bLength-1] == '=') --end; + for (; i < bLength; i += 4, j += 3) { + b1 = base64ReverseLookup[b64.charCodeAt(i+1)]; + b2 = base64ReverseLookup[b64.charCodeAt(i+2)]; + uint8Array[j] = base64ReverseLookup[b64.charCodeAt(i)] << 2 | b1 >> 4; + if (j+1 < end) uint8Array[j+1] = b1 << 4 | b2 >> 2; + if (j+2 < end) uint8Array[j+2] = b2 << 6 | base64ReverseLookup[b64.charCodeAt(i+3)]; } - )(wasmMemory.buffer); -writeSegment(1024, "aGVsbG8sIHdvcmxkIQoAAJwMAAAtKyAgIDBYMHgAKG51bGwp"); -writeSegment(1072, "EQAKABEREQAAAAAFAAAAAAAACQAAAAALAAAAAAAAAAARAA8KERERAwoHAAETCQsLAAAJBgsAAAsABhEAAAARERE="); -writeSegment(1153, "CwAAAAAAAAAAEQAKChEREQAKAAACAAkLAAAACQALAAAL"); -writeSegment(1211, "DA=="); -writeSegment(1223, "DAAAAAAMAAAAAAkMAAAAAAAMAAAM"); -writeSegment(1269, "Dg=="); -writeSegment(1281, "DQAAAAQNAAAAAAkOAAAAAAAOAAAO"); -writeSegment(1327, "EA=="); -writeSegment(1339, "DwAAAAAPAAAAAAkQAAAAAAAQAAAQAAASAAAAEhIS"); -writeSegment(1394, "EgAAABISEgAAAAAAAAk="); -writeSegment(1443, "Cw=="); -writeSegment(1455, "CgAAAAAKAAAAAAkLAAAAAAALAAAL"); -writeSegment(1501, "DA=="); -writeSegment(1513, "DAAAAAAMAAAAAAkMAAAAAAAMAAAMAAAwMTIzNDU2Nzg5QUJDREVGLTBYKzBYIDBYLTB4KzB4IDB4AGluZgBJTkYAbmFuAE5BTgAu"); + } +var bufferView = new Uint8Array(wasmMemory.buffer); +base64DecodeToExistingUint8Array(bufferView, 1024, "aGVsbG8sIHdvcmxkIQoAAJwMAAAtKyAgIDBYMHgAKG51bGwp"); +base64DecodeToExistingUint8Array(bufferView, 1072, "EQAKABEREQAAAAAFAAAAAAAACQAAAAALAAAAAAAAAAARAA8KERERAwoHAAETCQsLAAAJBgsAAAsABhEAAAARERE="); +base64DecodeToExistingUint8Array(bufferView, 1153, "CwAAAAAAAAAAEQAKChEREQAKAAACAAkLAAAACQALAAAL"); +base64DecodeToExistingUint8Array(bufferView, 1211, "DA=="); +base64DecodeToExistingUint8Array(bufferView, 1223, "DAAAAAAMAAAAAAkMAAAAAAAMAAAM"); +base64DecodeToExistingUint8Array(bufferView, 1269, "Dg=="); +base64DecodeToExistingUint8Array(bufferView, 1281, "DQAAAAQNAAAAAAkOAAAAAAAOAAAO"); +base64DecodeToExistingUint8Array(bufferView, 1327, "EA=="); +base64DecodeToExistingUint8Array(bufferView, 1339, "DwAAAAAPAAAAAAkQAAAAAAAQAAAQAAASAAAAEhIS"); +base64DecodeToExistingUint8Array(bufferView, 1394, "EgAAABISEgAAAAAAAAk="); +base64DecodeToExistingUint8Array(bufferView, 1443, "Cw=="); +base64DecodeToExistingUint8Array(bufferView, 1455, "CgAAAAAKAAAAAAkLAAAAAAALAAAL"); +base64DecodeToExistingUint8Array(bufferView, 1501, "DA=="); +base64DecodeToExistingUint8Array(bufferView, 1513, "DAAAAAAMAAAAAAkMAAAAAAAMAAAMAAAwMTIzNDU2Nzg5QUJDREVGLTBYKzBYIDBYLTB4KzB4IDB4AGluZgBJTkYAbmFuAE5BTgAu"); return asmFunc({ 'Int8Array': Int8Array, 'Int16Array': Int16Array, |