Skip to content

Commit cba48f1

Browse files
authored
Merge pull request #563 from AztecProtocol/cb/bb-faster-clearer-cleanups
chore(bb): faster-and-clearer C++ cleanups (reserve / const-ref / std::move / bulk-insert)
2 parents fab82cd + cfd9366 commit cba48f1

33 files changed

Lines changed: 84 additions & 98 deletions

barretenberg/cpp/src/barretenberg/commitment_schemes/gemini/gemini.hpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,8 +95,9 @@ template <class Fr> inline std::vector<Fr> powers_of_rho(const Fr& rho, const si
9595
*/
9696
template <class Fr> inline std::vector<Fr> powers_of_evaluation_challenge(const Fr& r, const size_t num_squares)
9797
{
98-
std::vector<Fr> squares = { r };
98+
std::vector<Fr> squares;
9999
squares.reserve(num_squares);
100+
squares.emplace_back(r);
100101
for (size_t j = 1; j < num_squares; j++) {
101102
squares.emplace_back(squares[j - 1].sqr());
102103
}

barretenberg/cpp/src/barretenberg/commitment_schemes/gemini/gemini_impl.hpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -184,15 +184,15 @@ std::vector<typename GeminiProver_<Curve>::Polynomial> GeminiProver_<Curve>::com
184184
const Fr final_eval = last.at(0) + u_last * (last.at(1) - last.at(0));
185185
Polynomial const_fold(1);
186186
const_fold.at(0) = final_eval;
187-
fold_polynomials.emplace_back(const_fold);
187+
fold_polynomials.emplace_back(std::move(const_fold));
188188

189189
// FOLD_{log_n+1}, ..., FOLD_{d_v-1}
190190
Fr tail = Fr(1);
191191
for (size_t k = log_n; k < virtual_log_n - 1; ++k) {
192192
tail *= (Fr(1) - multilinear_challenge[k]); // multiply by (1 - u_k)
193193
Polynomial next_const(1);
194194
next_const.at(0) = final_eval * tail;
195-
fold_polynomials.emplace_back(next_const);
195+
fold_polynomials.emplace_back(std::move(next_const));
196196
}
197197

198198
return fold_polynomials;
@@ -228,6 +228,7 @@ std::vector<typename GeminiProver_<Curve>::Claim> GeminiProver_<Curve>::construc
228228
const Fr& r_challenge)
229229
{
230230
std::vector<Claim> claims;
231+
claims.reserve(log_n + 1);
231232

232233
// Compute evaluation of partially evaluated batch polynomial (positive) A₀₊(r)
233234
Fr a_0_pos = A_0_pos.evaluate(r_challenge);

barretenberg/cpp/src/barretenberg/commitment_schemes/shplonk/shplemini.hpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -371,7 +371,9 @@ template <typename Curve, bool HasZK = false, bool HasGeminiMasking = HasZK> cla
371371
commitments.emplace_back(g1_identity);
372372
scalars.emplace_back(constant_term_accumulator);
373373

374-
BatchOpeningClaim<Curve> batch_opening_claim{ commitments, scalars, shplonk_evaluation_challenge };
374+
BatchOpeningClaim<Curve> batch_opening_claim{ std::move(commitments),
375+
std::move(scalars),
376+
shplonk_evaluation_challenge };
375377
ShpleminiVerifierOutput output = [&]() {
376378
if constexpr (HasZK) {
377379
return ShpleminiVerifierOutput{ batch_opening_claim, consistency_checked };
@@ -617,7 +619,8 @@ template <typename Curve, bool HasZK = false, bool HasGeminiMasking = HasZK> cla
617619
const std::vector<std::array<Fr, 3>>& sumcheck_round_evaluations)
618620
{
619621

620-
std::vector<Fr> denominators = {};
622+
std::vector<Fr> denominators;
623+
denominators.reserve(multilinear_challenge.size());
621624

622625
// The number of Gemini claims is equal to `2 * log_n` and `log_n` is equal to the size of
623626
// `multilinear_challenge`, as this method is never used with padding.

barretenberg/cpp/src/barretenberg/common/map.hpp

Lines changed: 0 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -35,31 +35,4 @@ std::array<OutElem, SIZE> map(std::array<InElem, SIZE> const& in, F&& op)
3535
return result;
3636
}
3737

38-
/*
39-
* Generic map function for mapping a containers element to another type.
40-
* This version passes the element index as a second argument to the operator function.
41-
*/
42-
template <template <typename, typename...> typename Cont,
43-
typename InElem,
44-
typename... Args,
45-
typename F,
46-
typename OutElem = typename std::invoke_result<F, InElem const&, size_t>::type>
47-
Cont<OutElem> mapi(Cont<InElem, Args...> const& in, F op)
48-
{
49-
Cont<OutElem> result;
50-
for (size_t i = 0; i < in.size(); ++i) {
51-
result.push_back(op(in[i], i));
52-
}
53-
return result;
54-
}
55-
56-
/*
57-
* Generic filter function for containers.
58-
*/
59-
template <typename Cont, typename F> Cont filter(Cont const& in, F op)
60-
{
61-
Cont copy(in);
62-
std::remove_if(copy.begin(), copy.end(), op);
63-
return copy;
64-
}
6538
} // namespace bb::transform

barretenberg/cpp/src/barretenberg/common/ref_vector.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ template <typename T> class RefVector {
6363
static RefVector from_span(const std::span<T>& span)
6464
{
6565
RefVector ret;
66+
ret.storage.reserve(span.size());
6667
for (std::size_t i = 0; i < span.size(); ++i) {
6768
ret.push_back(span[i]);
6869
}
@@ -124,6 +125,7 @@ template <typename T> class RefVector {
124125
template <typename ConvertibleFromT> operator std::vector<ConvertibleFromT>() const
125126
{
126127
std::vector<ConvertibleFromT> ret;
128+
ret.reserve(storage.size());
127129
for (T* elem : storage) {
128130
ret.push_back(*elem);
129131
}

barretenberg/cpp/src/barretenberg/common/serialize.hpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -448,6 +448,7 @@ template <typename T> std::vector<T> many_from_buffer(std::vector<uint8_t> const
448448
{
449449
const size_t num_elements = buffer.size() / sizeof(T);
450450
std::vector<T> elements;
451+
elements.reserve(num_elements);
451452
for (size_t i = 0; i < num_elements; ++i) {
452453
elements.push_back(from_buffer<T>(buffer, i * sizeof(T)));
453454
}
@@ -462,7 +463,7 @@ template <bool include_size = false, typename T> std::vector<uint8_t> to_buffer(
462463
if (include_size) {
463464
write(buf, value);
464465
} else {
465-
for (auto e : value) {
466+
for (const auto& e : value) {
466467
write(buf, e);
467468
}
468469
}

barretenberg/cpp/src/barretenberg/common/thread.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include <barretenberg/env/hardware_concurrency.hpp>
55
#include <cstdlib>
66
#include <string>
7+
#include <utility>
78

89
#ifndef NO_MULTITHREADING
910
#include <thread>
@@ -217,7 +218,7 @@ MultithreadData calculate_thread_data(size_t num_iterations, size_t min_iteratio
217218
end[thread_idx] = (thread_idx == num_threads - 1) ? num_iterations : (thread_idx + 1) * thread_size;
218219
}
219220

220-
return MultithreadData{ num_threads, start, end };
221+
return MultithreadData{ num_threads, std::move(start), std::move(end) };
221222
}
222223

223224
/**

barretenberg/cpp/src/barretenberg/common/utils.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ namespace bb::utils {
55
std::vector<uint8_t> hex_to_bytes(const std::string& hex)
66
{
77
std::vector<uint8_t> bytes;
8+
bytes.reserve(hex.length() / 2);
89

910
for (unsigned int i = 0; i < hex.length(); i += 2) {
1011
std::string byteString = hex.substr(i, 2);

barretenberg/cpp/src/barretenberg/crypto/pedersen_commitment/pedersen.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ namespace bb::crypto {
2222
*/
2323
template <typename Curve>
2424
typename Curve::AffineElement pedersen_commitment_base<Curve>::commit_native(const std::vector<Fq>& inputs,
25-
const GeneratorContext context)
25+
const GeneratorContext& context)
2626
{
2727
const auto generators = context.generators->get(inputs.size(), context.offset, context.domain_separator);
2828
Element result = Group::point_at_infinity;

barretenberg/cpp/src/barretenberg/crypto/pedersen_commitment/pedersen.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ template <typename Curve> class pedersen_commitment_base {
3131
using Group = typename Curve::Group;
3232
using GeneratorContext = typename crypto::GeneratorContext<Curve>;
3333

34-
static AffineElement commit_native(const std::vector<Fq>& inputs, GeneratorContext context = {});
34+
static AffineElement commit_native(const std::vector<Fq>& inputs, const GeneratorContext& context = {});
3535
};
3636

3737
using pedersen_commitment = pedersen_commitment_base<curve::Grumpkin>;

0 commit comments

Comments
 (0)