key: avoid allocation for non Rc<str> From implementations#3855
Conversation
`From<&str> for Rc` clones the content of the &str, so there are two allocations for most types: `to_string()` and `Rc::from`. Using a Rc<String> instead of Rc<str> internally can avoid this additional allocation for most types, and completely avoid it for `String`. This will now allocate when using it for Rc<str> directly (which wasn't the case previously). Since direct use of Rc<str> will not happen very often, I'd argue that this tradeoff is better than the status quo. Signed-off-by: Dominik Csapak <d.csapak@proxmox.com>
Size ComparisonDetails
✅ None of the examples has changed their size significantly. |
|
Visit the preview URL for this PR (updated for commit 18ad8a9): https://yew-rs-api--pr3855-key-rc-string-s78hgdv5.web.app (expires Thu, 15 May 2025 11:04:03 GMT) 🔥 via Firebase Hosting GitHub Action 🌎 |
Benchmark - coreYew MasterPull Request |
Benchmark - SSRYew MasterDetails
Pull RequestDetails
|
|
I fail to see how this helps in its current state.
This is not true, only one allocation is done. The existing code uses the Note that this is a dynamically sized value, and the single allocation of the
It can not avoid the allocation in the
I fail to see that
I agree. Yet, There is one part that might be worth to improve. The current |
i think allocation was the wrong choice of words here, I of course meant memcopy.
the last part is what i wanted to avoid, but see below, the double indirection is probably not worth it
yes, just noticed the double indirection too, so that's too bad. I don't have a good benchmark for this sadly.. but yes, if comparing is more often done than allocating, it's not worth that
yeah this was exactly what i was trying to avoid, but I did not find a good way to do that besides changing the only trivial improvement that i can see is to use a manual implementation for |
That is indeed a good catch! I haven't even noticed that we effectively clone the string there. Changing the impl to a manual impl From<String> for Key {
fn from(key: String) -> Self {
Self::from(key.as_str())
}
}should do the trick, I suppose. EDIT: Reading the source code, the |
|
ok, i'd close this pull request and do another one for the string case, or should i force push here ? |
|
Yes please do so |
From<&str> for Rcclones the content of the &str, so there are two allocations for most types:to_string()andRc::from.Using a Rc instead of Rc internally can avoid this additional allocation for most types, and completely avoid it for
String. This will now allocate when using it for Rc directly (which wasn't the case previously).Since direct use of Rc will not happen very often, I'd argue that this tradeoff is better than the status quo.
Description
change the internal type of
Keyto avoid allocations in most casesChecklist