Repository navigation
Copy-on-write for record values - #12305
Conversation
|
Benchmarks: first line is before, next line is after. |
|
In general, I feel like improving the |
|
This is very awesome, 10x on better on nested record access, Awesome work @devyn. You can also check the difference in memory allocation done after this change in divan by adding these two lines to the benchmark file. Side note: |
|
Thanks @FilipAndersson245! For future reference, I did this basically: git checkout main
cargo bench -- --min-time=5 | tee { save before.txt }
git checkout -
cargo bench -- --min-time=5 | tee { save after.txt }
open before.txt | lines | zip { open after.txt | lines } | flatten | save combined.txtWe could potentially have a script for this. Also I use |
a65f14c to
e341902
Compare
for the 128 nested record we have 26x fewer allocations, 😎Nice |
One of the selling points of divan is it robustness, so it should probably not be needed, having a good script that compares two different outputs would be very nice, |
I personally found that some of the really quick benchmarks could be overly noisy with the default settings, and setting a minimum time helped a lot... so I'm not sure. |
|
I renamed |
f68b478 to
796079b
Compare
|
Looking at the numbers, I am honestly not yet fully convinced of the runtime benefit for the incurred complexity. The only dramatic change is appearing for nushell/crates/nu-protocol/src/value/mod.rs Lines 1062 to 1067 in 3857e36 And indeed @IanManske ran into an annoying situation needing to clone the retrieved value for .... drum roll ... case-insensitive matching + did you mean. #11066 (comment) nushell/crates/nu-protocol/src/value/mod.rs Lines 1163 to 1189 in 3857e36 |
|
In theory, the
I think most of the remaining places that we clone are due to tricky or lazy implementation. But of course, they would still benefit from the reference counting until fixed. |
|
Good points @IanManske . Your reasoning is solid @devyn, my flippant reply is certainly based on the lack of benchmarks covering more "real" operations fairly instead of drilling down on just one aspect. Having some of those engine specific aspects exercised by benchmarks would also allow us to trade off at which granularity we would want to introduce the CoW behavior. (top level values stored in variables, records/lists, strings as well...) With how costly some of those Value refactors are (nervously looking at #12239), I really want to make sure that we are not obfuscating patterns in our implementation that need general rework. |
|
I would certainly like to try adding some more benchmarks that do some simulated real work with a complex mix of operations rather than just simply covering core operations on different data structures. There are likely to be synergistic effects that don't really make sense to benchmark in isolation. |
Only short-term solution Competes with nushell#12305
Only short-term solution Competes with nushell#12305
|
Rebased onto main (after #12325 and #12326) Seems to be overall performing better than #12340, but creation is slightly tending towards reduced performance.
|
|
And for the direct comparison to #12340 This rebased on main vs inserting `Record` directly into `Value`
For reference comparing my two runs on `main@0cf7de5`
|
|
To add yet more chaos, I have another branch using the |
|
Okay, interesting. It seems like it's mostly a wash vs #12340, but a bit faster sometimes. In that case, if this isn't really any slower, I think it might be better to go with this so we get the extra benefits. Thanks for comparing @sholderbach |
|
Okay, so the remaining thing to clear up is whether we're comfortable with the I definitely understand the aversion to introducing a |
|
Not a fan of this |
|
Okay, duly noted. I will fix it. |
796079b to
2b8a0ac
Compare
This adds a `Shared` type as a transparent copy-on-write pointer that clones to unique on mutable dereference. As an initial test, the `Record` within `Value::Record` is shared.
`Shared::unwrap()` renamed to `SharedCow::into_owned()`, which takes `self`, a little more ergonomic
This makes it more like the `Cow` interface and avoids sneaky clones
446faf2 to
7ac57ba
Compare
|
All green.
I think the slight awkwardness will actually encourage less unnecessary taking ownership, so we should have less cloning over time |
|
Where are we at on this? Seems like we're close but conflicts are now preventing landing? |
|
Sure, I'll resolve conflicts probably tomorrow |
|
Updated. 🙂 |
IanManske
left a comment
There was a problem hiding this comment.
Thanks! This looks good to go. Just a few minor fixes left.
|
@IanManske thanks for looking at this in detail! Great catches! I implemented everything you suggested. |
|
Great, thanks! Merging 🚀 |
Description
This adds a
SharedCowtype as a transparent copy-on-write pointer that clones to unique on mutate.As an initial test, the
RecordwithinValue::Recordis shared.There are some pretty big wins for performance. I'll post benchmark results in a comment. The biggest winner is nested access, as that would have cloned the records for each cell path follow before and it doesn't have to anymore.
The reusability of the
SharedCowtype is nice and I think it could be used to clean up the previous work I did withArcinEngineState. It's meant to be a mostly transparent clone-on-write that just clones on.to_mut()or.into_owned()if there are actually multiple references, but avoids cloning if the reference is unique.User-Facing Changes
Value::Recordfield is a different type (plugin authors)Tests + Formatting
toolkit fmttoolkit clippytoolkit testtoolkit test stdlibAfter Submitting
EngineStateValue::List