Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to generate wasm file. #320

Open
823639792 opened this issue Jun 26, 2023 · 7 comments
Open

How to generate wasm file. #320

823639792 opened this issue Jun 26, 2023 · 7 comments

Comments

@823639792
Copy link

When execute command on Windows:

mkdir build 
cd build
emcmake cmake .. -DCMAKE_EXE_LINKER_FLAGS="-s STANDALONE_WASM" -DCGLM_STATIC=ON
ninja -C .

only generate libcglm.a file, no wasm and js file,
so, how to generate wasm file?

@823639792
Copy link
Author

I want use it to three.js to improve compute speed.

@recp
Copy link
Owner

recp commented Jun 30, 2023

Hi @823639792

Sorry for the delay,

I’ll investigate this asap, libcglm.a can be used link to existing wasm executable but it seems you need wasm to use with js, an option can be added to generate wasm instead of arhive file, @myfreeer any feedback?

Thanks

@myfreeer
Copy link
Contributor

For better performance, it is recommanded to use inline functions, which should be faster compared to linking to a library, where compilers generates extra function call overhead.
Taking keyframe-resample-c as an example, code in C or C++ for your calucation logic, export functions to js, and write js wrapper (js glue), call it from js.
In this case cglm headers are directly included, and functions callable from js are defined in Makefile using -Wl,--export linker flag.
It is recommanded to code the main logic in C and export simple interface to js, and should not make too many calls to wasm from js. Even in modern browsers with jit, there is always overhead of js-to-wasm calls, so less calls lead to better and more predictable performance.

@recp
Copy link
Owner

recp commented Jun 30, 2023

@myfreeer many thanks for the point and benchmark. It seems wasm and simd version are similar ( more or less ), but may not reflect all operations due to auto vectorization maybe. I'll take a closer look asap.

@myfreeer
Copy link
Contributor

myfreeer commented Jul 2, 2023

@myfreeer many thanks for the point and benchmark. It seems wasm and simd version are similar ( more or less ), but may not reflect all operations due to auto vectorization maybe. I'll take a closer look asap.

The keyframe-resample-c project contains a mixed workload of lerp and slerp, the major performance bottleneck seems to be trigonometric functions in slerp, which uses non-simd functions from musl libc for precision. This issue contains an incomplete list of vectorized impl of trigonometric functions.

@recp
Copy link
Owner

recp commented Jul 2, 2023

@myfreeer Wow, thanks! It would be nice to improve and optimize slerp and any other trig functions in cglm, implementing simd here (slerp) is also in TODOs now.

For this issue, are there any benefits ( even a little ) to allow generate .wasm by an build option instead of library for some cases e.g. if someone want to use cglm in JS via wasm like @823639792?

@823639792
Copy link
Author

I made a test that calculates matrix multiplication 5000000 times.
Some computers will bring some improvement, but some older computers, the operation will become slower.
one computer:
WASM: 67ms
JS: 94.80000001192093ms
another:
WASM: 141.09999990463257ms
JS: 131.40000009536743ms

It may indeed be necessary to reduce js calls to wasm in order to improve performance.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants