emscripten-core / emscripten-core/emscripten

Might be possible to reduce overhead of JS exceptions without explicit EXCEPTION_CATCHING_ALLOWED list?

Open
#24,617 2 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

I've been playing a bit with various exception options lately and noticed a difference in the Chrome stack traces when comparing a plain `-fexceptions` build against one with an explicit `EXCEPTION_CATCHING_ALLOWED` list that I thought was worth mentioning.

My tests are using a [fork](https://github.com/tangobravo/zxing-cpp/tree/emscripten-exception-tests) of the ZXing library so others should be able to reproduce the same behaviour from that repo.

With `-fexceptions` I see a JS invoke function in between almost every function on the stack:

Image

With an explicit list of all the functions that `catch` in the codebase set as `-sEXCEPTION_CATCHING_ALLOWED` there are far fewer jumps through JS - only directly after the two functions in this stack trace that are include in that explicit list.

Image

I did look at the preprocessed source of QRDecoder.o with `-fexceptions` and can see there are some other functions that catch pulled in from the standard library, so all the JS jumps are quite possibly required for correctness.

However, if there is a way to get the compiler to automatically flag those "functions from user code that may catch" to get the same performance as the explicit list then that might mitigate much of the performance overhead of the standard `-fexceptions` mode and free build systems from having to maintain that list.

In my fork I added a CMake option to set the exception flags, so you can choose js/wasm/allowlist/none builds, all of which have `--fprofiling-funcs` too.
```
emcmake cmake -DZXING_EMSCRIPTEN_EXCEPTION_MODE=allowlist ../wrappers/wasm
```

ZXing uses exceptions internally for propagating decode errors for some barcode formats. I've added `invalid-qr.png` to that repo that contains invalid data. Open the `demo-reader.html` page, select QR from the dropdown and choose that invalid image, and you'll see a "No QRCodes Detected" message. Select "pause on caught exceptions" in DevTools to see the stack traces when the exceptions are thrown.

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.