The "Security Tax": Reclaiming 30% Performance in WSL 2
A significant WSL2 performance issue I wrote about in march now has a better RCA and reproduction steps. You can get 50+% better build performance with kernelCommandLine=spec_rstack_overflow=off see also https://github.com/microsoft/WSL/issues/41827 Comments
For developers operating lightweight systems within WSL 2, every megabyte of RAM and CPU cycle matter greatly. However, they may not be aware of the considerable "performance tax" introduced by kernel-level countermeasures for vulnerabilities such as Spectre and Meltdown. A recent set of tests was carried out on a Go-based backend application, and disabling these safeguards led to a 31.7% improvement in wall-clock compilation time.
This article explains how to examine your system and decide if the trade-off is appropriate for your working style. Compilation tasks are particularly "syscall-heavy". Every time the compiler accesses a header file, opens a file, or links a library, it communicates with the kernel. When mitigations are enabled, thousands of these transitions cause the CPU to "sanitize" its state, leading to a slowdown in the whole process.
The most telling statistic is the reduction in System Time. By turning off mitigations, the time spent inside the kernel decreased by nearly half, enabling the CPU to devote more time to compiling code (as shown by the increase in CPU efficiency).
To check if your kernel is currently being "taxed", you can inspect the CPU vulnerabilities directory. To turn off these settings, you must modify the Windows-side configuration file located at %UserProfile%\.wslconfig. This will disable the security tax and set a strict memory limit, resulting in a snappier and more efficient environment. After saving the file, run wsl --shutdown in PowerShell for the changes to take effect.
For local development machines used for compiling languages like Go, C++, or Rust, the 30% speed increase is frequently deemed worth the calculated risk. A single line of kernel configuration can provide a performance boost comparable to $1000+ worth of hardware enhancements.
Rewritten by urgent.news; may contain errors — check the original.
Source: tonym.us