Skip to content

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

Open
exercism-agent[bot] wants to merge 4 commits into
mainfrom
mold-and-ninja
Open

exercism-agent[bot] wants to merge 4 commits into
mainfrom
mold-and-ninja

Conversation

@exercism-agent

@exercism-agent exercism-agent Bot commented Nov 29, 2026 •

Copy link
Copy Markdown

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

This PR makes this project around 12% 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, and the parser now reports a crashing test as a failed test. 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. If Ninja is installed, the runner generates the build with -G Ninja and builds with ninja. This change saves about 0.3 s from the median run.
  2. mold. If mold is installed, the runner links with -DCMAKE_LINKER_TYPE=MOLD. 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.

The browser runner has neither Ninja nor mold. It builds with make -j"$(nproc)" "${slug}" and links with the default linker. Its cmake has no --build, so run.sh calls the build tool directly. The message for a build error is now the build tool's raw output, from both stdout and stderr.

Before, a crashing test was reported as an error, with make's error lines as the message. The parser now reads the crash from Catch2's XML:

  • If a test crashes, Catch2 records the signal. That test fails with a message such as due to a fatal error condition: SIGABRT - Abort (abnormal termination) signal, and the tests that ran before it keep their results.
  • If a test throws an exception outside an assertion, it fails with due to unexpected exception with message: .... Main reported this case as an error too.
  • If the XML stops part way, the result is an error with the message "The tests stopped before they finished, so there are no test results. This can happen if the code calls exit() or crashes." This happens when the code calls exit(). It also happens for any crash in the browser runner, where Catch2 can't catch signals.

A new fixture, example-exit, covers the last case.

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 results change:

  • Compile errors. The message now holds the build tool's progress lines and, with Ninja, a FAILED: line, the full compiler command and ninja: build stopped: subcommand failed. The compiler's own lines are unchanged. make's make[N]: *** [...] Error N lines no longer appear with Ninja.
  • Errors in a header. If both the solution file and the test file include a broken header, the errors now appear once for each file, because both files compile at the same time.
  • 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 crashing test now fails with Catch2's description of the signal, and the tests before it keep their results. Before, the whole run was an error. Output that the tests printed to stderr before the crash is no longer shown.
  • Exceptions outside an assertion. The test now fails with the exception's message. Before, the whole run was an error.
  • exit() in a solution. The message now says the tests stopped before they finished, 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 3.0s.

🤖 Generated with Claude Code

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

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
On the make path in the browser, bash writes its crash note to the script's stderr, so an aborted test run left the message empty. The runner now writes its own note in both shells.

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

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Comment thread bin/run.sh Resolved
Comment thread bin/run.sh Resolved
Comment thread bin/run.sh Outdated
-e '/^ninja: build stopped: /d' \
-e p > "${compilation_errors_file_name}"
else
make "${slug}" 2> "${compilation_errors_file_name}"

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Given that we get the speedup probably mainly from parallelism and not Ninja's more efficient "makefile" structure and parsing and that it is not available in the browser, this extra complexity is probably not worth it. Instead, what about going back to make and using make -j $(nproc)?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I timed a variant with mold, the single test run and make -j"$(nproc)" "${slug}" against this branch and main. It ran over the track's 101 example solutions on a t3.medium, the instance type used in production.

main make -j Ninja
Median, cold page cache 4.80 s 4.23 s 4.10 s
Median, warm page cache 2.97 s 2.62 s 2.56 s

You're right that most of the gain comes from parallelism. Ninja is still about 0.1 s faster per exercise with a cold cache and about 0.08 s with a warm one. Each figure is the median of the per-exercise differences. Both variants give the same results as main on all 101 examples.

The two differ in compile-error messages. If a header is broken, make -j shows the errors twice (once for the solution file and once for the test file), with a make[3]: *** Waiting for unfinished jobs.... line between them. Ninja, with the sed filter, shows them once. I didn't see make -j interleave lines from the two compilers in repeated runs.

