Birch-san / box2d-wasm

Box2D physics engine compiled to WebAssembly. Supports TypeScript and ES modules.
265 stars 21 forks source link

Build with newer Emscripten and different flags #27

Closed Birch-san closed 3 years ago

Birch-san commented 3 years ago

Seems to be more performant now

b2World#Step runs ~7x faster for me now. Previously, the fluid simulation took 18~20ms to do a world step, but now it completes in 2~3ms

Perhaps because of loop unrolling? Or something else? There are far fewer stack frames now. b2World#Step goes 11 stack frames deep, where previously it went as deep as 16.

Smaller

Shaved 90kB off the WASM. Maybe this is because of eliminating longjmp support?

Before:

349 kB Box2D.wasm
405 kB Box2D.js
= 754 kB

After:

259 kB Box2D.wasm
405 kB Box2D.js
= 664 kB

Faster UMD build

Originally I used a variable to skip UMD build whenever I wasn't preparing releases, but it really is identical to the ES build, save for preamble/postamble. We now create a UMD build instantly by doing a string replace over the ES build.

Build on Emscripten 2.0.16

A few reasons

2.0.16

  • Lists that are passed on the command line can now skip the opening and closing braces, allowing for simpler, more readable settings. e.g. -s EXPORTED_FUNCTIONS=foo,bar

2.0.14

  • Clang now performs loop unrolling when targeting WebAssembly at -O2 and higher. It can be disabled using -fno-unroll-loops.

This may be why it runs faster now.

2.0.12

  • Stop overriding CMake default flags based on build type. This will result in builds that are more like CMake does on other platforms. You may notice that RelWithDebInfo will now include debug info (it did not before, which appears to have been an error), and that Release will use -O3 instead of -O2 (which is a better choice anyhow). (#13083)

2.0.9

  • Added experimental support for using emscripten as a post link tool. In this case the input to emscripten is a single wasm file (for example the output of wasm-ld). When emcc is run with --post-link it will take a wasm file as input that perform all the normal post link steps such as finalizing and optimizing the wasm file and generating the JavaScript and/or html that will run it.

2.0.7

  • When -s SUPPORT_LONGJMP=0 is passed to disable longjmp, do not run the LLVM wasm backend path that handles longjmp. Before this only affected linking, and now the flag gives you the ability to affect codegen at compile time too. This is necessary if one does not want any invokes generated for longjmp at all.

Change flags

Removal

-s EXPORT_BINDINGS=1 No longer needed.

-s RESERVED_FUNCTION_POINTERS=20 removed in favour of ALLOW_TABLE_GROWTH

-s NO_EXIT_RUNTIME=1 removed because EXIT_RUNTIME = 0 by default

-s EXPORTED_RUNTIME_METHODS=[] removed because EXPORTED_RUNTIME_METHODS = [] by default

Simplification

-s NO_FILESYSTEM=1 simplified to -s FILESYSTEM=0

Addition

-s SUPPORT_LONGJMP=0 since I didn't notice Box2D's C++ source make any use of exception-handling. Nothing went bang.