Improves startup time when using std-lib (#13842)
Updated summary for commit
[612e0e2](https://github.com/nushell/nushell/pull/13842/commits/612e0e21602f55092bc121bfd07f7c3bf5119e4f)
- While folks are welcome to read through the entire comments, the core
information is summarized here.
# Description
This PR drastically improves startup times of Nushell by only parsing a
single submodule of the Standard Library that provides the `banner` and
`pwd` commands. All other Standard Library commands and submodules are
parsed when imported by the user. This cuts startup times by more than
60%.
At the moment, we have stopped adding to `std-lib` because every
addition adds a small amount to the Nushell startup time.
With this change, we should once again be able to allow new
functionality to be added to the Standard Library without it impacting
`nu` startup times.
# User-Facing Changes
* Nushell now starts about 60% faster
* Breaking change: The `dirs` (Shells) aliases will return a warning
message that it will not be auto-loaded in the following release, along
with instructions on how to restore it (and disable the message)
* The `use std <submodule> *` syntax is available for convenience, but
should be avoided in scripts as it parses the entire `std` module and
all other submodules and places it in scope. The correct syntax to
*just* load a submodule is `use std/<submodule> *` (asterisk optional).
The slash is important. This will be documented.
* `use std *` can be used for convenience to load all of the library but
still incurs the full loading-time.
* `std/dirs`: Semi-breaking change. The `dirs` command replaces the
`show` command. This is more in line with the directory-stack
functionality found in other shells. Existing users will not be impacted
by this as the alias (`shells`) remains the same.
* Breaking-change: Technically a breaking change, but probably only
impacts maintainers of `std`. The virtual path for the standard library
has changed. It could previously be imported using its virtual path (and
technically, this would have been the correct way to do it):
```nu
use NU_STDLIB_VIRTUAL_DIR/std
```
The path is now simply `std/`:
```nu
use std
```
All submodules have moved accordingly.
# Timings
Comparisons below were made:
* In a temporary, clean config directory using `$env.XDG_CONFIG_HOME =
(mktemp -d)`.
* `nu` was run with a release build
* `nu` was run one time to generate the default `config.nu` (etc.) files
- Otherwise timings would include the user-prompt
* The shell was exited and then restarted several times to get timing
samples
(Note: Old timings based on 0.97 rather than 0.98, but in the range of
being accurate)
| Scenario | `$nu.startup-time` |
| --- | --- |
| 0.97.2
([aaaab8e](https://github.com/nushell/nushell/commit/aaaab8e070c644a87bbd7682099e3fe9e6a4b42a))
Without this PR | 23ms - 24ms |
| This PR with deprecated commands | 9ms - <11ms |
| This PR after deprecated commands are removed in following release |
8ms - <10ms |
| Final PR (remove deprecated), using `--no-std-lib` | 6.1ms to 6.4ms |
| Final PR (remove deprecated), using `--no-config-file` | 3.1ms - 3.6ms
|
| Final PR (remove deprecated), using `--no-config-file --no-std-lib` |
1ms - 1.5ms |
*These last two timings point to the opportunity for further
optimization (see comment in thread below (will link once I write it).*
# Implementation details for future maintenance
* `use std banner` is a ridiculously deceptive call. That call parses
and imports *all* of `std` into scope. Simply replacing it with `use
std/core *` is essentially what saves ~14-15ms. This *only* imports the
submodule with the `banner` and `pwd` commands.
* From the code-comments, the reason that `NU_STDLIB_VIRTUAL_DIR` was
used as a prefix was so that there wouldn't be an issue if a user had a
`./std/mod.nu` in the current directory. This does **not** appear to be
an issue. After removing the prefix, I tested with both a relative
module as well as one in the `$env.NU_LIB_DIRS` path, and in all cases
the *internal* `std` still took precedence.
* By removing the prefix, users can now `use std` (and variants) without
requiring that it already be parsed and in scope.
* In the next release, we'll stop autoloading the `dirs` (shells)
functionality. While this only costs an additional 1-1.5ms, I think it's
better moved to the `config.nu` where the user can optionally remove it.
The main reason is its use of aliases (which have also caused issues) -
The `n`, `p`, and `g` short-commands are valuable real-estate, and users
may want to map these to something else.
For this release, there's an `deprecated_dirs` module that is still
autoloaded. As with the top-level commands, use of these will give a
deprecation warning with instructions on how to handle going forward.
To help with this, moved the aliases to their own submodule inside the
`dirs` module.
* Also sneaks in a small change where the top-level `dirs` command is
now the replacement for `dirs show`
* Fixed a double-import of `assert` in `dirs.nu`
* The `show_banner` step is replaced with simply `banner` rather than
re-importing it.
* A `virtual_path` may now be referenced with either a forward-slash or
a backward-slash on Windows. This allows `use std/<submodule>` to work
on all platforms.
# Performance side-notes:
* Future parsing and/or IR improvements should improve performance even
further.
* While the existing load time penalty of `std-lib` was not noticeable
on many systems, Nushell runs on a wide-variety of hardware and OS
platforms. Slower platforms will naturally see a bigger jump in
performance here. For users starting multiple Nushell sessions
frequently (e.g., `tmux`, Zellij, `screen`, et. al.) it is recommended
to keep total startup time (including user configuration) under ~250ms.
# Tests + Formatting
* All tests are green
* Updated tests:
- Removed the test that confirmed that `std` was loaded (since we
don't).
- Removed the `shells` test since it is not autoloaded. Main `dirs.nu`
functionality is tested through `stdlib-test`.
- Many tests assumed that the library was fully loaded, because it was
(even though we didn't intend for it to be). Fixed those tests.
- Tests now import only the necessary submodules (e.g., `use
std/assert`, rather than `use std assert`)
- Some tests *thought* they were loading `std/log`, but were doing so
improperly. This was masked by the now-fixed "load-everything-into-scope
bug". Local CI would pass due the `$env.NU_LOG_<...>` variables being
inherited from the calling process, but would fail in the "clean" GitHub
CI environment. These tests have also been fixed.
* Added additional tests for the changes
# After Submitting
Will update the Standard Library doc page
2024-10-03 13:28:22 +02:00
use std/log
export-env {
# Place NU_FORMAT... environment variables in module-scope
export use std/log *
}
2023-06-10 20:16:17 +02:00
2023-07-08 11:27:56 +02:00
def "nu-complete threads" [] {
2024-05-07 01:20:27 +02:00
seq 1 (sys cpu | length)
2023-07-08 11:27:56 +02:00
}
2023-06-10 20:16:17 +02:00
2023-07-02 10:41:33 +02:00
# Here we store the map of annotations internal names and the annotation actually used during test creation
# The reason we do that is to allow annotations to be easily renamed without modifying rest of the code
# Functions with no annotations or with annotations not on the list are rejected during module evaluation
# test and test-skip annotations may be used multiple times throughout the module as the function names are stored in a list
# Other annotations should only be used once within a module file
# If you find yourself in need of multiple before- or after- functions it's a sign your test suite probably needs redesign
def valid-annotations [] {
{
"#[test]": "test",
"#[ignore]": "test-skip",
"#[before-each]": "before-each"
"#[before-all]": "before-all"
"#[after-each]": "after-each"
"#[after-all]": "after-all"
}
}
# Returns a table containing the list of function names together with their annotations (comments above the declaration)
def get-annotated [
file: path
2023-10-01 15:37:51 +02:00
] path -> table<function_name: string, annotation: string> {
let raw_file = (
open $file
| lines
2023-07-02 10:41:33 +02:00
| enumerate
| flatten
)
2023-10-01 15:37:51 +02:00
$raw_file
| where item starts-with def and index > 0
2023-07-02 10:41:33 +02:00
| insert annotation {|x|
2023-10-01 15:37:51 +02:00
$raw_file
| get ($x.index - 1)
| get item
| str trim
2023-07-02 10:41:33 +02:00
}
2023-10-01 15:37:51 +02:00
| where annotation in (valid-annotations|columns)
| reject index
| update item {
2023-10-19 22:08:09 +02:00
split column --collapse-empty ' '
2023-10-01 15:37:51 +02:00
| get column2.0
}
| rename function_name
2023-07-02 10:41:33 +02:00
}
# Takes table of function names and their annotations such as the one returned by get-annotated
#
# Returns a record where keys are internal names of valid annotations and values are corresponding function names
# Annotations that allow multiple functions are of type list<string>
# Other annotations are of type string
# Result gets merged with the template record so that the output shape remains consistent regardless of the table content
2023-10-01 15:37:51 +02:00
def create-test-record [] nothing -> record<before-each: string, after-each: string, before-all: string, after-all: string, test: list<string>, test-skip: list<string>> {
2023-07-02 10:41:33 +02:00
let input = $in
let template_record = {
before-each: '',
before-all: '',
after-each: '',
after-all: '',
test-skip: []
}
let test_record = (
$input
| update annotation {|x|
valid-annotations
| get $x.annotation
}
2023-10-28 21:15:14 +02:00
| group-by --to-table annotation
| update items {|x|
$x.items.function_name
| if $x.group in ["test", "test-skip"] {
2023-07-02 10:41:33 +02:00
$in
} else {
get 0
}
}
2023-10-08 13:12:46 +02:00
| transpose --ignore-titles -r -d
2023-07-02 10:41:33 +02:00
)
$template_record
| merge $test_record
}
2023-06-10 20:16:17 +02:00
def throw-error [error: record] {
error make {
msg: $"(ansi red)($error.msg)(ansi reset)"
label: {
text: ($error.label)
2023-11-03 16:09:33 +01:00
span: $error.span
2023-06-10 20:16:17 +02:00
}
}
}
# show a test record in a pretty way
#
# `$in` must be a `record<file: string, module: string, name: string, pass: bool>`.
#
# the output would be like
# - "<indentation> x <module> <test>" all in red if failed
# - "<indentation> s <module> <test>" all in yellow if skipped
# - "<indentation> <module> <test>" all in green if passed
def show-pretty-test [indent: int = 4] {
let test = $in
[
2023-09-14 20:11:08 +02:00
(1..$indent | each {" "} | str join)
2023-06-10 20:16:17 +02:00
(match $test.result {
"pass" => { ansi green },
"skip" => { ansi yellow },
_ => { ansi red }
})
(match $test.result {
"pass" => " ",
"skip" => "s",
_ => { char failed }
})
" "
$"($test.name) ($test.test)"
(ansi reset)
] | str join
}
2023-07-02 10:41:33 +02:00
# Takes a test record and returns the execution result
# Test is executed via following steps:
# * Public function with random name is generated that runs specified test in try/catch block
# * Module file is opened
# * Random public function is appended to the end of the file
# * Modified file is saved under random name
# * Nu subprocess is spawned
# * Inside subprocess the modified file is imported and random function called
# * Output of the random function is serialized into nuon and returned to parent process
# * Modified file is removed
2023-06-10 20:16:17 +02:00
def run-test [
test: record
] {
2023-10-19 22:04:33 +02:00
let test_file_name = (random chars --length 10)
let test_function_name = (random chars --length 10)
2023-06-10 20:16:17 +02:00
let rendered_module_path = ({parent: ($test.file|path dirname), stem: $test_file_name, extension: nu}| path join)
let test_function = $"
export def ($test_function_name) [] {
($test.before-each)
try {
$context | ($test.test)
($test.after-each)
} catch { |err|
($test.after-each)
2023-07-02 10:41:33 +02:00
$err | get raw
2023-06-10 20:16:17 +02:00
}
}
"
open $test.file
| lines
| append ($test_function)
| str join (char nl)
| save $rendered_module_path
let result = (
2023-07-07 09:20:36 +02:00
^$nu.current-exe --no-config-file -c $"use ($rendered_module_path) *; ($test_function_name)|to nuon"
2023-06-10 20:16:17 +02:00
| complete
)
rm $rendered_module_path
return $result
}
2023-07-02 10:41:33 +02:00
# Takes a module record and returns a table with following columns:
#
# * file - path to file under test
# * name - name of the module under test
# * test - name of specific test
# * result - test execution result
2023-06-10 20:16:17 +02:00
def run-tests-for-module [
2023-07-02 10:41:33 +02:00
module: record<file: path name: string before-each: string after-each: string before-all: string after-all: string test: list test-skip: list>
2023-07-08 11:27:56 +02:00
threads: int
2023-10-01 15:37:51 +02:00
] -> table<file: path, name: string, test: string, result: string> {
2023-07-02 10:41:33 +02:00
let global_context = if not ($module.before-all|is-empty) {
2023-06-10 20:16:17 +02:00
log info $"Running before-all for module ($module.name)"
run-test {
file: $module.file,
before-each: 'let context = {}',
after-each: '',
2023-07-02 10:41:33 +02:00
test: $module.before-all
2023-06-10 20:16:17 +02:00
}
| if $in.exit_code == 0 {
$in.stdout
} else {
throw-error {
msg: "Before-all failed"
label: "Failure in test setup"
span: (metadata $in | get span)
}
}
} else {
{}
}
2023-07-02 10:41:33 +02:00
# since tests are skipped based on their annotation and never actually executed we can generate their list in advance
let skipped_tests = (
if not ($module.test-skip|is-empty) {
$module
| update test $module.test-skip
| reject test-skip
| flatten
| insert result 'skip'
} else {
[]
}
)
2023-06-10 20:16:17 +02:00
let tests = (
$module
2023-07-02 10:41:33 +02:00
| reject test-skip
| flatten test
2023-06-10 20:16:17 +02:00
| update before-each {|x|
2023-07-02 10:41:33 +02:00
if not ($module.before-each|is-empty) {
$"let context = \(($global_context)|merge \(($module.before-each)\)\)"
2023-06-10 20:16:17 +02:00
} else {
$"let context = ($global_context)"
}
}
| update after-each {|x|
2023-07-02 10:41:33 +02:00
if not ($module.after-each|is-empty) {
$"$context | ($module.after-each)"
2023-06-10 20:16:17 +02:00
} else {
''
}
}
2023-07-08 11:27:56 +02:00
| par-each --threads $threads {|test|
2023-07-02 10:41:33 +02:00
log info $"Running ($test.test) in module ($module.name)"
log debug $"Global context is ($global_context)"
2023-06-10 20:16:17 +02:00
$test|insert result {|x|
run-test $test
2023-07-02 10:41:33 +02:00
| if $in.exit_code == 0 {
'pass'
} else {
'fail'
2023-06-10 20:16:17 +02:00
}
}
}
2023-07-02 10:41:33 +02:00
| append $skipped_tests
| select file name test result
2023-06-10 20:16:17 +02:00
)
2023-07-02 10:41:33 +02:00
if not ($module.after-all|is-empty) {
2023-06-10 20:16:17 +02:00
log info $"Running after-all for module ($module.name)"
run-test {
file: $module.file,
before-each: $"let context = ($global_context)",
after-each: '',
2023-07-02 10:41:33 +02:00
test: $module.after-all
2023-06-10 20:16:17 +02:00
}
}
return $tests
}
2023-07-02 10:41:33 +02:00
# Run tests for nushell code
#
# By default all detected tests are executed
# Test list can be filtered out by specifying either path to search for, name of the module to run tests for or specific test name
# In order for a function to be recognized as a test by the test runner it needs to be annotated with # test
# Following annotations are supported by the test runner:
# * test - test case to be executed during test run
# * test-skip - test case to be skipped during test run
# * before-all - function to run at the beginning of test run. Returns a global context record that is piped into every test function
# * before-each - function to run before every test case. Returns a per-test context record that is merged with global context and piped into test functions
# * after-each - function to run after every test case. Receives the context record just like the test cases
# * after-all - function to run after all test cases have been executed. Receives the global context record
2023-06-10 20:16:17 +02:00
export def run-tests [
2023-07-08 11:27:56 +02:00
--path: path, # Path to look for tests. Default: current directory.
--module: string, # Test module to run. Default: all test modules found.
--test: string, # Pattern to use to include tests. Default: all tests found in the files.
--exclude: string, # Pattern to use to exclude tests. Default: no tests are excluded
--exclude-module: string, # Pattern to use to exclude test modules. Default: No modules are excluded
--list, # list the selected tests without running them.
--threads: int@"nu-complete threads", # Amount of threads to use for parallel execution. Default: All threads are utilized
2023-06-10 20:16:17 +02:00
] {
2024-05-13 15:45:44 +02:00
let available_threads = (sys cpu | length)
2023-07-08 11:27:56 +02:00
# Can't use pattern matching here due to https://github.com/nushell/nushell/issues/9198
let threads = (if $threads == null {
$available_threads
} else if $threads < 1 {
1
} else if $threads <= $available_threads {
$threads
} else {
$available_threads
})
2023-06-10 20:16:17 +02:00
let module_search_pattern = ('**' | path join ({
stem: ($module | default "*")
extension: nu
} | path join))
let path = if $path == null {
$env.PWD
} else {
if not ($path | path exists) {
throw-error {
msg: "directory_not_found"
label: "no such directory"
span: (metadata $path | get span)
}
}
$path
}
if not ($module | is-empty) {
try { ls ($path | path join $module_search_pattern) | null } catch {
throw-error {
msg: "module_not_found"
label: $"no such module in ($path)"
span: (metadata $module | get span)
}
}
}
let modules = (
`open`, `rm`, `umv`, `cp`, `rm` and `du`: Don't globs if inputs are variables or string interpolation (#11886)
# Description
This is a follow up to
https://github.com/nushell/nushell/pull/11621#issuecomment-1937484322
Also Fixes: #11838
## About the code change
It applys the same logic when we pass variables to external commands:
https://github.com/nushell/nushell/blob/0487e9ffcbc57c2d5feca606e10c3f8221ff5e00/crates/nu-command/src/system/run_external.rs#L162-L170
That is: if user input dynamic things(like variables, sub-expression, or
string interpolation), it returns a quoted `NuPath`, then user input
won't be globbed
# User-Facing Changes
Given two input files: `a*c.txt`, `abc.txt`
* `let f = "a*c.txt"; rm $f` will remove one file: `a*c.txt`.
~* `let f = "a*c.txt"; rm --glob $f` will remove `a*c.txt` and
`abc.txt`~
* `let f: glob = "a*c.txt"; rm $f` will remove `a*c.txt` and `abc.txt`
## Rules about globbing with *variable*
Given two files: `a*c.txt`, `abc.txt`
| Cmd Type | example | Result |
| ----- | ------------------ | ------ |
| builtin | let f = "a*c.txt"; rm $f | remove `a*c.txt` |
| builtin | let f: glob = "a*c.txt"; rm $f | remove `a*c.txt` and
`abc.txt`
| builtin | let f = "a*c.txt"; rm ($f \| into glob) | remove `a*c.txt`
and `abc.txt`
| custom | def crm [f: glob] { rm $f }; let f = "a*c.txt"; crm $f |
remove `a*c.txt` and `abc.txt`
| custom | def crm [f: glob] { rm ($f \| into string) }; let f =
"a*c.txt"; crm $f | remove `a*c.txt`
| custom | def crm [f: string] { rm $f }; let f = "a*c.txt"; crm $f |
remove `a*c.txt`
| custom | def crm [f: string] { rm $f }; let f = "a*c.txt"; crm ($f \|
into glob) | remove `a*c.txt` and `abc.txt`
In general, if a variable is annotated with `glob` type, nushell will
expand glob pattern. Or else, we need to use `into | glob` to expand
glob pattern
# Tests + Formatting
Done
# After Submitting
I think `str glob-escape` command will be no-longer required. We can
remove it.
2024-02-23 02:17:09 +01:00
ls ($path | path join $module_search_pattern | into glob)
2023-10-01 15:37:51 +02:00
| par-each --threads $threads {|row|
{
file: $row.name
name: ($row.name | path parse | get stem)
commands: (get-annotated $row.name)
}
2023-07-02 10:41:33 +02:00
}
2023-10-01 15:37:51 +02:00
| filter {|x| ($x.commands|length) > 0}
2023-06-10 20:16:17 +02:00
| upsert commands {|module|
2023-07-02 10:41:33 +02:00
$module.commands
| create-test-record
2023-06-10 20:16:17 +02:00
}
2023-07-02 10:41:33 +02:00
| flatten
| filter {|x| ($x.test|length) > 0}
2023-07-07 09:20:36 +02:00
| filter {|x| if ($exclude_module|is-empty) {true} else {$x.name !~ $exclude_module}}
| filter {|x| if ($test|is-empty) {true} else {$x.test|any {|y| $y =~ $test}}}
2023-06-10 20:16:17 +02:00
| filter {|x| if ($module|is-empty) {true} else {$module == $x.name}}
2023-07-02 10:41:33 +02:00
| update test {|x|
$x.test
2023-07-07 09:20:36 +02:00
| filter {|y| if ($test|is-empty) {true} else {$y =~ $test}}
| filter {|y| if ($exclude|is-empty) {true} else {$y !~ $exclude}}
2023-06-10 20:16:17 +02:00
}
)
if $list {
return $modules
}
if ($modules | is-empty) {
error make --unspanned {msg: "no test to run"}
}
let results = (
$modules
2023-07-08 11:27:56 +02:00
| par-each --threads $threads {|module|
run-tests-for-module $module $threads
2023-06-10 20:16:17 +02:00
}
| flatten
)
2023-07-02 10:41:33 +02:00
if ($results | any {|x| $x.result == fail}) {
2023-06-10 20:16:17 +02:00
let text = ([
$"(ansi purple)some tests did not pass (char lparen)see complete errors below(char rparen):(ansi reset)"
""
2023-07-08 11:27:56 +02:00
($results | par-each --threads $threads {|test| ($test | show-pretty-test 4)} | str join "\n")
2023-06-10 20:16:17 +02:00
""
] | str join "\n")
error make --unspanned { msg: $text }
}
}