The branch keeps Ninja for now. Switching to make -j would remove the sed filter and the Ninja branch, and the browser would run the same command. It would cost about 0.1 s per run, and header errors would appear twice. Let me know if you would like me to switch.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would the output of both, Ninja and make, be sufficient for a student to know what is going on in case of failure? If yes, accepting a change in actual wording is fine. The added complexity in run.sh we could address by not invoking ninja or make manually, but instead using cmake --build . --parallel -- "${slug}". In the case of Ninja --parallel would be redundant, but it doesn't hurt either way and maybe helps the client-side runner.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@exercism-agent have a look at my comment above

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, both are enough. The filter is gone, and the message is now the build tool's raw output. For a compile error with Ninja, it looks like this:

[1/3] Building CXX object CMakeFiles/bob.dir/bob.cpp.o
FAILED: [code=1] CMakeFiles/bob.dir/bob.cpp.o 
/usr/bin/c++ -DEXERCISM_RUN_ALL_TESTS -DEXERCISM_TEST_SUITE  -g -std=c++17 -Wall -Wextra -Wpedantic -Werror -MD -MT CMakeFiles/bob.dir/bob.cpp.o -MF CMakeFiles/bob.dir/bob.cpp.o.d -o CMakeFiles/bob.dir/bob.cpp.o -c /tmp/bob/bob.cpp
/tmp/bob/bob.cpp: In function 'std::string bob::{anonymous}::trim_copy(const std::string&)':
/tmp/bob/bob.cpp:21:15: error: expected ';' before '}' token
   21 |     return cpy
      |               ^
      |               ;
   22 | }
      | ~              
[2/3] Building CXX object CMakeFiles/bob.dir/bob_test.cpp.o
ninja: build stopped: subcommand failed.

The compiler's own lines are all there and in order. The link errors (in mold's format) and the errors from a broken header read the same way. If both files include the broken header, the errors appear once for each file. The long compiler command is the only noisy line. A filter that removed it would save one line, so I left it out.

cmake --build doesn't work in the browser runner, because its cmake is built without --build ("This cmake does not support --build"). So run.sh calls the tool directly, and both outputs go to the same file:

if [ -f build.ninja ]; then
    ninja "${slug}"
else
    make -j"$(nproc)" "${slug}"
fi > "${compilation_errors_file_name}" 2>&1

The browser fixtures pass with make -j.

Comment thread bin/run.sh Outdated
# run in the build did before. Then it adds a note of how the tests ended.
# The subshell keeps the shell's own crash note out of the file, because
# busybox sh would write it there and bash would not.
if [ -f "./${slug}" ]; then

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I really dislike this extra complexity. Explain why we need this and give a concrete example for me to repro where first doing make ${slug} followed by "./${slug}" -r xml -o "${test_output_file_name}" would not give the correct output.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The rerun is there for solutions that crash or exit early. If the tests crash, Catch2 records the signal in the XML, and the parser can't read that entry. If the tests call exit(), the XML stops part way. In both cases the parser reports an error and uses the compilation errors file as the message. On main, make had already run the tests, so that file held the tests' error output and make's report of the crash. With only make "${slug}", the file is empty, so the student gets no message at all.

To reproduce it, replace bob.cpp in a copy of exercises/practice/bob with:

#include "bob.h"

#include <cstdlib>
#include <iostream>

namespace bob {

std::string hey(std::string const&) {
    std::cerr << "about to abort" << std::endl;
    std::abort();
}

}  // namespace bob

Then run this inside the runner's image, with that folder mounted at /mnt/sol (for example docker run --rm -it -v "$PWD":/mnt/sol --entrypoint sh <image>):

cp -R /mnt/sol /tmp/bob && cd /tmp/bob
sed -i -e 's/#include <catch2\/catch.hpp>/#include <catch2\/catch_all.hpp>/g' bob_test.cpp
cmake -DEXERCISM_TEST_SUITE=1 -DEXERCISM_RUN_ALL_TESTS=1 .
make bob 2> compilation-errors
./bob -r xml -o test-output.xml
/opt/test-runner/bin/exercism_parser test-output.xml results.json compilation-errors bob_test.cpp
cat results.json

With only those two commands, results.json has no message:

{"version": 3, "status": "error"}

On main:

