Skip to content

Add virtual path abstraction layer#9245

Merged
kubouch merged 24 commits intonushell:mainfrom
kubouch:stdlib-loading
May 23, 2023
Merged

Add virtual path abstraction layer#9245
kubouch merged 24 commits intonushell:mainfrom
kubouch:stdlib-loading

Conversation

@kubouch
Copy link
Copy Markdown
Contributor

@kubouch kubouch commented May 19, 2023

Description

Adds the possibility to embed "virtual" files and directories into Nushell so that loading them does not require real physical paths on the file system.

The primary use case is loading the standard library. First, all the stdlib files are loaded as virtual files in a made-up NU_STDLIB_VIRTUAL_DIR directory: NU_STDLIB_VIRTUAL_DIR/std/mod.nu, NU_STDLIB_VIRTUAL_DIR/std/dirs.nu, etc. Then, when loading the stdlib, the parser's current working directory is set to the virtual directory. When parsing statements such as use dirs.nu, the parser looks into the virtual directories and finds the path there without it being present in the file system.

The virtual paths can be used in the future for other Nu code we might want to ship with Nushell, such as package manager.

One future use case for this could be "compiling" Nushell projects so that they can run on other machines without having to install their dependencies and requiring all source files to be on the file system. You could think of it as static linking.

Seems like loading stdlib from virtual paths improves the startup times by ~50 ms, too.

Limitations

source and source-env currently load the virtual paths in the parser but they fail during evaluation. We'd need to add the support for virtual files to the engine in

pub fn find_in_dirs_env(

User-Facing Changes

Faster stdlib loading

Tests + Formatting

After Submitting

@kubouch kubouch changed the title Add virtual file abstraction layer Add virtual path abstraction layer May 21, 2023
@amtoine
Copy link
Copy Markdown
Member

amtoine commented May 22, 2023

i have a drastic improvement with the startup time 😱 👀

cargo run -- -n gives me around

  • 850ms on latest main
  • 280ms with this PR

@kubouch
Copy link
Copy Markdown
Contributor Author

kubouch commented May 22, 2023

Wow, that's a lot. I'll try to finish it so we can merge it.

@kubouch
Copy link
Copy Markdown
Contributor Author

kubouch commented May 22, 2023

Startup times on my machine (Linux):

  • cargo run --release -- -n: ~15 ms
  • nu -n: 42-44 ms

@amtoine
Copy link
Copy Markdown
Member

amtoine commented May 23, 2023

after more attempts

  • nu: ~260ms
  • nu -n: ~85ms
  • cargo run --release on nushell:main@db4b26c: ~265ms
  • cargo run --release -- -n on nushell:main@db4b26c: ~85ms

and now the real meat 😏

  • cargo run --release on kubouch:stdlib-loading: ~180ms
  • cargo run --release -- -n on kubouch:stdlib-loading: ~30ms

very nice 👌

@kubouch kubouch marked this pull request as ready for review May 23, 2023 20:09
@kubouch kubouch merged commit 74724de into nushell:main May 23, 2023
@kubouch kubouch deleted the stdlib-loading branch May 23, 2023 20:48
@kubouch kubouch restored the stdlib-loading branch May 23, 2023 20:53
amtoine added a commit to amtoine/nushell that referenced this pull request May 24, 2023
This should solve the merge conflicts in nushell#9253 introduced by nushell#9245.
amtoine added a commit to amtoine/nu-git-manager that referenced this pull request May 26, 2023
amtoine added a commit to amtoine/nupm that referenced this pull request May 26, 2023
@cablehead
Copy link
Copy Markdown
Contributor

I've just added the ability to cross.stream to append Nushell modules to the stream. As you do, the modules are registered in the base Nushell engine's vfs. Then any handler etc running on cross.stream can use these modules!

Thanks @kubouch for adding this feature, which made that possible 🙏

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants