Skip to content

Commit c1e7fe2

Browse files
authored
Prevent unnecessary shared_ptr copies by accepting a value in SHAMapInnerNode::setChild (#4266)
* Do a move instead of a copy in `SHAMapInnerNode::setChild` * Create the value directly in the call
1 parent 4a5ad4c commit c1e7fe2

File tree

3 files changed

+6
-7
lines changed

3 files changed

+6
-7
lines changed

src/ripple/shamap/SHAMapInnerNode.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ class SHAMapInnerNode final : public SHAMapTreeNode,
147147
getChildHash(int m) const;
148148

149149
void
150-
setChild(int m, std::shared_ptr<SHAMapTreeNode> const& child);
150+
setChild(int m, std::shared_ptr<SHAMapTreeNode> child);
151151

152152
void
153153
shareChild(int m, std::shared_ptr<SHAMapTreeNode> const& child);

src/ripple/shamap/impl/SHAMap.cpp

+3-4
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ SHAMap::dirtyUp(
118118
assert(branch >= 0);
119119

120120
node = unshareNode(std::move(node), nodeID);
121-
node->setChild(branch, child);
121+
node->setChild(branch, std::move(child));
122122

123123
child = std::move(node);
124124
}
@@ -718,7 +718,7 @@ SHAMap::delItem(uint256 const& id)
718718
stack.pop();
719719

720720
node = unshareNode(std::move(node), nodeID);
721-
node->setChild(selectBranch(nodeID, id), prevNode);
721+
node->setChild(selectBranch(nodeID, id), std::move(prevNode));
722722

723723
if (!nodeID.isRoot())
724724
{
@@ -795,8 +795,7 @@ SHAMap::addGiveItem(SHAMapNodeType type, std::shared_ptr<SHAMapItem const> item)
795795
auto inner = std::static_pointer_cast<SHAMapInnerNode>(node);
796796
int branch = selectBranch(nodeID, tag);
797797
assert(inner->isEmptyBranch(branch));
798-
auto newNode = makeTypedLeaf(type, std::move(item), cowid_);
799-
inner->setChild(branch, newNode);
798+
inner->setChild(branch, makeTypedLeaf(type, std::move(item), cowid_));
800799
}
801800
else
802801
{

src/ripple/shamap/impl/SHAMapInnerNode.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -284,7 +284,7 @@ SHAMapInnerNode::getString(const SHAMapNodeID& id) const
284284

285285
// We are modifying an inner node
286286
void
287-
SHAMapInnerNode::setChild(int m, std::shared_ptr<SHAMapTreeNode> const& child)
287+
SHAMapInnerNode::setChild(int m, std::shared_ptr<SHAMapTreeNode> child)
288288
{
289289
assert((m >= 0) && (m < branchFactor));
290290
assert(cowid_ != 0);
@@ -310,7 +310,7 @@ SHAMapInnerNode::setChild(int m, std::shared_ptr<SHAMapTreeNode> const& child)
310310
auto const childIndex = *getChildIndex(m);
311311
auto [_, hashes, children] = hashesAndChildren_.getHashesAndChildren();
312312
hashes[childIndex].zero();
313-
children[childIndex] = child;
313+
children[childIndex] = std::move(child);
314314
}
315315

316316
hash_.zero();

0 commit comments

Comments
 (0)