nushell/crates/nu-cli/src
Reilly Wood 3b5172a8fa
LazyRecord (#7619)
This is an attempt to implement a new `Value::LazyRecord` variant for
performance reasons.

`LazyRecord` is like a regular `Record`, but it's possible to access
individual columns without evaluating other columns. I've implemented
`LazyRecord` for the special `$nu` variable; accessing `$nu` is
relatively slow because of all the information in `scope`, and [`$nu`
accounts for about 2/3 of Nu's startup time on
Linux](https://github.com/nushell/nushell/issues/6677#issuecomment-1364618122).

### Benchmarks

I ran some benchmarks on my desktop (Linux, 12900K) and the results are
very pleasing.

Nu's time to start up and run a command (`cargo build --release;
hyperfine 'target/release/nu -c "echo \"Hello, world!\""' --shell=none
--warmup 10`) goes from **8.8ms to 3.2ms, about 2.8x faster**.

Tests are also much faster! Running `cargo nextest` (with our very slow
`proptest` tests disabled) goes from **7.2s to 4.4s (1.6x faster)**,
because most tests involve launching a new instance of Nu.

### Design (updated)

I've added a new `LazyRecord` trait and added a `Value` variant wrapping
those trait objects, much like `CustomValue`. `LazyRecord`
implementations must implement these 2 functions:

```rust
// All column names
fn column_names(&self) -> Vec<&'static str>;

// Get 1 specific column value
fn get_column_value(&self, column: &str) -> Result<Value, ShellError>;
 ```

### Serializability

`Value` variants must implement `Serializable` and `Deserializable`, which poses some problems because I want to use unserializable things like `EngineState` in `LazyRecord`s. To work around this, I basically lie to the type system:

1. Add `#[typetag::serde(tag = "type")]` to `LazyRecord` to make it serializable
2. Any unserializable fields in `LazyRecord` implementations get marked with `#[serde(skip)]`
3. At the point where a `LazyRecord` normally would get serialized and sent to a plugin, I instead collect it into a regular `Value::Record` (which can be serialized)
2023-01-18 19:27:26 -08:00
..
completions LazyRecord (#7619) 2023-01-18 19:27:26 -08:00
menus Reduced LOC by replacing several instances of Value::Int {}, Value::Float{}, Value::Bool {}, and Value::String {} with Value::int(), Value::float(), Value::boolean() and Value::string() (#7412) 2022-12-09 11:37:51 -05:00
commands.rs
config_files.rs Improve empty pipelines (#7383) 2022-12-08 07:31:57 +13:00
eval_file.rs Add FILE_PWD environment variable when running 'nu script.nu' (#7424) 2022-12-10 19:23:44 +02:00
lib.rs
nu_highlight.rs Remove engine_state clones in REPL eval (#7713) 2023-01-10 17:22:32 -08:00
print.rs Add more input/output type annotations (#7532) 2022-12-21 20:20:46 +01:00
prompt_update.rs clean up some extra logging code in the cli (#7709) 2023-01-08 15:05:46 -05:00
prompt.rs Add cursor shape configuration for each edit mode (#7745) 2023-01-13 14:37:39 -06:00
reedline_config.rs Extend config support from F1-F12 to F1-F20, #7666 (#7669) 2023-01-03 22:00:21 +01:00
repl.rs Add cursor shape configuration for each edit mode (#7745) 2023-01-13 14:37:39 -06:00
syntax_highlight.rs Support redirect err and out to different streams (#7685) 2023-01-12 10:22:30 +01:00
util.rs Fix streaming page missing newline (#7466) 2022-12-14 16:45:37 +13:00
validation.rs Remove engine_state clones in REPL eval (#7713) 2023-01-10 17:22:32 -08:00