-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
Implement PartialEq for JS all the things #1433
Comments
I assume this is suggested for the sake of performance. I'm not sure if it would actually be faster in practice, but I guess we can find out. For the actual equality test, we can use On the other hand, Rust distinguishes between (I should also note that in Rust |
It should be noted that I was thinking of something like this in the generated js file...
You are correct that I had suggested the heap pointer comparison for performance to avoid the trampoline. Whether it would be better would depend on how many times it hit, and the cost of the trampoline.... both of which are pretty situational. |
Indeed I am aware of that. I think
What trampoline are you referring to? |
I was borrowing the word trampoline from this article, but thunking probably would have been a better choice. What I meant was the cost of swapping between wasm and js, which with wasm-bindgen infers grabbing something out of an array... To my understanding this means inferring multiple possible cache misses when a simple integer comparison may have done the trick instead. |
I think I would agree that all types in Currently that'd delegate to |
Oh, okay, but those wrappers don't exist (since they've been optimized out already). And using Also, the wasm-bindgen heap will be going away (it will be replaced with a wasm table and So I wouldn't worry about any of that right now. That seems like premature optimization to me. We can always add in that optimization later in a backwards compatible way (after doing benchmarks, of course). @alexcrichton One (potential) benefit of So I would be wary about using So your plan of impling it for |
I work on a system that animates and renders 200,000+ entities every 16ms while concurrently streaming up to 750,000 updates to the world state per second. Cache misses, lock-free threading and the like are my life. At present, I'm porting that system to web... which isn't quite at that level yet. When people say 'premature optimization', I feel these words are often used to shut down legitimate conversations. The hard part is that you never know the needs of the person raising the concern. Sure, 9 times out of 10 premature optimization is a real problem. But, wasm-bindgen is a library that is for people using Rust and WebAssembly... I'm sure some of the users care of this library care about it's performance. I do appreciate that anyref is going to alleviate this problem, so that this problem is a temporary one and temporary problems should get less attention to avoid wasted effort. Also, I don't want to say for sure whether the fast-out is a worthwhile optimization, but I do want wasm-bindgen to care about performance and consider it at every step. |
I certainly wasn't trying to do that! I merely said that the situation is currently in flux (and is changing rapidly, and will change more in the future), so any performance improvements made now might be invalidated soon. In addition, all optimizations (whether premature or not) must be backed by evidence (such as benchmarks, ideally of a real program), because otherwise it might make performance worse. I hope that is not a controversial viewpoint. I thought I made it pretty clear that I wasn't against the idea, I just think we need to do it later (with accompanying benchmarks). Sorry I wasn't clear enough. Believe me, this is Rust, we care a lot about performance (and I personally care a lot about performance, which is one reason why I chose Rust over other languages). And wasm-bindgen has been carefully designed so that it can be as performant as it can be right now, and will become more performant in the future (with host bindings). Our plan is to have faster-than-JS performance. There might be some small tweaks we can do to edge out a bit more performance, so if you find anything like that, let us know (with accompanying use cases and benchmarks). |
Thanks @Pauan, I appreciate that response a lot. |
Done in #1444! |
Motivation
I have, eg: a js_sys::JsString or a js_sys::Array and would like to put them into a struct with #[derive(PartialEq)]
Proposed Solution
Wrappers over js values would implement PartialEq by first comparing the heap pointer for equality and if unequal fall back to calling a javascript function which returns a strict equality comparison (Special consideration may need to be made for floats and NaN in particular to not use the fast-out since NaN != NaN)
Alternatives
One could manually implement equality for the struct without the use of derive, as well as a wrapper over strict equality for js-sys and other wasm-bindgen bindings... or they could use the newtype pattern. Aside from a lot of potential boilerplate they would need to access the internals of a binding (eg: it's heap pointer) to implement the fast out. When a part of a larger solution involving traits or multiple libraries these solutions may fall apart.
The text was updated successfully, but these errors were encountered: