Skip to content

Commit db34cbc

Browse files
committed
perf: reduce barretenberg compile overhead
1 parent d20e61a commit db34cbc

28 files changed

Lines changed: 178 additions & 125 deletions

barretenberg/cpp/bootstrap.sh

Lines changed: 83 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -123,21 +123,37 @@ function write_gtest_inventory {
123123
local inventory=$build_dir/.aztec-gtest-tests
124124
local tmp=$inventory.tmp
125125
local ctest_file
126-
local ctest_base
127-
local bin_name
126+
local ctest_files=()
128127

129128
[ -d "$build_dir" ] || return
130-
: > "$tmp"
131-
while IFS='|' read -r bin_name ctest_file; do
132-
list_tests_from_ctest "$ctest_file" | while read -r test; do
133-
printf '%s\t%s\n' "$bin_name" "$test"
134-
done
135-
done < <(
136-
while IFS= read -r ctest_file; do
137-
ctest_base=${ctest_file##*/}
138-
printf '%s|%s\n' "${ctest_base%%\[*}" "$ctest_file"
139-
done < <(find "$build_dir" -type f -name '*_tests[[]*[]]_tests.cmake') | sort
140-
) >> "$tmp"
129+
while IFS= read -r -d '' ctest_file; do
130+
ctest_files+=("$ctest_file")
131+
done < <(find "$build_dir" -type f -name '*_tests[[]*[]]_tests.cmake' -print0 | sort -z)
132+
133+
if [ "${#ctest_files[@]}" -gt 0 ]; then
134+
awk '
135+
FNR == 1 {
136+
bin = FILENAME
137+
sub(/^.*\//, "", bin)
138+
sub(/\[.*/, "", bin)
139+
}
140+
/--gtest_filter=/ {
141+
line = $0
142+
while (match(line, /--gtest_filter=/)) {
143+
line = substr(line, RSTART + RLENGTH)
144+
test = line
145+
sub(/\].*/, "", test)
146+
sub(/[" )].*/, "", test)
147+
if (test != "" && test !~ /DISABLED_/) {
148+
print bin "\t" test
149+
}
150+
line = substr(line, length(test) + 1)
151+
}
152+
}
153+
' "${ctest_files[@]}" > "$tmp"
154+
else
155+
: > "$tmp"
156+
fi
141157

142158
if [ -s "$tmp" ]; then
143159
mv "$tmp" "$inventory"
@@ -314,61 +330,67 @@ function build {
314330
fi
315331
}
316332

333+
function emit_native_test_cmd {
334+
local bin_name=$1
335+
local test=$2
336+
337+
# Skip heavy recursion tests in debug builds — they take 400-600s+ and the same
338+
# code paths are already exercised (with assertions) by faster tests in the suite.
339+
# Keep WithoutPredicate/1.GenerateVKFromConstraints (241s) so that the debug-only
340+
# native_verification_debug path in honk_recursion_constraint.cpp is still exercised.
341+
# None of the other skipped suites exercise unique debug-only (#ifndef NDEBUG) code paths.
342+
if [[ "$native_preset" == *debug* ]] && [[ "$test" =~ ^(HonkRecursionConstraintTest|ChonkRecursionConstraintTest|AvmRecursionInnerCircuitTests|AvmRecursionConstraintTest|AvmRecursiveTests\.TwoLayer|PaddingVariants/AvmRecursiveTestsParameterized\.TwoLayer|BoomerangTwoLayerAvmRecursiveVerifierTests|ECCVMRecursiveTests|GoblinRecursiveVerifierTests|GoblinAvmRecursiveVerifierTests|BoomerangGoblinRecursiveVerifierTests|BoomerangGoblinAvmRecursiveVerifierTests) ]]; then
343+
if [[ "$test" != "HonkRecursionConstraintTestWithoutPredicate/1.GenerateVKFromConstraints" ]]; then
344+
return
345+
fi
346+
fi
347+
348+
local prefix=$hash
349+
# A little extra resource for these tests.
350+
# IPARecursiveTests fails with 2 threads.
351+
if [[ "$test" =~ ^(AcirAvmRecursionConstraint|ChonkKernelCapacity|AvmRecursiveTests|IPARecursiveTests|HonkRecursionConstraintTest|ChonkRecursionConstraintTest) ]]; then
352+
prefix="$prefix:CPUS=4:MEM=8g"
353+
fi
354+
echo -e "$prefix barretenberg/cpp/scripts/run_test.sh $bin_name $test"
355+
}
356+
317357
function test_cmds_native {
318358
# E.g. build, build-debug or build-coverage
319359
cd $native_build_dir
320360

321-
declare -A inventory_tests=()
322-
local inv_bin
323-
local inv_test
324-
if [ -f .aztec-gtest-tests ]; then
325-
while IFS=$'\t' read -r inv_bin inv_test; do
326-
inventory_tests["$inv_bin"]+="$inv_test"$'\n'
361+
local bin_name
362+
local test
363+
if [ -s .aztec-gtest-tests ]; then
364+
while IFS=$'\t' read -r bin_name test; do
365+
[ -n "$bin_name" ] && [ -n "$test" ] || continue
366+
emit_native_test_cmd "$bin_name" "$test"
327367
done < .aztec-gtest-tests
328-
fi
329-
330-
declare -A discovered_tests=()
331-
local ctest_file
332-
local ctest_base
333-
while IFS= read -r ctest_file; do
334-
ctest_base=${ctest_file##*/}
335-
discovered_tests["${ctest_base%%\[*}"]=$ctest_file
336-
done < <(find . -type f -name '*_tests[[]*[]]_tests.cmake')
337-
338-
for bin in ./bin/*_tests; do
339-
local bin_name=${bin##*/}
340-
341-
{
342-
if [ -n "${inventory_tests[$bin_name]:-}" ]; then
343-
printf '%s' "${inventory_tests[$bin_name]}"
344-
elif [ -n "${discovered_tests[$bin_name]:-}" ]; then
345-
list_tests_from_ctest "${discovered_tests[$bin_name]}"
346-
else
347-
$bin --gtest_list_tests | \
348-
awk '/^[a-zA-Z]/ {suite=$1} /^[ ]/ {print suite$1}' | \
349-
grep -v 'DISABLED_'
350-
fi
351-
} | \
352-
while read -r test; do
353-
# Skip heavy recursion tests in debug builds — they take 400-600s+ and the same
354-
# code paths are already exercised (with assertions) by faster tests in the suite.
355-
# Keep WithoutPredicate/1.GenerateVKFromConstraints (241s) so that the debug-only
356-
# native_verification_debug path in honk_recursion_constraint.cpp is still exercised.
357-
# None of the other skipped suites exercise unique debug-only (#ifndef NDEBUG) code paths.
358-
if [[ "$native_preset" == *debug* ]] && [[ "$test" =~ ^(HonkRecursionConstraintTest|ChonkRecursionConstraintTest|AvmRecursionInnerCircuitTests|AvmRecursionConstraintTest|AvmRecursiveTests\.TwoLayer|PaddingVariants/AvmRecursiveTestsParameterized\.TwoLayer|BoomerangTwoLayerAvmRecursiveVerifierTests|ECCVMRecursiveTests|GoblinRecursiveVerifierTests|GoblinAvmRecursiveVerifierTests|BoomerangGoblinRecursiveVerifierTests|BoomerangGoblinAvmRecursiveVerifierTests) ]]; then
359-
if [[ "$test" != "HonkRecursionConstraintTestWithoutPredicate/1.GenerateVKFromConstraints" ]]; then
360-
continue
361-
fi
362-
fi
363-
local prefix=$hash
364-
# A little extra resource for these tests.
365-
# IPARecursiveTests fails with 2 threads.
366-
if [[ "$test" =~ ^(AcirAvmRecursionConstraint|ChonkKernelCapacity|AvmRecursiveTests|IPARecursiveTests|HonkRecursionConstraintTest|ChonkRecursionConstraintTest) ]]; then
367-
prefix="$prefix:CPUS=4:MEM=8g"
368+
else
369+
declare -A discovered_tests=()
370+
local bin
371+
local ctest_file
372+
local ctest_base
373+
while IFS= read -r ctest_file; do
374+
ctest_base=${ctest_file##*/}
375+
discovered_tests["${ctest_base%%\[*}"]=$ctest_file
376+
done < <(find . -type f -name '*_tests[[]*[]]_tests.cmake')
377+
378+
for bin in ./bin/*_tests; do
379+
bin_name=${bin##*/}
380+
381+
{
382+
if [ -n "${discovered_tests[$bin_name]:-}" ]; then
383+
list_tests_from_ctest "${discovered_tests[$bin_name]}"
384+
else
385+
$bin --gtest_list_tests | \
386+
awk '/^[a-zA-Z]/ {suite=$1} /^[ ]/ {print suite$1}' | \
387+
grep -v 'DISABLED_'
368388
fi
369-
echo -e "$prefix barretenberg/cpp/scripts/run_test.sh $bin_name $test"
389+
} | while read -r test; do
390+
emit_native_test_cmd "$bin_name" "$test"
370391
done || (echo "Failed to list tests in $bin" && exit 1)
371-
done
392+
done
393+
fi
372394

373395
# The pinned IVC inputs / VKs live in the public repo; the private fork
374396
# carries divergent circuits so the check is expected to fail there.

barretenberg/cpp/src/barretenberg/bbapi/bbapi_chonk.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,8 @@ ChonkLoad::Response ChonkLoad::execute(BBApiRequest& request) &&
4747
}
4848

4949
request.loaded_circuit_name = circuit.name;
50-
request.loaded_circuit_constraints = acir_format::circuit_buf_to_acir_format(std::move(circuit.bytecode));
50+
request.loaded_circuit_constraints =
51+
std::make_shared<acir_format::AcirFormat>(acir_format::circuit_buf_to_acir_format(std::move(circuit.bytecode)));
5152
request.loaded_circuit_vk = circuit.verification_key;
5253

5354
info("ChonkLoad - loaded circuit '", request.loaded_circuit_name, "'");
@@ -62,16 +63,15 @@ ChonkAccumulate::Response ChonkAccumulate::execute(BBApiRequest& request) &&
6263
throw_or_abort("Chonk not started. Call ChonkStart first.");
6364
}
6465

65-
if (!request.loaded_circuit_constraints.has_value()) {
66+
if (!request.loaded_circuit_constraints) {
6667
throw_or_abort("No circuit loaded. Call ChonkLoad first.");
6768
}
6869

6970
acir_format::WitnessVector witness_data = acir_format::witness_buf_to_witness_vector(std::move(witness));
70-
acir_format::AcirProgram program{ std::move(request.loaded_circuit_constraints.value()), std::move(witness_data) };
71+
acir_format::AcirProgram program{ std::move(*request.loaded_circuit_constraints), std::move(witness_data) };
7172

7273
// Clear loaded state immediately after moving out of it. This ensures that if any subsequent
73-
// step throws, the request won't appear to still have a valid circuit loaded (the optional
74-
// would be in a moved-from state, which is technically has_value()==true but poisoned).
74+
// step throws, the request won't appear to still have a valid circuit loaded.
7575
auto loaded_vk = std::move(request.loaded_circuit_vk);
7676
auto circuit_name = std::move(request.loaded_circuit_name);
7777
request.loaded_circuit_constraints.reset();

barretenberg/cpp/src/barretenberg/bbapi/bbapi_chonk.hpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,14 @@
88
* and the batch verifier service (start/queue/stop lifecycle).
99
*/
1010
#include "barretenberg/bbapi/bbapi_shared.hpp"
11-
#include "barretenberg/chonk/chonk.hpp"
11+
#include "barretenberg/chonk/chonk_proof.hpp"
1212
#include "barretenberg/common/named_union.hpp"
1313
#include "barretenberg/honk/proof_system/types/proof.hpp"
1414
#include "barretenberg/serialize/msgpack.hpp"
1515

1616
#ifndef __wasm__
1717
#include "barretenberg/chonk/batch_verifier_types.hpp"
1818
#include "barretenberg/chonk/chonk_batch_verifier.hpp"
19-
#include "barretenberg/chonk/chonk_proof.hpp"
2019
#include <condition_variable>
2120
#include <mutex>
2221
#include <queue>

barretenberg/cpp/src/barretenberg/bbapi/bbapi_execute.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,24 @@
11
#include "bbapi_execute.hpp"
22

33
namespace bb::bbapi {
4+
CommandResponse execute(BBApiRequest& request, Command&& command)
5+
{
6+
// Reset error state before execution
7+
request.error_message.clear();
8+
9+
CommandResponse response = std::move(command).visit([&request](auto&& cmd) -> CommandResponse {
10+
using CmdType = std::decay_t<decltype(cmd)>;
11+
return std::forward<CmdType>(cmd).execute(request);
12+
});
13+
14+
// Check if an error occurred during execution
15+
if (!request.error_message.empty()) {
16+
return ErrorResponse{ .message = std::move(request.error_message) };
17+
}
18+
19+
return response;
20+
}
21+
422
namespace { // anonymous
523
struct Api {
624
Command commands;

barretenberg/cpp/src/barretenberg/bbapi/bbapi_execute.hpp

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
#include "barretenberg/bbapi/bbapi_crypto.hpp"
66
#include "barretenberg/bbapi/bbapi_ecc.hpp"
77
#include "barretenberg/bbapi/bbapi_ecdsa.hpp"
8-
#include "barretenberg/bbapi/bbapi_schnorr.hpp"
98
#include "barretenberg/bbapi/bbapi_schema.hpp"
9+
#include "barretenberg/bbapi/bbapi_schnorr.hpp"
1010
#include "barretenberg/bbapi/bbapi_shared.hpp"
1111
#include "barretenberg/bbapi/bbapi_srs.hpp"
1212
#include "barretenberg/bbapi/bbapi_ultra_honk.hpp"
@@ -149,22 +149,6 @@ using CommandResponse = NamedUnion<ErrorResponse,
149149
* @param request The circuit registry (acting as the request context).
150150
* @return A variant of all possible command responses.
151151
*/
152-
inline CommandResponse execute(BBApiRequest& request, Command&& command)
153-
{
154-
// Reset error state before execution
155-
request.error_message.clear();
156-
157-
CommandResponse response = std::move(command).visit([&request](auto&& cmd) -> CommandResponse {
158-
using CmdType = std::decay_t<decltype(cmd)>;
159-
return std::forward<CmdType>(cmd).execute(request);
160-
});
161-
162-
// Check if an error occurred during execution
163-
if (!request.error_message.empty()) {
164-
return ErrorResponse{ .message = std::move(request.error_message) };
165-
}
166-
167-
return response;
168-
}
152+
CommandResponse execute(BBApiRequest& request, Command&& command);
169153

170154
} // namespace bb::bbapi

barretenberg/cpp/src/barretenberg/bbapi/bbapi_shared.hpp

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,24 +7,32 @@
77
* including circuit input types and proof system settings.
88
*/
99

10-
#include "barretenberg/chonk/chonk.hpp"
1110
#include "barretenberg/common/throw_or_abort.hpp"
12-
#include "barretenberg/dsl/acir_format/acir_format.hpp"
1311
#include "barretenberg/flavor/ultra_flavor.hpp"
1412
#include "barretenberg/flavor/ultra_keccak_flavor.hpp"
1513
#include "barretenberg/flavor/ultra_keccak_zk_flavor.hpp"
1614
#include "barretenberg/flavor/ultra_zk_flavor.hpp"
1715
#include "barretenberg/honk/execution_trace/mega_execution_trace.hpp"
18-
#include "barretenberg/stdlib/special_public_inputs/special_public_inputs.hpp"
16+
#include "barretenberg/special_public_inputs/special_public_inputs.hpp"
1917
#include "barretenberg/stdlib_circuit_builders/ultra_circuit_builder.hpp"
2018
#ifdef STARKNET_GARAGA_FLAVORS
2119
#include "barretenberg/flavor/ultra_starknet_flavor.hpp"
2220
#include "barretenberg/flavor/ultra_starknet_zk_flavor.hpp"
2321
#endif
2422
#include <cstdint>
23+
#include <memory>
2524
#include <string>
25+
#include <type_traits>
2626
#include <vector>
2727

28+
namespace acir_format {
29+
struct AcirFormat;
30+
}
31+
32+
namespace bb {
33+
class IVCBase;
34+
}
35+
2836
namespace bb::bbapi {
2937

3038
/**
@@ -182,7 +190,7 @@ struct BBApiRequest {
182190
// Name of the last loaded circuit
183191
std::string loaded_circuit_name;
184192
// Store the parsed constraint system to get ahead of parsing before accumulate
185-
std::optional<acir_format::AcirFormat> loaded_circuit_constraints;
193+
std::shared_ptr<acir_format::AcirFormat> loaded_circuit_constraints;
186194
// Store the verification key passed with the circuit
187195
std::vector<uint8_t> loaded_circuit_vk;
188196
// Policy for handling verification keys during accumulation
Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
#pragma once
2-
#include "barretenberg/circuit_checker/ultra_circuit_checker.hpp"
3-
#include "barretenberg/stdlib_circuit_builders/mega_circuit_builder.hpp"
4-
#include "barretenberg/stdlib_circuit_builders/ultra_circuit_builder.hpp"
2+
#include "barretenberg/stdlib/primitives/circuit_builders/circuit_builders_fwd.hpp"
53

64
namespace bb {
75

@@ -11,8 +9,8 @@ namespace bb {
119
*/
1210
class CircuitChecker {
1311
public:
14-
static bool check(const UltraCircuitBuilder& builder) { return UltraCircuitChecker::check(builder); }
15-
static bool check(const MegaCircuitBuilder& builder) { return UltraCircuitChecker::check(builder); }
12+
static bool check(const UltraCircuitBuilder& builder);
13+
static bool check(const MegaCircuitBuilder& builder);
1614
};
1715

1816
} // namespace bb

barretenberg/cpp/src/barretenberg/circuit_checker/ultra_circuit_checker.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,22 @@
11
#include "ultra_circuit_checker.hpp"
2+
#include "barretenberg/circuit_checker/circuit_checker.hpp"
23
#include "barretenberg/common/assert.hpp"
34
#include "barretenberg/flavor/mega_flavor.hpp"
45
#include "barretenberg/stdlib/primitives/circuit_builders/circuit_builders.hpp"
56
#include <unordered_set>
67

78
namespace bb {
89

10+
bool CircuitChecker::check(const UltraCircuitBuilder& builder)
11+
{
12+
return UltraCircuitChecker::check(builder);
13+
}
14+
15+
bool CircuitChecker::check(const MegaCircuitBuilder& builder)
16+
{
17+
return UltraCircuitChecker::check(builder);
18+
}
19+
920
template <> auto UltraCircuitChecker::init_empty_values<UltraCircuitBuilder_<UltraExecutionTraceBlocks>>()
1021
{
1122
return UltraFlavor::AllValues{};

barretenberg/cpp/src/barretenberg/ultra_honk/mega_honk.test.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
#include <cstdint>
33
#include <gtest/gtest.h>
44

5-
#include "barretenberg/circuit_checker/circuit_checker.hpp"
65
#include "barretenberg/common/log.hpp"
76
#include "barretenberg/goblin/mock_circuits.hpp"
87
#include "barretenberg/honk/proof_length.hpp"

barretenberg/cpp/src/barretenberg/ultra_honk/permutation.test.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
#include "barretenberg/circuit_checker/circuit_checker.hpp"
21
#include "barretenberg/honk/library/grand_product_delta.hpp"
32
#include "barretenberg/honk/library/grand_product_library.hpp"
43
#include "barretenberg/honk/relation_checker.hpp"

0 commit comments

Comments
 (0)