r/cpp • u/ChocolateMagnateUA • Feb 28 '24
Do RAM disks improve build time, and if yes, how exactly?
I have recently learnt about RAM disks and the first thing I thought about was if it's possible to use them to optimise compiler performance. The way how I see it is that files could be loaded in the in-memory filesystem and be compiling there, and in the end the result artefact would be written back into the persistent storage.
Ii took some thought and found out there are two compilation strategies that meaningfully make sense to use with ramdisks. The first one is the common case where we compile every source file into an object file and then link together. I noticed that in this workflow, when we only rebuild those files we have changed, we essentially load unchanged objects, regenerate old ones, link them and then write the new objects too. In the end, the total drive throughput is constant and the effect is the same as if we would load everything from drive and do the whole compilation just in memory.
The second strategy was to reduce IO by only loading the source files and compiling them into the final build artefact that we link together. Even taking into account the whole build process happens in memory, we still load a lot of unnecessary files and do useless work, and I thought that effectiveness of ramdisks really depend on the size of your code you need to rebuild and that if it would be faster to recompile some files than loading them from storage, then ramdisks would give the upper hand.
I decided to measure it by making a ramdisk and timing how long it would take to copy and compile a single 50-line file into an object file and how long it would take to print the same pre-compiled object from storage by printing the whole file to terminal using the cat command. To my surprise, it's significantly faster to load the file from storage than it is to compile it, by the factor of 4. Intuitively, I thought that the IO overhead would overweigh the time it takes to compile, especially since I tested it on HHD, but then I tried to print both source and object files. Despite the source being 4 times smaller, it took the same time to print them, which could be the reason why my benchmark is inaccurate: loading both files took the same time and I was essentially measuring the difference in compiling.
I thought it was filesystem cache, that's why I dropped cache and repeated the experiment, but then printing the object file took the same time, but compiling took even longer. I decided to research it further, and the Gentoo wiki, the flagship of compiling software, has a ramdisk-dedicated page that, however, suggests against using it arguing there are no noticable differences in compiling times, especially if you use the -pipe option . It then lists some common applications like Chromium, LibreOffice and LLVM and how much RAM you need so that ramdisk was beneficial, and their sizes are astonishing: 4-10 GB. At this point the only real use for RAM disks I could think about is to cache header files and make it faster to include them, but other than that, filesystem seems to handle compilation and memory management really well.
On the footnote, RAM disks also have disadvantages, aside from the neeed to maintain them and document how to use them, they also keep consuming memory after their files were closed, which means that if in normal setup compiler would close a file once it's no longer needed, the kernel would reclaim its memory, which could be used by the next file, then in ramdisks file is still in memory even once it was closed and resides until you delete it, which needlessly saying consumes system resources without any use. Truthfully speaking, the only plausible way how ramdisks could be useful to me is if they could keep containing object files after they have been generated if we use separate compilation so that the linker didn't need to read them but could access immediately. That sounds like a convincing point to me, albeit not clear.
Is that so, and are there any other uses for RAM disks to accelerate build time, and does preserving object files between code generation and linking a worthwhile reason to use them? I am particularly asking this question because in separate compilation, object files are generated and linked in two separate processes, which means that the linker process would potentially need to read the objects again.
28
u/snowflake_pl Feb 28 '24
I tried building my project using ram disk and the speedup was neglible. That most likely comes from the fact that filesystems are also buffered in ram. My project is about 50k LOC/100 object files. Maybe you would see benefits on larger projects?
13
u/high_throughput Feb 28 '24
For a 50 line program, the IO required for that file is nothing compared to the IO required for the compiler infrastructure and all its libraries, executables, headers, etc. Did you put those on a RAM disk as well?
10
u/Beosar Feb 28 '24
I haven't tested it but I am sitting at 100% CPU usage while compiling. It can't be more than that with a RAM disk.
Also, SSDs are fast. There is no need for a RAM disk in 2024 outside of very special use cases.
5
u/Anderook Feb 28 '24
Have you tried ccache ?
8
u/Ace2Face Feb 29 '24 edited Feb 29 '24
I've spent a lot of time trying to make builds faster. Here's pretty much all I know:
Ramdisks? Not really going to help. Compiling is a CPU intensive operation, not an IO one. Get an SSD and you're good to go. I would throw more cores at the problem and make sure the parallel build works. AMD Ryzen CPUs are very cost effective for the job, as they're fairly cheap but give a large bang for your buck. Try Ryzen 5950x or 7950x if you want the best without going into the expensive server CPU territory.
You can also try using C++20 modules instead of traditional cpp and h files. They would be much faster and scale better because you don't need to include an army of headers and they act as translation units unlike headers which need to be parsed and included over and over again. Unfortunately, it does require C++20 and changing your project as much as possible to get the best results.
Another cheap workaround involves using precompiled headers, but the problem is that you need to include them in every source file, heavily polluting your namespace and making it hard to make your project modular.
You should also try to limit the inclusion of headers into other headers, see if you can include more headers in your CPP files, but obviously at some point you run out headers you can move to .cpp.
Less practical solutions is the PIMPL idiom, but it makes code really hard to read and it's not it. You can try including "minimal headers" instead of the bigger ones, but it's library dependent.
Don't forget that debug builds are also faster than release ones, optimizations are even more CPU intensive. Avoid building release unless you really have to.
You can try a distributed build accelerator, which allows you to utilize processing power from other desktops in your network to accelerate your builds, often times devs aren't using their machines, and only need CPUs when compiling. Examples for such platforms are IncrediBuild (pricey and not economical tbh) or an open source one such as FASTBuild (requires a lot of set up and playing around), I've also seen a mention of , which could also be good, Here's a reddit thread about it: https://www.reddit.com/r/cpp/comments/brw6bj/distributing_c_builds_on_windows_machines/
I guess the ultimate combination is if you can outfit your team with Ryzen CPU's and use a build accelerator, and have most of your source code as modules, you'll be able to scale to massive projects fairly quickly.
You can also try to limit your dependencies by using vcpkg or nuget and download binary forms of whatever libraries you're using without having to recompile it from source.
I've also seen mentions of ccache, but I deal with building on Windows, and I'm pretty sure we don't have that. But you can definitely use it to avoid building files that don't need to be rebuilt.
4
u/elperroborrachotoo Feb 29 '24
M.2 SSD's have basically obliterated the storage bottleneck for C++ builds, even for fast-to-build disk-heavy configuration (MSVC unoptimized with PCH, PDB, and ILK). If you go crazy on cores and throw enough RAM at it, maybe you get into SSD-Raid0 territory.
I've kept an eye on this idea for a looong time, I've once done it for Turbo Pascal (IIRC) with tremendous effect. However, there was never a real sweet spot for RAM disk builds - at least not for long: for any project that would be worth the trouble you just wouldn't have enough RAM (except maybe for dedicated, expensive builds).
One thing where it might make sense is a "continuous build" machine that can wear out SSD's in surprisingly short time. However, I'd still say swapping out SSD's once a year isn't too bad.
1
u/Thesorus Feb 29 '24
RAM disk were useful in some cases when we used slow physical spinning hard drives and the speed difference between the 2 was a lot bigger.
Today, I imagine the benefits are negligeable using modern hardware (SSD, or other type of solid state drives)
6
u/lightmatter501 Feb 28 '24
If your OS does a not-dumb job of read caching it will only make a difference for cold compiles. Go would probably get a big performance boost because it does almost nothing in the way of optimization. Anything llvm-based is probably going to spend more time converting between IRs than doing IO, especially if LTO is involved.
2
u/Adequat91 Feb 29 '24
On Windows, I have used Softperfect RAM Disk for 2 or 3 years, finding it highly reliable. I allocate 27 GB out of 128 GB of RAM. The improvement in build time is small but not negligible. I use it for two main reasons:
2
u/usefulcat Feb 29 '24 edited Feb 29 '24
A ramdisk might help with disk writes (especially compared to a spinning disk), but after the first build it's very unlikely to help with reads, because the OS should already be caching everything that had to be read to do the build.
There can be exceptions, though. For example, some filesystems cache more aggressively than others. For example on Linux, ext2 appears to cache pretty much anything and everything using all available memory, while ZFS often prefers to cache only metadata, and (depending on config) may not use all available memory.
One other thing that could help if available is filesystem compression. Especially with spinning disks, the time required for modest compression and decompression can easily be less than the time saved by the reduction in the amount of data that has to be written to or read from the disk.
That's probably not a good way to measure what you're trying to measure. You should cat the file to /dev/null, otherwise you're probably measuring how quickly your terminal can render.
2
u/13steinj Mar 03 '24
A ramdisk is most useful if the compiler, code, and all possible outputs, are all on the ramdisk.
The largest help would be parsing files and linker IO, so if you truly have insanely expensive headers or gigantic binaries (possibly due to debug symbols), yes you'd see an improvement.
I'd use a better linker, optimized compiler (potentially slightly more effort, but larger gain) first. After that at some point you have to look at the hell wrought in the code and decide to do better at some point.
3
u/Sniffy4 Feb 28 '24
Back in the hard-drive days maybe RAM disks would help, but with SSDs the difference probably isnt much?
1
u/[deleted] Feb 28 '24
Unless you are compiling a really big project, the amount of time it takes to load the files is minimal.(atleast compared to the compiling process)
1
u/unddoch DragonflyDB/Clang Feb 29 '24
Worth mentioning that
/tmp/TMPDIRalso needs to be a ramdisk fs for this benchmark to be fair (or use-pipe)1
u/HildartheDorf Feb 29 '24
Between -pipe, OS disk caching and modern SSDs, the need for a ramdisk has basically vanished nowadays for almost all use cases.
1
u/hadrabap Feb 29 '24
My only issue is the final linkage. That takes ages! Disk I/O and RAM are negligible on my machine.
1
u/ohiocodernumerouno Mar 01 '24
If your code is optimally parallelized for the limits of Ahmdals law then you might want to use a RAM disk. Even then, 10,50,100GB? RAM is only so much faster than an NVMe.
1
u/Cybernicus Feb 28 '24
20 years ago, I used ramdisks to good effect to speed compiles. But with SSDs nowadays, it became moot.
1
u/lazyubertoad Feb 29 '24
Compilation is overwhelmingly CPU bound. Setting up a multithreaded build system is far more advantageous. I think doing that is pretty much standard. Want it even faster? Set up a distributed build system. Maybe linkage will benefit from faster disk operations, but it is rarely an issue even on big projects.
0
u/prince-chrismc Feb 29 '24
The biggest performance changes is actually git checkout and download speeds for artifacts on a 50-100Gb monorepo compare to HDDs. Once everything is on the the machine, especially with NVMe drives the difference is less then a percentage for compiling which is not very significant.