Mini Project, Part 2: Unity Build, ccache & the Scorecard
Part 1 measured SlowGame and added a precompiled header. This part reduces the number of compiles with a unity build, makes clean rebuilds nearly free with ccache, and ends with a scorecard of every configuration, all measured in one deterministic session.
Each session starts by recreating the project with the same generator as in part 1:
mkdir -p slowgame/src && cd slowgame
cat > src/common.hpp <<'H'
#pragma once
#include <algorithm>
#include <map>
#include <regex>
#include <string>
#include <vector>
H
systems="audio input physics render net ui save script anim ai fx world camera loader config log"
for s in $systems; do cat > src/$s.cpp <<SRC
#include "common.hpp"
namespace { int weight(const std::string& name) { return int(name.size()); } }
int ${s}_update() {
std::map<std::string, std::vector<int>> data{{"$s", {3, 1, 2}}};
std::sort(data["$s"].begin(), data["$s"].end());
return weight("$s") + (std::regex_match("$s", std::regex("[a-z]+")) ? 1 : 0);
}
SRC
done
{ for s in $systems; do echo "int ${s}_update();"; done
echo 'int main() { int total = 0;'
for s in $systems; do echo " total += ${s}_update();"; done
echo ' return total > 0 ? 0 : 1; }'; } > src/main.cpp
cat > CMakeLists.txt <<'CM'
cmake_minimum_required(VERSION 3.25)
project(SlowGame CXX)
option(USE_PCH "precompile the heavy standard headers" OFF)
file(GLOB SRCS CONFIGURE_DEPENDS src/*.cpp)
add_executable(slowgame ${SRCS})
set_target_properties(slowgame PROPERTIES UNITY_BUILD_UNIQUE_ID UNITY_ID)
if(USE_PCH)
target_precompile_headers(slowgame PRIVATE <algorithm> <map> <regex> <string> <vector>)
endif()
CMStep 2: a unity build, and its first pitfall
Seventeen files at the default batch size of 8 should become three unity TUs. Try it:
mkdir -p slowgame/src && cd slowgame
cat > src/common.hpp <<'H'
#pragma once
#include <algorithm>
#include <map>
#include <regex>
#include <string>
#include <vector>
H
systems="audio input physics render net ui save script anim ai fx world camera loader config log"
for s in $systems; do cat > src/$s.cpp <<SRC
#include "common.hpp"
namespace { int weight(const std::string& name) { return int(name.size()); } }
int ${s}_update() {
std::map<std::string, std::vector<int>> data{{"$s", {3, 1, 2}}};
std::sort(data["$s"].begin(), data["$s"].end());
return weight("$s") + (std::regex_match("$s", std::regex("[a-z]+")) ? 1 : 0);
}
SRC
done
{ for s in $systems; do echo "int ${s}_update();"; done
echo 'int main() { int total = 0;'
for s in $systems; do echo " total += ${s}_update();"; done
echo ' return total > 0 ? 0 : 1; }'; } > src/main.cpp
cat > CMakeLists.txt <<'CM'
cmake_minimum_required(VERSION 3.25)
project(SlowGame CXX)
option(USE_PCH "precompile the heavy standard headers" OFF)
file(GLOB SRCS CONFIGURE_DEPENDS src/*.cpp)
add_executable(slowgame ${SRCS})
set_target_properties(slowgame PROPERTIES UNITY_BUILD_UNIQUE_ID UNITY_ID)
if(USE_PCH)
target_precompile_headers(slowgame PRIVATE <algorithm> <map> <regex> <string> <vector>)
endif()
CM
cmake -S . -B unity -G Ninja -DCMAKE_UNITY_BUILD=ON > /dev/null
cmake --build unity 2>&1 | grep -m2 -E 'error|note' | sed 's#/home/learner/slowgame/##'src/net.cpp:2:17: error: redefinition of ‘int {anonymous}::weight(const std::string&)’
src/log.cpp:2:17: note: ‘int {anonymous}::weight(const std::string&)’ previously defined here
Every subsystem has its own namespace { int weight(…) }, which is fine in separate TUs and a redefinition in a unity TU (Unity Build Pitfalls). The CMake target already sets UNITY_BUILD_UNIQUE_ID UNITY_ID, so the fix is the documented pattern: wrap the helper in namespace UNITY_ID, call it as UNITY_ID::weight, and define UNITY_ID as empty for normal builds:
mkdir -p slowgame/src && cd slowgame
cat > src/common.hpp <<'H'
#pragma once
#include <algorithm>
#include <map>
#include <regex>
#include <string>
#include <vector>
H
systems="audio input physics render net ui save script anim ai fx world camera loader config log"
for s in $systems; do cat > src/$s.cpp <<SRC
#include "common.hpp"
namespace { int weight(const std::string& name) { return int(name.size()); } }
int ${s}_update() {
std::map<std::string, std::vector<int>> data{{"$s", {3, 1, 2}}};
std::sort(data["$s"].begin(), data["$s"].end());
return weight("$s") + (std::regex_match("$s", std::regex("[a-z]+")) ? 1 : 0);
}
SRC
done
{ for s in $systems; do echo "int ${s}_update();"; done
echo 'int main() { int total = 0;'
for s in $systems; do echo " total += ${s}_update();"; done
echo ' return total > 0 ? 0 : 1; }'; } > src/main.cpp
cat > CMakeLists.txt <<'CM'
cmake_minimum_required(VERSION 3.25)
project(SlowGame CXX)
option(USE_PCH "precompile the heavy standard headers" OFF)
file(GLOB SRCS CONFIGURE_DEPENDS src/*.cpp)
add_executable(slowgame ${SRCS})
set_target_properties(slowgame PROPERTIES UNITY_BUILD_UNIQUE_ID UNITY_ID)
if(USE_PCH)
target_precompile_headers(slowgame PRIVATE <algorithm> <map> <regex> <string> <vector>)
endif()
CM
for f in src/*.cpp; do
[ "$f" = src/main.cpp ] && continue
sed -i -e '1a #ifndef UNITY_ID\n#define UNITY_ID\n#endif' \
-e 's/namespace { int weight/namespace { namespace UNITY_ID { int weight/' \
-e 's/size()); } }/size()); } } }/' \
-e 's/return weight(/return UNITY_ID::weight(/' "$f"
done
sed -n '1,6p' src/audio.cpp
for unity in OFF ON; do
cmake -S . -B u-$unity -G Ninja -DCMAKE_UNITY_BUILD=$unity -DCMAKE_CXX_FLAGS=-H > /dev/null
cmake --build u-$unity > u-$unity.log 2>&1 && ./u-$unity/slowgame
echo "UNITY=$unity: $(grep -c 'Building CXX' u-$unity.log) compiles, $(grep -c '^\.' u-$unity.log) header opens"
done
ls u-ON/CMakeFiles/slowgame.dir/Unity/ | grep -c 'cxx