Omit some C ++ subsystems

I notice that with emscripten even relatively small C ++ files can quickly turn into quite huge JavaScript files. Example:

#include <memory> int main(int argc, char** argv) { std::shared_ptr<int> sp(new int); } 

Compile this with recent emsdk using a command like

 em++ -std=c++11 -s DISABLE_EXCEPTION_CATCHING=1 -s NO_FILESYSTEM=1 \ -s NO_BROWSER=1 -s NO_EXIT_RUNTIME=1 -O3 -o foo.js foo.cc 

The resulting file is larger than 400 KB. When resetting -g I can do

 grep -n '^function _' foo.js | c++filt -_ 

and see what features we have. Here are some examples:

 std::__1::moneypunct<char, false>::do_thousands_sep() const std::__1::locale::~locale() std::__1::basic_string<wchar_t, …>::~basic_string() std::__1::time_get<…>::__get_day(…) const std::__1::codecvt<wchar_t, char, __mbstate_t>::codecvt(unsigned int) std::__1::locale::__imp::install(std::__1::locale::facet*, long) _printf_core 

I myself do not call this, but nonetheless, all functions are included. Probably many of them are included in some kind of virtual function table. Others may be associated with some kind of static initializer.

If it was regular code associated with one shared library somewhere on my hard drive; I would not mind. But half the megabyte in the JavaScript code that needs to be transmitted is only for one common pointer? There must be a way to avoid this.

+6
source share
1 answer

One solution implemented here simply splits the C ++ library into several parts. By moving the code associated with I / O and locale to a separate library, all code that can work without it can avoid the static initializer of the I / O subsystem, which leads to a dependency on the functions described above. Unfortunately, this will also affect strstream for obvious reasons.


Update: Since upstream commit 301e4ad (first included in version 1.30.6), system libraries are no longer compiled as a single *.bc , but instead as a static *.a library that contains several different objects. Of these, only those associated with them are really related, which significantly reduces the size of the code for simple cases.

+1
source

Source: https://habr.com/ru/post/984692/


All Articles