You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I noticed that the static memory initializer in the generated code uses bytes instead of 32-bit ints. Switching it to use 32-bit ints instead made the code significantly smaller in our case (we lost around 150kb). Is there a reason this isn't done when targeting typed arrays?
When you use --memory-init-file you get a binary file anyhow, which is the recommended route when using large initializers. So the other code path has not been optimized that much. But you are very correct, this would be smaller and in the new compiler probably not that hard to do.
We should be fine with --memory-init-file but it seems to me like it's still worth doing since it's such a big win for such a seemingly simple change. This optimization would still benefit projects that are loaded over file://, for example.
It's difficult to use multiple files and embedding that within the JS isn't an option unfortunately. Albeit that might be a short term solution for me. Is it significant work to use Uint32?
We package up our engine JavaScript file with other JavaScript files and expect it to be delivered "atomically", so it wouldn't be able to go and fetch the mem init file at load time. We also wouldn't know where to load it (everything gets uploaded to the CDN by the hash of its content).
It's way more convenient if we can just have an efficient JavaScript representation of the mem init array. Perhaps a string or base64 string of some sort.
It's not yet a big enough pain point to fund work to submit a pull request, but perhaps this clarifies our use case.
@trevorlinton Not sure what you mean about "multiple files and embedding"? The mem init file should just work normally in all respects, except there is an extra file.
It is probably not too much work to do uint32. Just write some python to scan and rewrite that stuff.
@chadaustin , surely you download lots of binary assets too? Or is the whole filesystem in a single JS file?
Ah, we don't embed any files into our program. We stream all of the 3D
content from our asset server.
The static memory initialization is mostly string constants and global
variables.
Reply to this email directly or view it on GitHubhttps://github.com//issues/2188#issuecomment-39155956
.
The asset server is only for 3D content, which is independent of the current version of the emscripten-compiled engine. Also, sometimes the engine is loaded within Node.js or shipped to arbitrary other projects as a single JavaScript file. It's also packaged up with closure compiler into other projects at the company, and they may or may not have convenient access to the asset server.
It's just way more convenient to have a single, self-contained JavaScript file with no external dependencies.
Meanwhile I cooked up a patch in the i32-mem-init branch. Looks like it works on most stuff but i see test suite failures, help with more testing and figuring out those issues would be very helpful.
Whoa, surprising results! I just ran a benchmark of the current approach (large array of uint8 literals) against a string literal and string literals won handily!
My test methodology was to use console.time in the HTML and console.timeEnd in the JS to measure both script parse and ArrayBuffer initialization time. I ran against my local disk to avoid any network IO.
The string literal approach is easily the winner, most likely because the JavaScript parser has less work to do.
Makes me wonder if there are other large arrays in Emscripten output that can be converted to string literals... Could probably result in a sizable parse time reduction. :)
Compilers generally put zero-initialized variables in a bss section, and only include non-zero initialized variables in the memory initializer. Emscripten does not do this. An ArrayBuffer is initialized to zero according to the specification, so variables could be initialized to zero for free.
Emscripten has the USE_BSS option and some associated code, but that is not getting executed. I guess it only gets executed when not using fastcomp? Is BSS not implemented with fastcomp, or is something broken?
I see that normally, clang will use bss and common sections, but when compiling with Emscripten options it only puts a few variables in common. Then the opt invocation moves everything into the data segment. After that, initializer splitting can help with long stretches of zeros if not using a memory init file. Finally, gzip compression can help, but no form of lossless compression can fully compensate for this.
I've also noticed that the memory init file is counterproductive with DOSBox after gzip compression:
Without .mem: 1000033 js.gz
With .mem: 856088 js.gz + 147788 mem.gz = 1003876
I don't know if this is because of long spans of zeros removed by initializer splitting. I thought gzip should handle that sort of thing well.
Yes, we had someone add a BSS optimization in the old compiler, and I think nothing has been done in the current one. So we might have a suboptimal memory initializer.
I'm surprised the memory init file is worse after gzip. Startup should still be faster, though, even with a slightly larger download (handling binary data is much faster than parsing JS). But it would be nice to understand this.
With DOSBox, I'm not sure that using the memory init file helps startup times. Using file:// URLs in Firefox 35, startup seems faster without. That wasn't using gzip though. I'm turning off the memory init file when not using emterpreter for now.
I did some performance tests. Turns out my base88 takes about 1.7 × the time of minstr when parsing. Replacing the indexOf call with a lookup table I can make that factor 1.3 or so. Still worse, and it compresses poorly as well. It's still nice in terms of uncompressed size, but I doubt that will warrant using it. Surprising thing is, however, how poorly the compression in Python is. That takes ages!
I've got a small improvement to offer for the minstr implementation: if you XOR all bytes with 32, you'll avoid all those \0 and escape sequences for small bytes in general. That reduces uncompressed size from 230.8k to 217.9k, and although the win in compressed size, from 47.1 to 46.9, is neglgible, at least things didn't get worse there. I've got the feeling that parsing time in Chromium even decreases, while Firefox remains the same. It's hard to tell with all the fluctuation I'm seeing there, though.
Taking that XOR approach one step further, one could do a character frequency analysis and encode the common bytes with printable characters. See tinylut in https://github.com/gagern/Web-Benchmarks/tree/tinylut/meminit for an implementation. Uncompressed 187.7k resp. compressed 46.2k. Initialization time has suffered a bit, but not too much I think. Not sure how the compression would look if one were to include this in a larger emscripten output file.
This issue has been automatically marked as stale because there has been no activity in the past 2 years. It will be closed automatically if no further activity occurs in the next 7 days. Feel free to re-open at any time if this issue is still relevant.
Activity
kripken commentedon Mar 5, 2014
When you use
--memory-init-fileyou get a binary file anyhow, which is the recommended route when using large initializers. So the other code path has not been optimized that much. But you are very correct, this would be smaller and in the new compiler probably not that hard to do.evanw commentedon Mar 5, 2014
Thanks for the pointer to
--memory-init-file. I somehow missed that flag.kripken commentedon Mar 5, 2014
Would the 32-bit optimization still interest you? Or not relevant with that flag?
evanw commentedon Mar 5, 2014
We should be fine with
--memory-init-filebut it seems to me like it's still worth doing since it's such a big win for such a seemingly simple change. This optimization would still benefit projects that are loaded overfile://, for example.trevorlinton commentedon Mar 21, 2014
+1
chadaustin commentedon Mar 31, 2014
+1 (ccing myself on this thread)
kripken commentedon Mar 31, 2014
What are the reasons for not using a mem init file? Is there an issue with that option?
trevorlinton commentedon Mar 31, 2014
It's difficult to use multiple files and embedding that within the JS isn't an option unfortunately. Albeit that might be a short term solution for me. Is it significant work to use Uint32?
chadaustin commentedon Mar 31, 2014
We package up our engine JavaScript file with other JavaScript files and expect it to be delivered "atomically", so it wouldn't be able to go and fetch the mem init file at load time. We also wouldn't know where to load it (everything gets uploaded to the CDN by the hash of its content).
It's way more convenient if we can just have an efficient JavaScript representation of the mem init array. Perhaps a string or base64 string of some sort.
It's not yet a big enough pain point to fund work to submit a pull request, but perhaps this clarifies our use case.
kripken commentedon Mar 31, 2014
@trevorlinton Not sure what you mean about "multiple files and embedding"? The mem init file should just work normally in all respects, except there is an extra file.
It is probably not too much work to do uint32. Just write some python to scan and rewrite that stuff.
@chadaustin , surely you download lots of binary assets too? Or is the whole filesystem in a single JS file?
chadaustin commentedon Apr 1, 2014
On Mon, Mar 31, 2014 at 4:46 PM, Alon Zakai [email protected]:
The static memory initialization is mostly string constants and global
variables.
Chad Austin
Technical Director, IMVU
http://engineering.imvu.com http://www.imvu.com/members/Chad/
http://chadaustin.me
kripken commentedon Apr 1, 2014
Couldn't you stream the mem init from your asset server? (can't the JS and the mem init be just more assets?)
chadaustin commentedon Apr 1, 2014
The asset server is only for 3D content, which is independent of the current version of the emscripten-compiled engine. Also, sometimes the engine is loaded within Node.js or shipped to arbitrary other projects as a single JavaScript file. It's also packaged up with closure compiler into other projects at the company, and they may or may not have convenient access to the asset server.
It's just way more convenient to have a single, self-contained JavaScript file with no external dependencies.
kripken commentedon Apr 1, 2014
Ok, I see.
Meanwhile I cooked up a patch in the
i32-mem-initbranch. Looks like it works on most stuff but i see test suite failures, help with more testing and figuring out those issues would be very helpful.00bc108
6 remaining items
chadaustin commentedon Apr 30, 2014
Whoa, surprising results! I just ran a benchmark of the current approach (large array of uint8 literals) against a string literal and string literals won handily!
My test methodology was to use
console.timein the HTML andconsole.timeEndin the JS to measure both script parse and ArrayBuffer initialization time. I ran against my local disk to avoid any network IO.See control.html and minstr.html at https://github.com/chadaustin/Web-Benchmarks/tree/master/meminit
The results I measured on my machine were:
control (array of 8-bit integers)
Chrome: 75 ms [trials: 75, 67, 81, 82, 70]
Firefox: 40.4 ms [trials: 38.62, 44.22, 39.66, 40.54, 38.77]
minstr (string literal)
Chrome: 19.8 ms [trials: 17, 20, 24, 18, 20]
Firefox: 11.6 ms [trials: 7.25, 16.92, 8.49, 19.62, 5.59]
The string literal approach is easily the winner, most likely because the JavaScript parser has less work to do.
Makes me wonder if there are other large arrays in Emscripten output that can be converted to string literals... Could probably result in a sizable parse time reduction. :)
kripken commentedon Apr 30, 2014
Wow, nice!
Not many other big arrays. The allocate() ones are the significant ones. So optimizing those with ministr should give a nice speedup here.
dreamlayers commentedon Jan 28, 2015
Compilers generally put zero-initialized variables in a bss section, and only include non-zero initialized variables in the memory initializer. Emscripten does not do this. An
ArrayBufferis initialized to zero according to the specification, so variables could be initialized to zero for free.Emscripten has the
USE_BSSoption and some associated code, but that is not getting executed. I guess it only gets executed when not using fastcomp? Is BSS not implemented with fastcomp, or is something broken?I see that normally, clang will use bss and common sections, but when compiling with Emscripten options it only puts a few variables in common. Then the
optinvocation moves everything into the data segment. After that, initializer splitting can help with long stretches of zeros if not using a memory init file. Finally, gzip compression can help, but no form of lossless compression can fully compensate for this.I've also noticed that the memory init file is counterproductive with DOSBox after gzip compression:
Without .mem: 1000033 js.gz
With .mem: 856088 js.gz + 147788 mem.gz = 1003876
I don't know if this is because of long spans of zeros removed by initializer splitting. I thought gzip should handle that sort of thing well.
kripken commentedon Jan 28, 2015
Yes, we had someone add a BSS optimization in the old compiler, and I think nothing has been done in the current one. So we might have a suboptimal memory initializer.
I'm surprised the memory init file is worse after gzip. Startup should still be faster, though, even with a slightly larger download (handling binary data is much faster than parsing JS). But it would be nice to understand this.
dreamlayers commentedon Jan 29, 2015
I think DOSBox might be a special case due to having very large zero initialized variables. It uses over 20 MB for the TLB (for efficient paging). I just tried https://github.com/dreamlayers/synaesthesia/ and there the memory init file definitely helps: 137182 vs.162551 bytes gzip -9 compressed.
With DOSBox, I'm not sure that using the memory init file helps startup times. Using file:// URLs in Firefox 35, startup seems faster without. That wasn't using gzip though. I'm turning off the memory init file when not using emterpreter for now.
gagern commentedon Apr 9, 2015
I just filed #3326 about encoding the memory initializer into a string literal. Hadn't known about this report here before. I'm using base 88, not escape sequences in the literal. https://github.com/chadaustin/Web-Benchmarks/tree/master/meminit ends up at 186,700 bytes uncompressed resp. 61,366 after gzip. I haven't done any performance tests yet.
gagern commentedon Apr 9, 2015
I did some performance tests. Turns out my base88 takes about 1.7 × the time of minstr when parsing. Replacing the
indexOfcall with a lookup table I can make that factor 1.3 or so. Still worse, and it compresses poorly as well. It's still nice in terms of uncompressed size, but I doubt that will warrant using it. Surprising thing is, however, how poorly the compression in Python is. That takes ages!I've got a small improvement to offer for the minstr implementation: if you XOR all bytes with 32, you'll avoid all those
\0and escape sequences for small bytes in general. That reduces uncompressed size from 230.8k to 217.9k, and although the win in compressed size, from 47.1 to 46.9, is neglgible, at least things didn't get worse there. I've got the feeling that parsing time in Chromium even decreases, while Firefox remains the same. It's hard to tell with all the fluctuation I'm seeing there, though.gagern commentedon Apr 9, 2015
Taking that XOR approach one step further, one could do a character frequency analysis and encode the common bytes with printable characters. See
tinylutin https://github.com/gagern/Web-Benchmarks/tree/tinylut/meminit for an implementation. Uncompressed 187.7k resp. compressed 46.2k. Initialization time has suffered a bit, but not too much I think. Not sure how the compression would look if one were to include this in a larger emscripten output file.evanw commentedon Apr 9, 2015
That's awesome! Really cool idea.
gagern commentedon Apr 9, 2015
Implemented this approach for #3326.
stale commentedon Aug 31, 2019
This issue has been automatically marked as stale because there has been no activity in the past 2 years. It will be closed automatically if no further activity occurs in the next 7 days. Feel free to re-open at any time if this issue is still relevant.