turbolent / w2c2

Translates WebAssembly modules to portable C
MIT License
712 stars 37 forks source link

Add some string/char related micro-optimizations #73

Closed tekknolagi closed 1 year ago

tekknolagi commented 1 year ago

Together, these patches bring down wasm2c compile time of clang.wasm from 1.7s to 1.2s on my machine. Let me know if you would like these and if you would like me to squash.

tekknolagi commented 1 year ago

stringBuilderAppendChar is now 12% of total instructions according to callgrind.

tekknolagi commented 1 year ago

There's also probably some room to split out a version for C-string literals so that it can be inlined and the strlen can be folded away, which---if you want it---would also make writing characters faster on the current code without my patches.

EDIT: Nevermind, I think this already happens.

tekknolagi commented 1 year ago

Rebased the strncpy->memcpy change out of existence since I am less sure about that one and it's not relevant in the PR, I think. I'll figure out how to run tests locally.

tekknolagi commented 1 year ago

The tests look weirdly suspiciously broken, so I figured it was UB or something. Ran locally with -fsanitize=undefined and can't get them to fail :/

tekknolagi commented 1 year ago

Also, further, I diffed clang.c before and after my changes and there is no difference...

turbolent commented 1 year ago

@tekknolagi Thank you for the optimizations, the numbers look great! I'll try to find time to investigate why the tests fail

tekknolagi commented 1 year ago

:partying_face: