New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
emcc generates large amount of unused boilerplate JS code for small amount of C code #1838
Comments
Hard to read, can you please fix the formatting of your comment? it looks like one huge wall of text atm ;) |
Magically works now. Github freaked out for a moment there. |
Try closure compiler to reduce code size in addition, EMSCRIPTEN_KEEPALIVE can keep code alive without EXPORTED_FUNCTIONS (but it doesn't export - the code won't be accessible from the outside. |
The |
|
Aha, finally success! I'm down to 16KB of JS code now. I added |
Note that if you really care only about code size, you can get a small reduction from |
Cool, thanks. I'd really like to just elide all of the boilerplate that doesn't have to do with initializing the heap and exporting the module. Some more options for limiting generated boilerplate would be great! |
Closure should remove everything possible (if not, then that is a bug in closure, and we should help them fix it). In general though, emscripten output will always have a little basic overhead in terms of setting up a C-like runtime environment, parts of libc as necessary, etc. But that should be just a few K at most. |
is there any official advice on this?
actually blows the file up to 1.3MB now, where
generates 115KB (both also have --bind and --memory-init-file 0). All I am compiling here is this cpp:
with these pre/post js:
yet there are things like d.da("/tmp");d.da("/home");d.da("/home/web_user")}, which are surely not used. |
by getting rid of --bind magic in favor of exprort "C" / -s EXPORTED_FUNCTIONS="['_test']" I was able to go down to 85KB from 115 above, but still nowhere near 16KB that are claimed here. |
and adding this
only cuts it down to 41KB. |
and DEFAULT_LIBRARY_FUNCS_TO_INCLUDE + bind (and O2) = 71KB. I think I will try to stick with this way as it seem to give good compromise, but if there is anything else I can do - please give me the link. |
|
My emcc command-line is:
This gives me a JS file size of 142KB. That's exceedingly large, in my opinion. My C code compiles to just 363 lines of JS code (about 7KB), whereas the remainder of the enormous generated JS code, 3247 lines, is almost all unused boilerplate code. My C code does not use any emscripten features at all. It is just portable logic and uses no dependencies or external libraries.
I toyed with
-O2
but it only made my code broken because the functions I declared asextern
to be defined in JS were complained about being missing:That seems like a bug to me; extern functions should always be externally "linkable" regardless of optimization. Also, -O2 did not reduce any of the boilerplate code.
I added
-s DEFAULT_LIBRARY_FUNCS_TO_INCLUDE="['memcpy','memset','malloc','free','strlen']"
to my emcc commandline (i.e. removing the'$Browser'
element fromsrc/settings.js
).This significantly reduced the file size to just 53KB, a considerable improvement for sure, but even 53KB is still too large in my opinion. I really just want my portable and independent C code to be compiled to a minimal JS module with no bells or whistles.
Is there no other way to reduce the generated size by chopping out bits of boilerplate? Obviously I don't want to hack up emscripten itself because I want a clean upgrade path. I'd like to see more emphasis on reducing generated code size and even optimizing for it. Outlining is fine but it's not an issue in my code; the excessive amount of boilerplate is.
Also, is there a nice way to define JS-exported functions in my C code as opposed to supplying the EXPORTED_FUNCTIONS list on the emcc commandline? I pored over what little documentation there is but couldn't find anything like this.
I don't mean to sound whiny; this is a great project and I'm loving the results!
The text was updated successfully, but these errors were encountered: