Repository navigation
Precompile Catch2, link with mold and run the tests once (~1.7x faster) #114
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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 "$@" |
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -43,12 +43,56 @@ cp -R "${input_dir}/" "${build_dir}" && cd "${build_dir}" | |||||
| # Replace the old Catch2 v2 include line with Catch2 v3 | ||||||
| sed -i -e 's/#include <catch2\/catch.hpp>/#include <catch2\/catch_all.hpp>/g' "${test_file_path}" | ||||||
|
|
||||||
| cmake -DEXERCISM_TEST_SUITE=1 -DEXERCISM_RUN_ALL_TESTS=1 . | ||||||
| make 2> "${compilation_errors_file_name}" | ||||||
| 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}" | ||||||
| } | ||||||
|
|
||||||
| # 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 | ||||||
|
|
||||||
| # 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" | ||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this would be cleaner:
Suggested change
In general, doing things in the |
||||||
| 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 | ||||||
| # 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 | ||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is known and something I found surprising when starting working on exercises and track maintenance. I think we could discuss if we'd want to change that. Normally, you build a project with a plain Tests are typically fast, so I never considered this an issue. But I do see that for slow solutions we pay too much. However, I don't see why it needs to be so complicated. Always doing make "${slug}" 2> "${compilation_errors_file_name}"followed by the line that produces the XML should work? All CMakeLists.txt are identical, so assuming that there is a target named |
||||||
| 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 - | ||||||
|
|
||||||
|
|
||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This sounds very fragile. I guess there's a reason why pre-compiled headers are not exactly popular ...