emscripten-core / emscripten

Emscripten: An LLVM-to-WebAssembly Compiler
Other
25.8k stars 3.31k forks source link

Performance difference with the native C++ environment. #15826

Open alalialisiong opened 2 years ago

alalialisiong commented 2 years ago

emsdk version : 3.0.0

Hello. I'm working on porting c++ code to JS that uses a header-only library called 'CGAL'. For convenience, the EMBIND function is used. However, wasm performance so different from that in c++ environment is disappointing. The following is the time taken when executing the same function with the same options. I built the program with the following command:

emcc \ -DCGAL_ALWAYS_ROUND_TO_NEAREST \ -DCGAL_WITH_GMPXX \ -DCGAL_USE_GMPXX=1 \ -DCGAL_DO_NOT_USE_BOOST_MP \ -DBOOST_ALL_NO_LIB \ /home/liam/project/src/cgal_interface.cpp \ -I /home/liam/cgal/include \ -I /home/liam/opt/include \ -I /home/liam/eigen-3.3.9 \ -I /home/liam/project/src/boolean \ -I /home/liam/project/src/offset \ -I /home/liam/project/src/simplify \ -I /home/liam/project/src/types \ -I /home/liam/project/src/utils \ -L /home/liam/opt/lib \ -O3 \ -static \ -std=c++1z \ --bind \ -o /home/liam/project/dist/cgal.js \ -lgmpxx \ -lmpfr \ -lgmp \ -s MODULARIZE=1 \ -s USE_BOOST_HEADERS=1 \ -s ALLOW_MEMORY_GROWTH=1 \ -s ASSERTIONS=1 \ -s DISABLE_EXCEPTION_CATCHING=0 \ -s SINGLE_FILE=0 \ -s VERBOSE=1 \ -s ENVIRONMENT=web \

native c++ => 0.085 sec wasm => 4.977 sec

How can I optimize the runtime execution speed?

kripken commented 2 years ago

For a big difference like that, maybe start with profiling,

https://emscripten.org/docs/optimizing/Optimizing-Code.html#profiling

The other ideas on that page might help too.