{"version": 3, "status": "error", "message": "about to abort\nmake[2]: *** [CMakeFiles/test_bob.dir/build.make:70: CMakeFiles/test_bob] Aborted (core dumped)\nmake[1]: *** [CMakeFiles/Makefile2:122: CMakeFiles/test_bob.dir/all] Error 2\nmake: *** [Makefile:91: all] Error 2\n"}

On this branch:

{"version": 3, "status": "error", "message": "about to abort\nThe tests crashed with signal SIGABRT.\n"}

A solution that calls std::exit(3) gives the same empty message with the two commands. On this branch it gives The tests exited early with status 3.

The second run uses the console reporter because Catch2 holds back the tests' own output (here, "about to abort") while the XML reporter is active. The browser CI also depends on this. In the browser, example-empty-file links and then aborts, and its expected results need a message.

I also tried one run with both reporters (--reporter xml::out=test-output.xml --reporter console::out=-::colour-mode=none). That would remove the second run. The message would then hold Catch2's console report, which names the test that crashed and the signal. But Catch2 still holds back the tests' own output, so "about to abort" would be lost, and a solution that calls exit() would only get the "Randomness seeded to" line. Let me know if you would prefer that version.

@ahans ahans Nov 30, 2026 •

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Alright, understood. In your example, the "about to abort" message would not make it to the student with current main, because apparently only the last three lines are reported and the message is before those. With the change on the branch things would be certainly cleaner, since the (rather unhelpful) make messages would be stripped.

Now you say that the parser can't handle that case, but the XML has all required information, apart from stdout before the crash. How about changing the parser to properly handle that case too? We have three failure modes: 1) compilation errors, 2) crashing test binary, 3) test binary runs but reports test failures. On current main, 2) is treated like a compilation error. With the fixed parser, we would count it as a runtime failure, which would also be cleaner IMHO.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@exercism-agent have a look at my comment above

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done. The parser now reads the crash from the XML, and run.sh no longer runs the tests a second time.

  • If a test crashes, Catch2 records a FatalErrorCondition, and that test now fails with Catch2's own wording, for example due to a fatal error condition: SIGABRT - Abort (abnormal termination) signal. The tests that ran before it keep their results. Catch2 stops at the crash, so the tests after it don't appear.
  • If a test throws outside an assertion, Catch2 records an Exception. The test now fails with due to unexpected exception with message: .... On main the parser couldn't read this case either, and the student saw make's error lines.
  • If the code calls exit(), the XML stops part way. The result is then an error with the message "The tests stopped before they finished, so there are no test results. This can happen if the code calls exit() or crashes."
  • Top-level errors now come only from build errors, that message, and XML that the parser can't read for some other reason.

Catch2 can't catch signals in the browser runner. A crash there still cuts the XML short, so the browser shows the "stopped before they finished" message. I added an example-exit fixture. A crash fixture would give a failed test in Docker and an error in the browser, so I left that one out.

Your bob example (prints "about to abort", then calls std::abort()) now gives:

{"version": 3, "status": "fail", "tests": [{"name": "stating_something", "status": "fail", "message": "\nFAILED:\n  due to a fatal error condition:\n  SIGABRT - Abort (abnormal termination) signal\nat /tmp/bob/bob_test.cpp:10\n", "test_code": "{\n    REQUIRE(\"Whatever.\" == bob::hey(\"Tom-ay-to, tom-aaaah-to.\"));\n}"}]}

iHiD and others added 2 commits November 30, 2026 07:55
Co-authored-by: iHiD <286476+iHiD@users.noreply.github.com>

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The parser now reads Catch2's FatalErrorCondition and Exception elements, so a test that crashes or throws outside an assertion fails with Catch2's message, and the tests before it keep their results. If the XML stops part way (exit() in Docker, or any crash in the browser), the parser says so. run.sh no longer runs the tests a second time, and it keeps the raw output of Ninja or make with -j.

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

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@exercism-agent exercism-agent Bot changed the title Build with Ninja, link with mold, and run the tests once (~15% faster) Build with Ninja, link with mold, and run the tests once (~12% faster) Nov 30, 2026
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.

2 participants