emscripten-core / emscripten-core/emscripten

Big performance difference between two cases with the same semantics

Open
#20,179 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
C++
Stars
27.6k
Forks
3.6k
Avg merge
1d 1h
Merged PRs (30d)
105

Description

Hi, I use the `Emscripten` to compile the attached test case(`flops.fast.c`) and run it on `Wasmtime`. And then I do a very small change(`flops.slow.c`, only move the location of two `printf` statements) in this case which don't affect the program semantics. However, the execution time of `flops.slow.c` is almost 4 slower than that of `flops.fast.c` (measured by `time` tool).

- `flops.fast.c`: 0.615s
- `flops.slow.c`: 2.395s

![截屏2023-09-02 16 03 45](https://github.com/emscripten-core/emscripten/assets/32137313/022b3241-ec29-46a3-8025-fff8f9bd5c1d)

**Version of emscripten/emsdk:**
emcc (Emscripten gcc/clang-like replacement + linker emulating GNU ld) 3.1.45 (ef3e4e3b044de98e1811546e0bc605c65d3412f4)
clang version 18.0.0 (https://github.com/llvm/llvm-project d1e685df45dc5944b43d2547d0138cd4a3ee4efe)
Target: wasm32-unknown-emscripten
Thread model: posix
InstalledDir: /home/ringzzz/emsdk/upstream/bin

**Version of Wasmtime:**
wasmtime-cli 13.0.0

**Command line in full:**
```
emcc -sENVIRONMENT=shell -O2 -s WASM=1 -s TOTAL_MEMORY=512MB flops.slow.c -o flops.slow.wasm
emcc -sENVIRONMENT=shell -O2 -s WASM=1 -s TOTAL_MEMORY=512MB flops.fast.c -o flops.fast.wasm
```

**Command line with -v**
```
"/home/ringzzz/emsdk/upstream/bin/clang" --version
"/home/ringzzz/emsdk/upstream/bin/clang" -target wasm32-unknown-emscripten -fignore-exceptions -fvisibility=default -mllvm -combiner-global-alias-analysis=false -mllvm -enable-emscripten-sjlj -mllvm -disable-lsr -DEMSCRIPTEN -Werror=implicit-function-declaration --sysroot=/home/ringzzz/emsdk/upstream/emscripten/cache/sysroot -Xclang -iwithsysroot/include/fakesdl -Xclang -iwithsysroot/include/compat -O2 -v flops.slow.c -c -o /tmp/emscripten_temp_xa1oznxz/flops.slow_0.o
clang version 18.0.0 (https://github.com/llvm/llvm-project d1e685df45dc5944b43d2547d0138cd4a3ee4efe)
Target: wasm32-unknown-emscripten
Thread model: posix
InstalledDir: /home/ringzzz/emsdk/upstream/bin
(in-process)
"/home/ringzzz/emsdk/upstream/bin/clang-18" -cc1 -triple wasm32-unknown-emscripten -emit-obj -disable-free -clear-ast-before-backend -disable-llvm-verifier -discard-value-names -main-file-name flops.slow.c -mrelocation-model static -mframe-pointer=none -ffp-contract=on -fno-rounding-math -mconstructor-aliases -target-cpu generic -debugger-tuning=gdb -fdebug-compilation-dir=/home/ringzzz/emscripten/test-input -v -fcoverage-compilation-dir=/home/ringzzz/emscripten/test-input -resource-dir /home/ringzzz/emsdk/upstream/lib/clang/18 -D EMSCRIPTEN -isysroot /home/ringzzz/emsdk/upstream/emscripten/cache/sysroot -internal-isystem /home/ringzzz/emsdk/upstream/lib/clang/18/include -internal-isystem /home/ringzzz/emsdk/upstream/emscripten/cache/sysroot/include/wasm32-emscripten -internal-isystem /home/ringzzz/emsdk/upstream/emscripten/cache/sysroot/include -O2 -Werror=implicit-function-declaration -ferror-limit 19 -fvisibility=default -fgnuc-version=4.2.1 -fignore-exceptions -fcolor-diagnostics -vectorize-loops -vectorize-slp -iwithsysroot/include/fakesdl -iwithsysroot/include/compat -mllvm -combiner-global-alias-analysis=false -mllvm -enable-emscripten-sjlj -mllvm -disable-lsr -o /tmp/emscripten_temp_xa1oznxz/flops.slow_0.o -x c flops.slow.c
clang -cc1 version 18.0.0 based upon LLVM 18.0.0git default target x86_64-unknown-linux-gnu
ignoring nonexistent directory "/home/ringzzz/emsdk/upstream/emscripten/cache/sysroot/include/wasm32-emscripten"
#include "..." search starts here:
#include <...> search starts here:
/home/ringzzz/emsdk/upstream/emscripten/cache/sysroot/include/fakesdl
/home/ringzzz/emsdk/upstream/emscripten/cache/sysroot/include/compat
/home/ringzzz/emsdk/upstream/lib/clang/18/include
/home/ringzzz/emsdk/upstream/emscripten/cache/sysroot/include
End of search list.
"/home/ringzzz/emsdk/upstream/bin/wasm-ld" -o flops.slow.wasm /tmp/emscripten_temp_xa1oznxz/flops.slow_0.o -L/home/ringzzz/emsdk/upstream/emscripten/cache/sysroot/lib/wasm32-emscripten /home/ringzzz/emsdk/upstream/emscripten/cache/sysroot/lib/wasm32-emscripten/crt1.o -lGL -lal -lhtml5 -lstandalonewasm-nocatch -lstubs -lc -ldlmalloc -lcompiler_rt -lc++-noexcept -lc++abi-noexcept -lsockets -mllvm -combiner-global-alias-analysis=false -mllvm -enable-emscripten-sjlj -mllvm -disable-lsr /tmp/tmptt40f4yzlibemscripten_js_symbols.so --strip-debug --export-if-defined=__start_em_asm --export-if-defined=__stop_em_asm --export-if-defined=__start_em_lib_deps --export-if-defined=__stop_em_lib_deps --export-if-defined=__start_em_js --export-if-defined=__stop_em_js --export=stackSave --export=stackRestore --export=stackAlloc --export=__errno_location --export-table -z stack-size=65536 --initial-memory=536870912 --max-memory=536870912 --global-base=1024
"/home/ringzzz/emsdk/upstream/bin/llvm-objcopy" flops.slow.wasm flops.slow.wasm --remove-section=.debug* --remove-section=producers
"/home/ringzzz/emsdk/upstream/bin/wasm-emscripten-finalize" --dyncalls-i64 --no-legalize-javascript-ffi --standalone-wasm flops.slow.wasm -o flops.slow.wasm --detect-features
"/home/ringzzz/emsdk/node/16.20.0_64bit/bin/node" /home/ringzzz/emsdk/upstream/emscripten/src/compiler.js /tmp/tmpl3oxy536.json
"/home/ringzzz/emsdk/upstream/bin/wasm-opt" --post-emscripten -O2 --low-memory-unused --zero-filled-memory --pass-arg=directize-initial-contents-immutable flops.slow.wasm -o flops.slow.wasm --mvp-features --enable-mutable-globals --enable-sign-ext
```
[flops.fast.c.txt](https://github.com/emscripten-core/emscripten/files/12503468/flops.fast.c.txt)
[flops.slow.c.txt](https://github.com/emscripten-core/emscripten/files/12503469/flops.slow.c.txt)

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.