Crates.io | sledgehammer_bindgen_macro |
lib.rs | sledgehammer_bindgen_macro |
version | 0.6.0 |
source | src |
created_at | 2024-02-27 16:28:38.763921 |
updated_at | 2024-08-22 16:10:43.735857 |
description | Fast batched js bindings |
homepage | |
repository | https://github.com/demonthos/sledgehammer_bindgen/ |
max_upload_size | |
id | 1155233 |
size | 86,794 |
Sledgehammer bindgen provides faster rust batched bindings for js code.
wasm-bindgen is a lot more general it allows returning values and passing around a lot more different types of values. For most users wasm-bindgen is a better choice. Sledgehammer is specifically designed for web frameworks that want low-level, fast access to the dom.
You can use sledgehammer bindgen with wasm-bindgen. See the docs and examples for more information.
Strings are expensive to decode, but the cost doesn't change much with the size of the string. Wasm-bindgen calls TextDecoder.decode for every string. Sledgehammer only calls TextEncoder.decode once per batch.
If the string is small, it is faster to decode the string in javascript to avoid the constant overhead of TextDecoder.decode
See this benchmark: https://jsbench.me/4vl97c05lb/5
In sledgehammer every operation is encoded as a sequence of bytes packed into an array. Every operation takes 1 byte plus whatever data is required for it.
Each operation is encoded in a batch of four as a u32. Getting a number from an array buffer has a high constant cost, but getting a u32 instead of a u8 is not more expensive. Sledgehammer bindgen reads the u32 and then splits it into the 4 individual bytes. It will shuffle and pack the bytes into as few buckets as possible and try to inline reads into js.
See this benchmark: https://jsbench.me/csl9lfauwi/2