Skip to content

More compact memory initialization? #2188

Closed

Description

@evanw

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?

Activity

  1. kripken commented on Mar 5, 2014

    @kripken
    Member

    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.

  2. evanw commented on Mar 5, 2014

    @evanw
    ContributorAuthor

    Thanks for the pointer to --memory-init-file. I somehow missed that flag.

  3. kripken commented on Mar 5, 2014

    @kripken
    Member

    Would the 32-bit optimization still interest you? Or not relevant with that flag?

  4. evanw commented on Mar 5, 2014

    @evanw
    ContributorAuthor

    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.

  5. trevorlinton commented on Mar 21, 2014

    @trevorlinton

    +1

  6. chadaustin commented on Mar 31, 2014

    @chadaustin
    Collaborator

    +1 (ccing myself on this thread)

  7. kripken commented on Mar 31, 2014

    @kripken
    Member

    What are the reasons for not using a mem init file? Is there an issue with that option?

  8. trevorlinton commented on Mar 31, 2014

    @trevorlinton

    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?

  9. chadaustin commented on Mar 31, 2014

    @chadaustin
    Collaborator

    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.

  10. kripken commented on Mar 31, 2014

    @kripken
    Member

    @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?

  11. chadaustin commented on Apr 1, 2014

    @chadaustin
    Collaborator

    On Mon, Mar 31, 2014 at 4:46 PM, Alon Zakai [email protected]:

    @trevorlinton https://github.com/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 https://github.com/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
    .

    Chad Austin
    Technical Director, IMVU
    http://engineering.imvu.com http://www.imvu.com/members/Chad/
    http://chadaustin.me

  12. kripken commented on Apr 1, 2014

    @kripken
    Member

    Couldn't you stream the mem init from your asset server? (can't the JS and the mem init be just more assets?)

  13. chadaustin commented on Apr 1, 2014

    @chadaustin
    Collaborator

    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.

  14. kripken commented on Apr 1, 2014

    @kripken
    Member

    Ok, I see.

    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.

    00bc108

  15. 6 remaining items

  16. chadaustin commented on Apr 30, 2014

    @chadaustin
    Collaborator

    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.

    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. :)

  17. kripken commented on Apr 30, 2014

    @kripken
    Member

    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.

  18. dreamlayers commented on Jan 28, 2015

    @dreamlayers
    Contributor

    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.

  19. kripken commented on Jan 28, 2015

    @kripken
    Member

    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.

  20. dreamlayers commented on Jan 29, 2015

    @dreamlayers
    Contributor

    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.

  21. gagern commented on Apr 9, 2015

    @gagern
    Contributor

    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.

  22. gagern commented on Apr 9, 2015

    @gagern
    Contributor

    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.

  23. gagern commented on Apr 9, 2015

    @gagern
    Contributor

    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.

  24. evanw commented on Apr 9, 2015

    @evanw
    ContributorAuthor

    That's awesome! Really cool idea.

  25. gagern commented on Apr 9, 2015

    @gagern
    Contributor

    Implemented this approach for #3326.

  26. stale commented on Aug 31, 2019

    @stale

    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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions