与原生 Windows NPM/Yarn 处理相比,为什么 WSL 非常慢?

Why is WSL extremely slow when compared with native Windows NPM/Yarn processing?

我最近经常使用 WSL,因为我需要一些原生 UNIX 工具(而且模拟器还不够好)。我注意到使用 NPM/Yarn 时的速度差异令人难以置信。

我进行了一个简单的测试,证实了我的感受。测试是 运行 npx create-react-app my-test-app 并且 WSL 结果是 Done in 287.56s. 而 GitBash 以 Done in 10.46s..

结束

这不是全部情况,因为在这两种情况下感知时间都更长,但即使基于此 - 某处也存在大问题。我只是不知道在哪里。我正在从事的项目使用了数十个库,即使更改其中一个库也需要几分钟而不是几秒钟。

这是我可以解决的问题吗?如果是这样 - 去哪里寻找线索?

附加信息:

既然你提到从 Git Bash 中执行相同的文件(具有适当的性能),我将在这里做一个假设。如果我在这方面有误,请纠正我,我会删除答案并寻找另一种可能性。

如果您的文件存储在 /mnt/c(a.k.a。C:,或 Git /C Bash) 或任何其他 Windows 驱动器,因为它们可能需要被 Git Bash.

访问

WSL2 使用 9P 协议访问 Windows 驱动器,目前已知 非常 与以下相比:

  • 本机 NTFS(显然)
  • WSL2 使用的虚拟磁盘上的 ext4 文件系统
  • 甚至 Windows 驱动器的 WSL1 的性能

我看到一个 git clone 的大型仓库(WSL2 Linux 内核 Git 集线器)在 Windows 驱动器上的 WSL2 上需要 8 分钟,但是在根文件系统上只有几秒钟。

两种可能性:

  • 如果可能(对于大多数 Node 项目而言),请使用 wsl --set-version <distroname> 1 将 WSL 转换为版本 1。我总是建议首先使用 wsl --export 进行备份。

    并且由于您无论如何都要进行备份,因此您也可以通过 wsl --import 将备份创建为 --version 1 来创建实例的 副本 (作为最后一个参数)。 WSL1 和 WSL2 都有各自的用途,您可能会发现将两者都放在身边会有所帮助。

    有关确切语法的更多详细信息,请参阅

  • 或者将项目移动到 WSL 根目录下的某个位置,例如 /home/username/src/.

基于@notthedr01ds 的回复。

如果您查看 Microsoft 的 Comparing WSL 1 and WSL 2,“跨 OS 文件系统的性能”在 WSL2 中明显更差。

我的case陷入了Exceptions for using WSL 1 rather than WSL 2

  • Your project files must be stored in the Windows file system. WSL 1 offers faster access to files mounted from Windows.
    • If you will be using your WSL Linux distribution to access project files on the Windows file system, and these files cannot be stored on the Linux file system, you will achieve faster performance across the OS files systems by using WSL 1.

这意味着我需要切换到版本 1。

wsl --set-version Ubuntu 1
Conversion in progress, this may take a few minutes...
Conversion complete.

之前测试

>time git status
...
real    0m6.436s
user    0m0.055s
sys     0m36.380s

之后测试
> time git status
...
real    0m0.126s
user    0m0.016s
sys     0m0.641s