When using emscripten, I've noticed that even small C++ files can result in large JavaScript files. For example:
#include <memory>
int main(int argc, char** argv) {
std::shared_ptr<int> sp(new int);
}
Compiling this with a 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 over 400k in size. Adding -g
allows me to do
grep -n '^function _' foo.js | c++filt -_
to see the functions included. Some examples are:
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
Even though I'm not calling these functions myself, they are included in the JavaScript file, possibly due to virtual function tables or static initializers.
If this was normal code linked to a shared library on my HDD, it would be fine. But having half a megabyte of JavaScript code transferred for a single shared pointer seems excessive. There must be a way to avoid this.