nushell/crates/nu-command/src/stor/reset.rs
Ian Manske 9996e4a1f8
Shrink the size of Expr (#12610)
# Description
Continuing from #12568, this PR further reduces the size of `Expr` from
64 to 40 bytes. It also reduces `Expression` from 128 to 96 bytes and
`Type` from 32 to 24 bytes.

This was accomplished by:
- for `Expr` with multiple fields (e.g., `Expr::Thing(A, B, C)`),
merging the fields into new AST struct types and then boxing this struct
(e.g. `Expr::Thing(Box<ABC>)`).
- replacing `Vec<T>` with `Box<[T]>` in multiple places. `Expr`s and
`Expression`s should rarely be mutated, if at all, so this optimization
makes sense.

By reducing the size of these types, I didn't notice a large performance
improvement (at least compared to #12568). But this PR does reduce the
memory usage of nushell. My config is somewhat light so I only noticed a
difference of 1.4MiB (38.9MiB vs 37.5MiB).

---------

Co-authored-by: Stefan Holderbach <sholderbach@users.noreply.github.com>
2024-04-24 15:46:35 +00:00

72 lines
1.9 KiB
Rust

use crate::database::{SQLiteDatabase, MEMORY_DB};
use nu_engine::command_prelude::*;
#[derive(Clone)]
pub struct StorReset;
impl Command for StorReset {
fn name(&self) -> &str {
"stor reset"
}
fn signature(&self) -> Signature {
Signature::build("stor reset")
.input_output_types(vec![(Type::Nothing, Type::table())])
.allow_variants_without_examples(true)
.category(Category::Database)
}
fn usage(&self) -> &str {
"Reset the in-memory database by dropping all tables."
}
fn search_terms(&self) -> Vec<&str> {
vec!["sqlite", "remove", "table", "saving", "drop"]
}
fn examples(&self) -> Vec<Example> {
vec![Example {
description: "Reset the in-memory sqlite database",
example: "stor reset",
result: None,
}]
}
fn run(
&self,
_engine_state: &EngineState,
_stack: &mut Stack,
call: &Call,
_input: PipelineData,
) -> Result<PipelineData, ShellError> {
let span = call.head;
// Open the in-mem database
let db = Box::new(SQLiteDatabase::new(std::path::Path::new(MEMORY_DB), None));
if let Ok(conn) = db.open_connection() {
db.drop_all_tables(&conn)
.map_err(|err| ShellError::GenericError {
error: "Failed to open SQLite connection in memory from reset".into(),
msg: err.to_string(),
span: Some(Span::test_data()),
help: None,
inner: vec![],
})?;
}
// dbg!(db.clone());
Ok(Value::custom(db, span).into_pipeline_data())
}
}
#[cfg(test)]
mod test {
use super::*;
#[test]
fn test_examples() {
use crate::test_examples;
test_examples(StorReset {})
}
}