Repository navigation
Refactor Record to use a single backing Vec - #12326
Conversation
This shrinks `Record`'s size in half and should make it again amenable to be part of `Value` directly. Might have slightly different performance characteristics as the cache locality changes on lookups (if you directly need the value, it should be closer, but in other cases may blow up the cache line budget) Also different perf characteristics on creation expected. `from_raw_cols_vals` now probably worse. Comparison with the main branch (boxed Record) revealed no significant change to the creation but an improvement when accessing larger N. The fact that this was more pronounced for nested (still cloning before nushell#12325) leads to the conclusion that this may still be dominated by the smaller clone necessary for a 24-byte `Record` over the previous 48 bytes.
| self.index_of(col).and_then(|idx| self.vals.get(idx)) | ||
| self.inner | ||
| .iter() | ||
| .find_map(|(k, v)| if k == col.as_ref() { Some(v) } else { None }) |
There was a problem hiding this comment.
I recently just learned about bool::then_some. This could be
| .find_map(|(k, v)| if k == col.as_ref() { Some(v) } else { None }) | |
| .find_map(|(k, v)| (k == col.as_ref()).then_some(v)) |
if you think that this is better.
There was a problem hiding this comment.
Can go either way, always wondering what the impact on the perf of the debug builds is with those fun little adapters.
|
Hm. I still kind of like the CoW idea as a broader pattern for potentially large objects because cloning values is very common, naively/unnecessarily or not. I would like to be able to keep some large data in a variable and use it multiple times without having to pay for a complex clone with potentially thousands of mallocs every time I do. Since this is a by-value language, optimizing multiple uses of the same value as by-reference in some way is a sensible and transparent optimization to do. But the performance boost on access is also very valuable. |
|
Nice, |
|
I am gonna split this off into two commits
|
9b98b79 to
215e168
Compare
This decouples the serialized representation of `Record` from its internal implementation. It now gets treated as a map type in `serde`. This has several benefits: - more efficient representation (not showing inner fields) - human readable e.g. as a JSON record - no breaking changes when refactoring the `Record` internals in the future (see nushell#12326, or potential introduction of `indexmap::IndexMap` for large N) - we now deny the creation of invalid records a non-cooperating plugin could produce - guaranteed key-value correspondence - checking for unique keys
# Description This decouples the serialized representation of `Record` from its internal implementation. It now gets treated as a map type in `serde`. This has several benefits: - more efficient representation (not showing inner fields) - human readable e.g. as a JSON record - no breaking changes when refactoring the `Record` internals in the future (see #12326, or potential introduction of `indexmap::IndexMap` for large N) - we now deny the creation of invalid records a non-cooperating plugin could produce - guaranteed key-value correspondence - checking for unique keys # Breaking change to the plugin protocol: Now expects a record/map directly as the `Record.val` field instead of a serialization of it.
# Description This decouples the serialized representation of `Record` from its internal implementation. It now gets treated as a map type in `serde`. This has several benefits: - more efficient representation (not showing inner fields) - human readable e.g. as a JSON record - no breaking changes when refactoring the `Record` internals in the future (see nushell#12326, or potential introduction of `indexmap::IndexMap` for large N) - we now deny the creation of invalid records a non-cooperating plugin could produce - guaranteed key-value correspondence - checking for unique keys # Breaking change to the plugin protocol: Now expects a record/map directly as the `Record.val` field instead of a serialization of it.
# Description This decouples the serialized representation of `Record` from its internal implementation. It now gets treated as a map type in `serde`. This has several benefits: - more efficient representation (not showing inner fields) - human readable e.g. as a JSON record - no breaking changes when refactoring the `Record` internals in the future (see nushell#12326, or potential introduction of `indexmap::IndexMap` for large N) - we now deny the creation of invalid records a non-cooperating plugin could produce - guaranteed key-value correspondence - checking for unique keys # Breaking change to the plugin protocol: Now expects a record/map directly as the `Record.val` field instead of a serialization of it.
Description
This shrinks
Record's size in half and and allows you to include it inValuewithout growing the size.Changing the
Recordinternals may have slightly different performance characteristics as the cache locality changes on lookups (if you directly need the value, it should be closer, but in other cases may blow up the cache line budget)Also different perf characteristics on creation expected.
Record::from_raw_cols_valsnow probably worse.Benchmarking
Comparison with the main branch (boxed Record) revealed no significant change to the creation but an improvement when accessing larger N.
The fact that this was more pronounced for nested access (still cloning before #12325) leads to the conclusion that this may still be dominated by the smaller clone necessary for a 24-byte
Recordover the previous 48 bytes.Highlights
The
cargo benchoutput is interleavedFirst line
mainbranchSecond line
record-switcherooFull benchmark run
User-Facing Changes
Reduced memory usage