Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I am working with WSL a lot lately because I need some native UNIX tools (and emulators aren't good enough). I noticed that the speed difference when working with NPM/Yarn is incredible.

I conducted a simple test that confirmed my feelings. The test was running npx create-react-app my-test-app and the WSL result was Done in 287.56s. while GitBash finished with Done in 10.46s..

This is not the whole picture, because the perceived time was higher in both cases, but even based on that - there is a big issue somewhere. I just don't know where. The project I'm working on uses tens of libraries and changing even one of them takes minutes instead of seconds.

Is this something that I can fix? If so - where to look for clues?

Additional info:

  • my processor: Processor AMD Ryzen 7 5800H with Radeon Graphics, 3201 Mhz, 8 Core(s), 16 Logical Processors

  • I'm running Windows 11 with all the latest updates to both the system and the WSL. The chosen system is Ubuntu 20.04

  • I've seen some questions that are somewhat similar like 'npm install' extremely slow on Windows, but they don't touch WSL at all (and my pure Windows NPM works fast).

  • the issue is not limited to NPM, it's also for Yarn

  • another problem that I'm getting is that file watching is not happening (I need to restart the server with every change). In some applications I don't get any errors, sometimes I get the following:

    Watchpack Error (initial scan): Error: EACCES: permission denied, lstat '/mnt/c/DumpStack.log.tmp'
    Watchpack Error (initial scan): Error: EACCES: permission denied, lstat '/mnt/c/hiberfil.sys'
    Watchpack Error (initial scan): Error: EACCES: permission denied, lstat '/mnt/c/pagefile.sys'
    Watchpack Error (initial scan): Error: EACCES: permission denied, lstat '/mnt/c/swapfile.sys'
    
  • npm start in an empty (freshly initialized) create-react-app takes ages to render something in the browser in WSL and when executed from GitBash - I can see stuff in 2-4 seconds

  • it is possible that's it's purely a WSL problem, but it just hurts the most when using NPM/Yarn

like image 260
WrRaThY Avatar asked Aug 29 '21 11:08

WrRaThY


People also ask

Is WSL faster than Windows?

File performance across the Windows and Linux operating systems is faster in WSL 1 than WSL 2, so if you are using Windows applications to access Linux files, you will currently achieve faster performance with WSL 1.

Is WSL2 slower than WSL1?

WSL2 is more than 2 times faster than WSL1.


2 Answers

Since you mention executing the same files (with proper performance) from within Git Bash, I'm going to make an assumption here. Correct me if I'm wrong on this, and I'll delete the answer and look for another possibility.

This would be explained (and expected) if your files are stored on /mnt/c (a.k.a. C:, or /C under Git Bash) or any other Windows drive, as they would likely need to be to be accessed by Git Bash.

WSL2 uses the 9P protocol to access Windows drives, and it is currently known to be very slow when compared to:

  • Native NTFS (obviously)
  • The ext4 filesystem on the virtual disk used by WSL2
  • And even the performance of WSL1 with Windows drives

I've seen a git clone of a large repo (the WSL2 Linux kernel Github) take 8 minutes on WSL2 on a Windows drive, but only seconds on the root filesystem.

Two possibilities:

  • If possible (and it is for most Node projects), convert your WSL to version 1 with wsl --set-version <distroname> 1. I always recommend making a backup with wsl --export first.

    And since you are making a backup anyway, you may as well just create a copy of the instance by wsl --importing your backup as --version 1 (as the last argument). WSL1 and WSL2 both have their uses, and you may find it helpful to keep both around.

    See this answer for more details on the exact syntax..

  • Or just move the project over to somewhere under the WSL root, such as /home/username/src/.

like image 116
NotTheDr01ds Avatar answered Jan 31 '23 12:01

NotTheDr01ds


Building on @notthedr01ds's reponse.

If you look at Microsoft's Comparing WSL 1 and WSL 2 the 'Performance across the OS file systems' is explicitly worse in WSL2.

Comparison of WS1 and WSL2 from Comparing WSL 1 and WSL 2

My case fell into 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.

This means I needed to swap to version 1.

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

Test before

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

Test after

> time git status
...
real    0m0.126s
user    0m0.016s
sys     0m0.641s
like image 31
tymtam Avatar answered Jan 31 '23 10:01

tymtam