NewsCrawler.org

What it takes to count the Linux kernel's 36M lines of code in under 70ms

The article describes mezura, a program that counts lines of code, and how its time on the Linux kernel v6.15 fell from about 0.4 s to 67 ms. The tree holds about 36 million lines in roughly 63,700 source files totaling 1.37 GB, and the program reads them from the warm page cache[1] at roughly 20 GB/s. The tests ran on an 8-core Ryzen 7 9700X desktop with Debian 13 and ext4.

To see how far the program could go, the author wrote a bare C version that only walks directories and reads files, and on this tree it could not get much below 41 to 42 ms. That left the 88 ms version mostly in its own code. Profiles misled in places: a flamegraph[2] shows where time is spent, not how much is saved by deleting code, and a change that cut branch misses[3] by 14% made the program 12% slower. The author then replaced byte loops with branchless word-at-a-time (SWAR[4]) arithmetic, which added about 75 extra instructions per line.

On Linux the 67 ms run used 0.62 s of CPU time in user code and 0.28 s in the kernel, while the same run on Windows 11 with NTFS took 175 ms and spent more than half its CPU time in the kernel. To avoid extra metadata calls, the author uses the fact that FindNextFileW already returns the file size on Windows, so a buffer of size + 1 bytes reads a whole file and confirms its end in one read call.

Read the original petros-papatheodorou.devDiscussion

Discussion

Commenters are split on the piece. One asked why the problem needs solving at all, and another suggested cutting the story of how the author got there in favour of the results. One commenter would rather accept a few more milliseconds than a crash when files change mid-scan, and questioned the rule for files modified in the last five seconds; the author replied with links to the repository and benchmarks.

Sources

Published: First seen: Читать по-русски

Open as a page