-
-
Notifications
You must be signed in to change notification settings - Fork 638
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
Remove clone bounds #37
Conversation
Also includes an unrelated fix for a bug in the two "counters" examples which I noticed while testing. |
Yes, I would love it if you'd add the webcam example. This is the kind of thing that's great to have in the examples folder in addition to the very typical counters and todos. |
I'm working on a few changes to this that will allow Memo to continue taking an Basically the idea is to use RefCell::take() to get the previous value out of the signal inside the memo effect. That signal will temporarily be empty, but it's replaced synchronously by the update so I don't think it's actually possible for anything to read it while it's empty. Stay tuned. I'm terrible at GitHub so not quite sure, but hopefully I can push some changes to the PR later today. |
The fundamental issue I ran into when trying to remove the clone bound from Memo without changing the API is that we need to pass the memo function the previous state and then compare that previous state against the value returned by the function. This means we need to have ownership (or a reference to) the previous state after we call the memo function. As far as I can tell, this can be achieved by:
This could also be circumvented by introducing a Hash bound on the state. That way we could hash the state every time the memo function returns and just compare that hash against the previous hash to determine if it changed. I feel like hash is perhaps more annoying to implement than clone, but you're less likely to have unhashable data than uncloneable data. This does have the advantage though that we never have to clone and the memo function can mutate the state in place rather than allocating new memory if it wants, so this might be more performant? I wouldn't assume either way without doing some benchmarks though. |
You're completely correct, of course. I'd forgotten that we'd need still need to have the old value around, to checked whether it had changed! I think taking You mentioned docs changes but I think you've left everything in a consistent state, as far as I can tell. All right with you if I go ahead and merge it? |
Yeah, hopefully not too many documentation changes, but I wanted to add something for |
Yeah the difference was way more impressive on the first run but even if the actual changes are only 0.01 - 0.02 on the total, that's closing 10-20% of the gap between Leptos and Solid. At this point the benchmarking is a little silly — I'm genuinely focusing on other things like the RPC design — but it's good to feel like we're making things slightly faster, and certainly not slower! I'm going to merge this. Really good work, and thanks for the contributions. |
As mentioned in #36, this removes every
Clone
bound that didn't seem necessary. The main notes are:Resource::with()
method was added, with similar behavior toSignal::with()
to allow access without cloningimpl FnMut(Option<&T>) -> T
instead ofimpl FnMut(Option<T>) -> T
which is a breaking change, albeit a minor oneThis still needs some documentation updates which I'll hopefully get to tomorrow. I'll probably also drop the webcam component that spawned this PR and #35 in as an example unless you object. Tested with all of the examples, but let me know if you see any issues.