Skip to content

Commit

Permalink
Don't copy when flattening leaves
Browse files Browse the repository at this point in the history
  • Loading branch information
j-berman committed Aug 3, 2024
1 parent cbf6a5d commit 30fc80b
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 11 deletions.
2 changes: 1 addition & 1 deletion src/blockchain_db/lmdb/db_lmdb.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2038,7 +2038,7 @@ bool BlockchainLMDB::audit_tree(const uint64_t expected_n_leaf_tuples) const
throw0(DB_ERROR(lmdb_error("Failed to get parent in first layer: ", result).c_str()));

// Get the expected leaf chunk hash
const auto leaves = m_curve_trees->flatten_leaves(leaf_tuples_chunk);
const auto leaves = m_curve_trees->flatten_leaves(std::move(leaf_tuples_chunk));
const fcmp::curve_trees::Selene::Chunk chunk{leaves.data(), leaves.size()};

// Hash the chunk of leaves
Expand Down
11 changes: 5 additions & 6 deletions src/fcmp/curve_trees.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -677,17 +677,16 @@ CurveTrees<Helios, Selene>::LeafTuple CurveTrees<Helios, Selene>::leaf_tuple(
};
//----------------------------------------------------------------------------------------------------------------------
template<typename C1, typename C2>
std::vector<typename C2::Scalar> CurveTrees<C1, C2>::flatten_leaves(const std::vector<LeafTuple> &leaves) const
std::vector<typename C2::Scalar> CurveTrees<C1, C2>::flatten_leaves(std::vector<LeafTuple> &&leaves) const
{
std::vector<typename C2::Scalar> flattened_leaves;
flattened_leaves.reserve(leaves.size() * LEAF_TUPLE_SIZE);

for (const auto &l : leaves)
for (auto &l : leaves)
{
// TODO: implement without cloning
flattened_leaves.emplace_back(l.O_x);
flattened_leaves.emplace_back(l.I_x);
flattened_leaves.emplace_back(l.C_x);
flattened_leaves.emplace_back(std::move(l.O_x));
flattened_leaves.emplace_back(std::move(l.I_x));
flattened_leaves.emplace_back(std::move(l.C_x));
}

return flattened_leaves;
Expand Down
4 changes: 2 additions & 2 deletions src/fcmp/curve_trees.h
Original file line number Diff line number Diff line change
Expand Up @@ -237,8 +237,8 @@ class CurveTrees

LeafTuple leaf_tuple(const PreprocessedLeafTuple &preprocessed_leaf_tuple) const;

// Flatten leaves [(O.x, I.x, C.x),(O.x, I.x, C.x),...] -> [scalar,scalar,scalar,scalar,scalar,scalar,...]
std::vector<typename C2::Scalar> flatten_leaves(const std::vector<LeafTuple> &leaves) const;
// Flatten leaves [(O.x, I.x, C.x),(O.x, I.x, C.x),...] -> [O.x, I.x, C.x, O.x, I.x, C.x...]
std::vector<typename C2::Scalar> flatten_leaves(std::vector<LeafTuple> &&leaves) const;

// Convert cryptonote tx outs to contexts ready to be converted to leaf tuples, grouped by unlock height
void tx_outs_to_leaf_tuple_contexts(const cryptonote::transaction &tx,
Expand Down
4 changes: 2 additions & 2 deletions tests/unit_tests/curve_trees.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -495,7 +495,7 @@ bool CurveTreesGlobalTree::audit_tree(const std::size_t expected_n_leaf_tuples)
{
MDEBUG("Auditing global tree");

const auto &leaves = m_tree.leaves;
auto leaves = m_tree.leaves;
const auto &c1_layers = m_tree.c1_layers;
const auto &c2_layers = m_tree.c2_layers;

Expand Down Expand Up @@ -582,7 +582,7 @@ bool CurveTreesGlobalTree::audit_tree(const std::size_t expected_n_leaf_tuples)
// Now validate leaves
return validate_layer<Selene>(m_curve_trees.m_c2,
c2_layers[0],
m_curve_trees.flatten_leaves(leaves),
m_curve_trees.flatten_leaves(std::move(leaves)),
m_curve_trees.m_leaf_layer_chunk_width);
}
//----------------------------------------------------------------------------------------------------------------------
Expand Down

0 comments on commit 30fc80b

Please sign in to comment.