Skip to content

Build with Ninja, link with mold, and run the tests once (~15% faster) - #115

Open
exercism-agent[bot] wants to merge 1 commit into
mainfrom
mold-and-ninja
Open

exercism-agent[bot] wants to merge 1 commit into
mainfrom
mold-and-ninja

Conversation

@exercism-agent

Copy link
Copy Markdown

Hello 🙂 I'm working with Claude to try and get our tooling faster.

This PR makes this project around 15% faster. I've run it in production to benchmark things in the real world against the most recent few hundred solutions, so I'm confident that it works robustly. However, I've not written the code. I have tried my hardest to get Claude to write sensible messages and comments that explain stuff, but I'd ask for your patience in the fact I'm sending AI code to you. Everything below here is from your friendly neighbourhood LLM...


This PR follows @ahans's suggestion on #114: build with Ninja and link with mold, and leave out the precompiled header. It also runs the tests once. That PR stays a draft.

What changed

Most of a run goes on compiling the solution and the test file. make compiles the two files one after the other. Ninja compiles them at the same time, so the build takes about as long as the slower of the two files.

  1. Ninja. The runner now generates the build with -G Ninja and builds with ninja. This change saves about 0.3 s from the median run.
  2. mold. The runner links with -DCMAKE_LINKER_TYPE=MOLD, as suggested in that review. mold links faster than GNU ld because it uses all cores. This change saves about 0.1 s.
  3. Tests run once. The exercises' default target builds the test executable and then runs it, and run.sh then ran it again to write the XML report. The runner now builds only the ${slug} target, so the tests run once. This saves little on the median, because most tests are fast. It cuts the slowest runs the most.

If Ninja or mold is installed, run.sh uses it. The browser runner has neither, so it keeps building with make (now make "${slug}") and linking with the default linker.

Ninja prints the compiler's output on stdout, after a FAILED: line and the full compiler command. run.sh removes those lines and Ninja's closing ninja: build stopped line, so the message keeps the compiler's own output. If a second file also fails, run.sh keeps only the first one's errors, because make stopped at the first failure.

While Catch2 writes the XML report, it holds back the tests' own output. Before, a crashing solution's message held that output, because the build had already run the tests with the console reporter. Now, if the parser reports an error and the test executable exists, run.sh runs the tests again with the console reporter and adds their error output to the message. This happens for solutions that crash or exit early.

The image grows from 584 MB to 609 MB. mold accounts for almost all of the growth.

Messages students will see change

All 101 example solutions give byte-identical results.json files. These error messages change:

  • Compile errors. The compiler's lines are the same. The three make[N]: *** [...] Error N lines at the end are gone.
  • Errors in a header. If both the solution file and the test file include a broken header, the errors usually now come from compiling the solution file (In file included from bob.cpp:5), where before they came from the test file (In file included from bob_test.cpp:1). The errors themselves are the same.
  • Link errors. These are now in mold's format (mold: error: undefined symbol: ... followed by >>> referenced by ... lines), in place of GNU ld's undefined reference to ... lines.
  • Crashes. A crash still shows the tests' error output. make's three lines are replaced by the shell's note, such as Segmentation fault (core dumped) or Aborted (core dumped).
  • exit() in a solution. If the tests exit early, the message now says The tests exited early with status 3. (with the actual status), in place of make's Error 3 lines.

The Catch2 headers in the track

This PR doesn't change the track. To measure the header change you suggested, I switched four exercises' tests from catch_all.hpp to catch_test_macros.hpp (plus the floating-point matchers header for space-age). The results stayed the same, and each run was about 0.9s to 1.0s faster with a cold cache, with or without this PR:

Exercise Main image This PR's image
two-fer 4.59s to 3.72s 4.24s to 3.18s
bob 5.11s to 4.05s 4.33s to 3.39s
binary-search-tree 5.49s to 4.53s 4.78s to 3.92s
space-age 4.40s to 3.48s 3.99s to 2.99s

This has been tested in production, and has reduced the median run from 3.4s to 2.9s.

🤖 Generated with Claude Code

Co-authored-by: iHiD <286476+iHiD@users.noreply.github.com>

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant