r/cpp • build2 • Aug 05 '26

Faster Than Ninja

https://build2.org/blog/faster-than-ninja.xhtml
65 Upvotes

69 comments sorted by

View all comments

75

u/GrammelHupfNockler Aug 05 '26

I'm gonna be honest: The number of times I thought "gee, that ninja sure could be faster" is pretty close to 0, and in many cases that was due to slow NFS mounts. So I applaud the engineering effort, but I think the time I can spend optimizing my individual compilations is much better spent :)

53

u/donalmacc Game Developer Aug 05 '26

I think you're right, but;

Before we measure build2, let's at least acknowledge the elephant in the room: while Ninja builds the project in 3.4s, CMake takes 15.6s to generate the Ninja build files. So if you had Xerces-C++ as a dependency of your project and it was being built from scratch, you would wait 19 seconds, not 3.4, for this build.

I definitely wish Cmake was faster.

10

u/13steinj Aug 05 '26

Was this compared apples to apples to build2's configuration step?

CMake's configuration step is generally "run once, don't need changes after." As a result i don't care. The amount of times I have to rerun the configure step ler day is less than 2.

8

u/greencursordev Aug 05 '26

Adding a source file forces reconfigure

2

u/13steinj Aug 06 '26

Ah, fair, I guess most of my new files are headers (for better or worse).

3

u/berium build2 Aug 05 '26

Was this compared apples to apples to build2's configuration step?

build2 doesn't have a required configuration step, at least not analogous to CMake, which is really a project/buildfile generation step (plus generation of whatever Ninja cannot handle, like generated headers).

You can configure a build2 project, but that's just validating and saving the configuration information into a file so that you don't have to repeat it on the command line. For this project it takes 150ms.

→ More replies (2)

1

u/Nicksaurus Aug 05 '26

Do you ever switch between debug and release builds? That's the main cause of reconfigures for me

2

u/13steinj Aug 06 '26

Yes? CLion sets up a different configuration directory for debug/release if you're not using a multi config generator. If you are that's implicit/internal.

22

u/TheRavagerSw Aug 05 '26

Yeah cmake configure times are garbage
But a lot also goes to project build script quality, which is also garbage.

6

u/gharveymn Aug 05 '26

Yeah cmake configure times are garbage

IMO they really aren't, at least in comparison to Autotools. It's only really slow if you are heavily leveraging compilation tests, and those are cached for subsequent reconfigurations too.

2

u/greencursordev Aug 05 '26 edited Aug 05 '26

Changing any cml file (even just adding a source file) leads to reconfigure. That takes 3-4 minutes on our setup. That can't be far off the worst possible solution.

2

u/TheRavagerSw Aug 05 '26

Have you ever configured SDL or Qt or Boost?
It is as slow as Autotools, I compiled GCC today, it wasn't as slow as SDL configure.

5

u/arjuna93 Aug 05 '26

But… Boost doesn’t use CMake, Qt does not use CMake until 6.x, and is a horrible mess anyway, so expected to configure and build slow. What the problem with SDL configure speed with CMake?

3

u/Nicksaurus Aug 05 '26

Boost has a cmake build now and if you fetch and build it from source you end up waiting 30+ seconds to reconfigure every time you touch any part of your build script or configuration (or longer if you wipe the build folder and reconfigure from scratch, which you have to do fairly often when working on cmake scripts)

2

u/gharveymn Aug 05 '26

It's been a while but I don't remember them being horrendously bad... but those are enormous projects as well. However, I can say with absolute certainty that it isn't as slow as autotools because I have converted projects from autotools to CMake. Bash scripts are just never going to be as fast as C++.

→ More replies (2)
→ More replies (1)

5

u/_Noreturn Aug 05 '26

Why isn't cmake faster. I had configs take up to like 3 minutes although I feel like they shouldn't

11

u/kisielk Aug 05 '26

Newer versions of CMake have profiling so you could profile your build and figure out why it’s so slow

6

u/donalmacc Game Developer Aug 05 '26

CMake checks that compiles support features by compiling source files with your compiler, and it's slow. I just pulled SDL's source, and it's 16 seconds to run on my i9, most of that time is compiling to check if my compiler supports a variety of features. It's stupid.

→ More replies (4)

3

u/helloiamsomeone Aug 05 '26

People put a whole lot of garbage (like figuring out compiler flags for warnings/errors, where clang-format is, docs building, test building, etc) that has nothing to do with describing their project's build and usage requirements, so you can thank project maintainers for wasting your time. If you have tight control over your toolchain, you can preload cache variables with -C/a toolchain file and save CMake some time figuring out what the toolchain is and whether it actually works.

2

u/LB-- Professional+Hobbyist Aug 05 '26

In my experience most of the wait time is spent after all that stuff has already run though, during the part where CMake generates the new build scripts.

2

u/helloiamsomeone Aug 05 '26

I have built Google projects before where the most time was in fact spent in configure and not generation. Generation also involves graph solving, so it's a little more understandable and those are usually harder problems.

→ More replies (3)

6

u/airflow_matt Aug 05 '26

I think the benefit here is not being faster than ninja, it's being much faster than cmake + ninja. The issue here is that the cmake part doesn't run too often. But also cmake being a steaming heap of dogcrap I would welcome anything to replace it with enough traction.

2

u/crowbarous Aug 05 '26

I work on a project that generates ~25k individual .ninja files. (It's about the size of Chromium I guess? maybe a bit larger.) Every time ninja is invoked, it spends ~60s in the kernel, in one thread, reading those before it can do anything else (including determine that it's a no-op build). I have checked that it's all just pure open+read+close overhead.

I did not find any option for it to avoid it loading files that will definitely not contain anything related to the selected target, so right now I'm resorting to post-processing the .ninja files to not include things I don't care about. This part of ninja sure can be made faster.

7

u/CandyCrisis Aug 05 '26

I think you're using it in a way that it wasn't designed for? IIRC Chromium generates far fewer Ninja files.

→ More replies (3)

1

u/delta_p_delta_x Aug 05 '26

Better than Android. Change one Android.bp file? 4-minute reconfigure, oops.