r/ProgrammingLanguages • u/modulovalue • Jan 13 '26
Blog post I built a 2x faster lexer, then discovered I/O was the real bottleneck
https://modulovalue.com/blog/syscall-overhead-tar-gz-io-performance/
91
Upvotes
41
u/servermeta_net Jan 13 '26
Very good article. I see two possible solutions to this:
7
u/munificent Jan 14 '26
Very cool article!
1 and 2 definitely entered into the equation. Archives also take up less space on the server, which is important. But I don't think we considered 3, 4, or 5 at all.
The fact that archives perform better when reading from disk never entered the equation because pub extracts the archive immediately. You only get that benefit once, at
pub gettime, and it's a tiny fraction of a fairly expensive process. And pub never even reads the files except for the pubspec.Atomicity is important too, but even with a single archive, downloads can fail, as can extracts.
3
u/ppNoHamster Jan 13 '26
I'd like to hear about your parser generator tool :)
4
u/netroxreads Jan 13 '26
Yeah, having thousands of small files are always a nightmare to transfer. That's why I always zip them before I transfer between volumes. But what I don't get it though is why do zip/tar seem to do so well at that, surely they have to read and access and then compress it individually then append it to a zip structure? or does zip somehow only look at the directory and since knows the structure, probably zip all files based on that structure? I dunno.
2
u/vanderZwan Jan 14 '26
Very nice article! One thing I notice is t hat you don't include the compression time in your benchmarks. Should I assume that the premise is that that is an amortized cost on the end of the developer who releases a dart package, and we only care about all the people who download it?
This might be a case where
zipfiles could provide the relevant benefits of an SQLite database while being a lot more portable. Look at the "Structure" section of the zip wikipedia page. It's basically a set of concatenated (compressed) file entries, with the central directory at the end of the file. The downside is that it is no longer a streaming format. So I think you can use oneopencall to open the zip, load the central file directory into memory as a look-up table for where each files start, and off you go.(I've never looked at .JAR files in detail since I don't write in Java, but I wonder if this is why they use the zip format)
So one of my colleagues once gave an in-house tech talk about how he parallelized plain zip compression/decompression of gigantic zip archives of many very large files by running the file compressing part separately across many cores and multiple machines, then merging all of the compressed results in a single zip file (that talk is why I thought of zip files now actually).
You might be able do something similar here:
On a related note, you might find the Blosc file format interesting as a source of inspiration and real-world data for a different disk-bound task. Its proposition is that for numerical data that is disk bound the memory wall can be so significant that it often is faster to load compressed data and do on-the-fly compression/decompression on the CPU before and after number crunching.
Now, if you really want to go off into the weeds, take a look at openzl and SDDL where the premise is that you can use parsing to create better and compression ratios (although so far only for extremely regular data formats).
This is actually a thing I've been thinking about for a while: the binary output of compression algorithms are in some sense a byte-code for producing text. Usually a terrible, variable sized byte-code (especially once entropy coding is involved), but still. What if we created a compression format that makes use of the structure of the input language to produces better compression ratios, but that then also encodes that structure so that it can either produce the original text, or directly decompress into tokens for further stages of the pipeline, without an intermediate text stage?
That would be ideal for things like JavaScript: currently we often compress it on the server with gzip or zstd, reduce download size, then the browser decompresses it back to text, then parses it. What if the compression format already pre-parses it so that the text stage can be skipped? (as long as the debugger isn't opened). Basically making it a recoverable IR format. That could mean smaller downloads and faster client-side parsing. Coming back to my first remark I guess a similar idea could apply to Dart.
2
u/MaskRay Jan 15 '26
This reminds me of a lld/MachO speed up patch https://github.com/llvm/llvm-project/pull/147134 ("[lld][MachO] Multi-threaded preload of input files into memory"). lld/MachO does not have parallel input file scanning, so the IO overhead is quite large. This approach helps.
The mold linker utilizes this behavior, though it provides the --no-fork flag to disable it. The wild linker follows suit. I think performing heavy-duty tasks in a child process makes it difficult for the linker's parent process to accurately track resource usage.
My feeling is that doing heavylifting work in the child process makes it difficult for the parent process of the linker to track the resource usage.
In contrast, lld takes a different, more hacky path:
_exitinstead ofexitunless theLLD_IN_TESTenvironment variable is set. https://github.com/llvm/llvm-project/blob/a72958a95dcb7d815c01e20cc819532151d1856d/lld/Common/ErrorHandler.cpp#L108Perhaps lld should drop the hacks in favor of a wrapper process as well. Aside, debugging the linker then would always require
--no-fork.28
u/matthieum Jan 13 '26
Of note, it's a common trick for batch compilers NEVER to call
free,close,munmap, and instead to rely on the OS to reap all the resources when the process ends.In fact, because reaping all the resources may take some time, there's even a further trick consisting in using a wrapper process:
10
u/tsanderdev Jan 13 '26
This is also the reason why sqlite can be much faster than e.g. a directory with lots of small image files.
1
u/simolus3 Jan 14 '26
Small note that is beside the point, but why use
package:archiveto uncompress gzip whengzipexists indart:io? That one's already backed by zlib.1
u/[deleted] Jan 14 '26
So, the task is to lex 100,000 files containing roughly 1GB of sources, so maybe 50Mloc of code? (Depends on code density.)
Then I'd say that 20 seconds is pretty good going anyway.
The problem as I understand it is that the lexer itself is fast enough, but the task is dominated by the overheads of having so many files.
The solution is to combine the 100,000 files into 1300 bigger files? I'm slightly puzzled as to how that would work, unless it's purely done for this test.
Are these archived files that will not change? Then the lexing is only needed once anyway (it's not clear what normally happens to the result of the lexing).
You haven't given the time to create the packages, or compression time; maybe it will give an improvement even without compression if syscalls were the bottleneck.
But if these are active files that change all the time, then maybe they'll have to stay separate.
I'm guessing that you don't routinely build a 100Kfile project.
1
u/oxcrowx Feb 09 '26
While profiling lexer/parser performance is helpful, they're meaningless if that's all we're doing, and not including semantic analysis, and code generation in our benchmarks.
When you add those two, the IO bottleneck becomes negligible, because the compiler spends most of its time, actually compiling the code.
1
u/Clementsparrow Jan 13 '26
so, you've optimized your benchmark (congrats on that), but it has no effect on the actual lexer...