From 428573295e800598e5109aa35b74a0ef787cb8fd Mon Sep 17 00:00:00 2001 From: iHiD <286476+iHiD@users.noreply.github.com> Date: Wed, 18 Nov 2026 16:40:07 +0900 Subject: [PATCH 1/3] Run the tests once instead of twice The default make target builds the test executable and then runs it. run.sh then ran it a second time to write the XML. Building only the executable halves the time spent running tests. If the parser reports an error, run.sh runs the default target as before, so that crash messages keep make's output. Co-authored-by: iHiD <286476+iHiD@users.noreply.github.com> Co-Authored-By: Claude Opus 5.5 --- bin/run.sh | 32 ++++++++++++++++++++++++++++++-- 1 file changed, 30 insertions(+), 2 deletions(-) diff --git a/bin/run.sh b/bin/run.sh index b420940..6aa97f9 100755 --- a/bin/run.sh +++ b/bin/run.sh @@ -43,12 +43,40 @@ cp -R "${input_dir}/" "${build_dir}" && cd "${build_dir}" # Replace the old Catch2 v2 include line with Catch2 v3 sed -i -e 's/#include /#include /g' "${test_file_path}" +parse_results() { + /opt/test-runner/bin/exercism_parser "${build_dir}/${test_output_file_name}" "${results_file}" "${build_dir}/${compilation_errors_file_name}" "${test_file_path}" +} + cmake -DEXERCISM_TEST_SUITE=1 -DEXERCISM_RUN_ALL_TESTS=1 . -make 2> "${compilation_errors_file_name}" + +# Build only the test executable. The default target also runs the tests, +# so building it ran every test twice: once from make and once below to +# write the XML. Slow solutions took twice as long and timed out sooner. +# An exercise with no target named after the slug builds everything as before. +# The browser runner has no grep, so this uses sed. +built_target_only=false +if [ -n "$(sed -n "/^${slug}:/p" Makefile)" ]; then + built_target_only=true + make "${slug}" 2> "${compilation_errors_file_name}" +else + make 2> "${compilation_errors_file_name}" +fi # In case of compilation errors the executable will not be created [ -f "./${slug}" ] && chmod +x "./${slug}" && "./${slug}" -r xml -o "${test_output_file_name}" -/opt/test-runner/bin/exercism_parser "${build_dir}/${test_output_file_name}" "${results_file}" "${build_dir}/${compilation_errors_file_name}" "${test_file_path}" +parse_results + +# If the tests crashed or threw, the parser reports an error and shows the +# compilation errors file as the message. Run the default target as before, +# so that the message holds the same output from make as it did before. +if ${built_target_only} && [ -f "./${slug}" ]; then + case "$(cat "${results_file}")" in + *'"status": "error"'*) + make 2>> "${compilation_errors_file_name}" + parse_results + ;; + esac +fi cd - From 7f77f2fc2064036fc18fcca254925a49f01e55b2 Mon Sep 17 00:00:00 2001 From: iHiD <286476+iHiD@users.noreply.github.com> Date: Wed, 18 Nov 2026 16:52:43 +0900 Subject: [PATCH 2/3] Precompile Catch2's header in the Docker image Every test file includes catch2/catch_all.hpp, which took about a second to parse on every run. The image now holds a precompiled copy, and a compiler launcher makes the test file include it first. The solution's files compile unchanged. The browser runner has no precompiled header and compiles as before. Co-authored-by: iHiD <286476+iHiD@users.noreply.github.com> Co-Authored-By: Claude Opus 5.5 --- Dockerfile | 9 +++++++++ bin/compile-with-pch.sh | 25 +++++++++++++++++++++++++ bin/run.sh | 12 +++++++++++- 3 files changed, 45 insertions(+), 1 deletion(-) create mode 100755 bin/compile-with-pch.sh diff --git a/Dockerfile b/Dockerfile index 401a488..b6fa2fa 100644 --- a/Dockerfile +++ b/Dockerfile @@ -12,6 +12,15 @@ RUN git clone https://github.com/catchorg/Catch2.git --depth 1 --branch v3.4.0 & cd .. && \ rm -rf Catch2 +# Precompile Catch2's catch_all.hpp, which every test file includes. Parsing it +# takes about a second per run, and loading the precompiled copy takes about +# 0.1 s. GCC only uses the precompiled copy when the compiler options match, so +# these are the options used by the exercises' CMakeLists.txt in Debug mode. +# -Wpedantic matters because it changes which macros GCC defines. +# bin/compile-with-pch.sh makes the test file include it first. +RUN c++ -std=c++17 -g -Wall -Wextra -Wpedantic -Werror -x c++-header \ + /usr/local/include/catch2/catch_all.hpp -o /usr/local/include/catch2/catch_all.hpp.gch + WORKDIR /opt/test-runner COPY . . RUN g++ -I ./include ./src/*.cpp -o ./bin/exercism_parser -lboost_system -lboost_json diff --git a/bin/compile-with-pch.sh b/bin/compile-with-pch.sh new file mode 100755 index 0000000..74281b3 --- /dev/null +++ b/bin/compile-with-pch.sh @@ -0,0 +1,25 @@ +#!/usr/bin/env sh + +# Synopsis: +# Compile one file with the C++ compiler. CMake calls this script in place of +# the compiler (see CMAKE_CXX_COMPILER_LAUNCHER in run.sh) and passes the full +# compiler command as arguments. + +# The test file includes Catch2's catch_all.hpp, which takes about a second to +# parse. The Docker image holds a precompiled copy of that header, built in +# the Dockerfile. GCC only uses a precompiled header if it is +# the first thing included, and the test file includes the solution first. +# So for the test file, this script includes Catch2 before anything else. +# The test file's own include of Catch2 then does nothing. +# +# The solution's files are compiled unchanged. If a precompiled header does not +# match the compiler options, GCC ignores it and parses the header as before. + +# run.sh sets EXERCISM_TEST_FILE to the test file's path. CMake passes the +# source file as the last argument. +for last_arg; do :; done + +if [ "${last_arg}" = "${EXERCISM_TEST_FILE}" ]; then + exec "$@" -include catch2/catch_all.hpp +fi +exec "$@" diff --git a/bin/run.sh b/bin/run.sh index 6aa97f9..4b53c16 100755 --- a/bin/run.sh +++ b/bin/run.sh @@ -47,7 +47,17 @@ parse_results() { /opt/test-runner/bin/exercism_parser "${build_dir}/${test_output_file_name}" "${results_file}" "${build_dir}/${compilation_errors_file_name}" "${test_file_path}" } -cmake -DEXERCISM_TEST_SUITE=1 -DEXERCISM_RUN_ALL_TESTS=1 . +# The Docker image holds a precompiled copy of Catch2's header, which saves +# about a second on every compile of the test file. compile-with-pch.sh makes +# the test file use it. The browser runner has no precompiled header, so it +# compiles as before. +cmake_options="-DEXERCISM_TEST_SUITE=1 -DEXERCISM_RUN_ALL_TESTS=1" +if [ -e /usr/local/include/catch2/catch_all.hpp.gch ]; then + export EXERCISM_TEST_FILE="${test_file_path}" + cmake_options="${cmake_options} -DCMAKE_CXX_COMPILER_LAUNCHER=/opt/test-runner/bin/compile-with-pch.sh" +fi + +cmake ${cmake_options} . # Build only the test executable. The default target also runs the tests, # so building it ran every test twice: once from make and once below to From 402f528027a9aa29ea1d0e62639f372cce120229 Mon Sep 17 00:00:00 2001 From: iHiD <286476+iHiD@users.noreply.github.com> Date: Wed, 18 Nov 2026 17:01:24 +0900 Subject: [PATCH 3/3] Link with mold GNU ld took about 0.3 s to link each test executable, and mold takes about 0.06 s. run.sh only asks for mold when it is installed, so the browser runner links as before. Co-authored-by: iHiD <286476+iHiD@users.noreply.github.com> Co-Authored-By: Claude Opus 5.5 --- Dockerfile | 2 +- bin/run.sh | 6 ++++++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/Dockerfile b/Dockerfile index b6fa2fa..8178b65 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,6 +1,6 @@ FROM alpine:3.24.3@sha256:be2656061b536c1883c4034790e21d0a09c5aaaf51968b89368a3c0371ea2c9a -RUN apk add --no-cache coreutils g++ libc-dev cmake make git boost-dev +RUN apk add --no-cache coreutils g++ libc-dev cmake make git boost-dev mold # Build Catch as a library directly inside the Docker image # since it takes a bit of time to compile it,this way we speed diff --git a/bin/run.sh b/bin/run.sh index 4b53c16..1fe92cf 100755 --- a/bin/run.sh +++ b/bin/run.sh @@ -57,6 +57,12 @@ if [ -e /usr/local/include/catch2/catch_all.hpp.gch ]; then cmake_options="${cmake_options} -DCMAKE_CXX_COMPILER_LAUNCHER=/opt/test-runner/bin/compile-with-pch.sh" fi +# mold links the test executable in about 0.06 s, against about 0.3 s for +# GNU ld, because it uses all cores and does less work per symbol. +if command -v mold > /dev/null; then + cmake_options="${cmake_options} -DCMAKE_EXE_LINKER_FLAGS=-fuse-ld=mold" +fi + cmake ${cmake_options} . # Build only the test executable. The default target also runs the tests,