Improves startup time when using std-lib

This commit is contained in:
NotTheDr01ds 2024-09-13 13:24:59 -04:00
parent aaaab8e070
commit 192ca033ca
8 changed files with 46 additions and 70 deletions

View File

@ -163,7 +163,7 @@ pub fn evaluate_repl(
eval_source(
engine_state,
&mut unique_stack,
r#"use std banner; banner"#.as_bytes(),
r#"banner"#.as_bytes(),
"show_banner",
PipelineData::empty(),
false,

View File

@ -9,20 +9,16 @@ use nu_protocol::{
};
use std::path::PathBuf;
// Virtual std directory unlikely to appear in user's file system
const NU_STDLIB_VIRTUAL_DIR: &str = "NU_STDLIB_VIRTUAL_DIR";
pub fn load_standard_library(
engine_state: &mut nu_protocol::engine::EngineState,
) -> Result<(), miette::ErrReport> {
trace!("load_standard_library");
let (block, delta) = {
// Using full virtual path to avoid potential conflicts with user having 'std' directory
// in their working directory.
let std_dir = PathBuf::from(NU_STDLIB_VIRTUAL_DIR).join("std");
let std_dir = PathBuf::from("std");
let mut std_files = vec![
("mod.nu", include_str!("../std/mod.nu")),
("core.nu", include_str!("../std/core.nu")),
("dirs.nu", include_str!("../std/dirs.nu")),
("dt.nu", include_str!("../std/dt.nu")),
("help.nu", include_str!("../std/help.nu")),
@ -52,26 +48,15 @@ pub fn load_standard_library(
let std_dir = std_dir.to_string_lossy().to_string();
let source = r#"
# Define the `std` module
module std
# Prelude
use std dirs [
enter
shells
g
n
p
dexit
]
use std pwd
use std/core.nu *
"#;
let _ = working_set.add_virtual_path(std_dir, VirtualPath::Dir(std_virt_paths));
// Add a placeholder file to the stack of files being evaluated.
// The name of this file doesn't matter; it's only there to set the current working directory to NU_STDLIB_VIRTUAL_DIR.
let placeholder = PathBuf::from(NU_STDLIB_VIRTUAL_DIR).join("loading stdlib");
let placeholder = PathBuf::from("loading stdlib");
working_set.files = FileStack::with_file(placeholder);
let block = parse(

33
crates/nu-std/std/core.nu Normal file
View File

@ -0,0 +1,33 @@
use dt.nu [datetime-diff, pretty-print-duration]
# Print a banner for nushell with information about the project
export def banner [] {
let dt = (datetime-diff (date now) 2019-05-10T09:59:12-07:00)
$"(ansi green) __ ,(ansi reset)
(ansi green) .--\(\)°'.' (ansi reset)Welcome to (ansi green)Nushell(ansi reset),
(ansi green)'|, . ,' (ansi reset)based on the (ansi green)nu(ansi reset) language,
(ansi green) !_-\(_\\ (ansi reset)where all data is structured!
Please join our (ansi purple)Discord(ansi reset) community at (ansi purple)https://discord.gg/NtAbbGn(ansi reset)
Our (ansi green_bold)GitHub(ansi reset) repository is at (ansi green_bold)https://github.com/nushell/nushell(ansi reset)
Our (ansi green)Documentation(ansi reset) is located at (ansi green)https://nushell.sh(ansi reset)
(ansi cyan)Tweet(ansi reset) us at (ansi cyan_bold)@nu_shell(ansi reset)
Learn how to remove this at: (ansi green)https://nushell.sh/book/configuration.html#remove-welcome-message(ansi reset)
It's been this long since (ansi green)Nushell(ansi reset)'s first commit:
(pretty-print-duration $dt)
Startup Time: ($nu.startup-time)
"
}
# Return the current working directory
export def pwd [
--physical (-P) # resolve symbolic links
] {
if $physical {
$env.PWD | path expand
} else {
$env.PWD
}
}

View File

@ -1,5 +1,6 @@
# std.nu, `used` to load all standard library components
export module core.nu
export module assert.nu
export module dirs.nu
export module dt.nu
@ -15,8 +16,6 @@ export-env {
use log.nu []
}
use dt.nu [datetime-diff, pretty-print-duration]
# Add the given paths to the PATH.
#
# # Example
@ -160,27 +159,6 @@ export def bench [
}
}
# Print a banner for nushell with information about the project
export def banner [] {
let dt = (datetime-diff (date now) 2019-05-10T09:59:12-07:00)
$"(ansi green) __ ,(ansi reset)
(ansi green) .--\(\)°'.' (ansi reset)Welcome to (ansi green)Nushell(ansi reset),
(ansi green)'|, . ,' (ansi reset)based on the (ansi green)nu(ansi reset) language,
(ansi green) !_-\(_\\ (ansi reset)where all data is structured!
Please join our (ansi purple)Discord(ansi reset) community at (ansi purple)https://discord.gg/NtAbbGn(ansi reset)
Our (ansi green_bold)GitHub(ansi reset) repository is at (ansi green_bold)https://github.com/nushell/nushell(ansi reset)
Our (ansi green)Documentation(ansi reset) is located at (ansi green)https://nushell.sh(ansi reset)
(ansi cyan)Tweet(ansi reset) us at (ansi cyan_bold)@nu_shell(ansi reset)
Learn how to remove this at: (ansi green)https://nushell.sh/book/configuration.html#remove-welcome-message(ansi reset)
It's been this long since (ansi green)Nushell(ansi reset)'s first commit:
(pretty-print-duration $dt)
Startup Time: ($nu.startup-time)
"
}
# the cute and friendly mascot of Nushell :)
export def ellie [] {
let ellie = [
@ -193,17 +171,6 @@ export def ellie [] {
$ellie | str join "\n" | $"(ansi green)($in)(ansi reset)"
}
# Return the current working directory
export def pwd [
--physical (-P) # resolve symbolic links
] {
if $physical {
$env.PWD | path expand
} else {
$env.PWD
}
}
# repeat anything a bunch of times, yielding a list of *n* times the input
#
# # Examples

View File

@ -0,0 +1,7 @@
use std assert
#[test]
def banner [] {
use std core
assert ((core banner | lines | length) == 15)
}

View File

@ -1,5 +1,4 @@
use std assert
use std assert
use std log
# A couple of nuances to understand when testing module that exports environment:

View File

@ -70,11 +70,6 @@ def path_add_expand [] {
rm $real_dir $link_dir
}
#[test]
def banner [] {
std assert ((std banner | lines | length) == 15)
}
#[test]
def repeat_things [] {
std assert error { "foo" | std repeat -1 }

View File

@ -1,15 +1,5 @@
use crate::repl::tests::{fail_test, run_test_std, TestResult};
#[test]
fn library_loaded() -> TestResult {
run_test_std("scope modules | where name == 'std' | length", "1")
}
#[test]
fn prelude_loaded() -> TestResult {
run_test_std("shells | length", "1")
}
#[test]
fn not_loaded() -> TestResult {
fail_test("log info", "")