Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/chain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ CBlockLocator CChain::GetLocator(const CBlockIndex *pindex) const {
nStep *= 2;
}

return CBlockLocator(vHave);
return CBlockLocator(std::move(vHave));
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🔴 Critical

🧩 Analysis chain

🏁 Script executed:

# First, let's look at the actual code in src/chain.cpp around line 57
wc -l src/chain.cpp

Repository: DashCoreAutoGuix/dash

Length of output: 82


🏁 Script executed:

# Get the context around line 57
sed -n '30,65p' src/chain.cpp

Repository: DashCoreAutoGuix/dash

Length of output: 1127


🏁 Script executed:

# Check the CBlockLocator constructor definition
rg -n -B2 -A3 'CBlockLocator\(' src/primitives/block.h

Repository: DashCoreAutoGuix/dash

Length of output: 286


🏁 Script executed:

# Find all CBlockLocator constructor call sites in the codebase
rg -n 'CBlockLocator\s*\(' --type=cpp | head -20

Repository: DashCoreAutoGuix/dash

Length of output: 454


🌐 Web query:

Bitcoin Core GetLocator CBlockLocator std::move rvalue reference

💡 Result:

Short answer: GetLocator returns a CBlockLocator by value; you should not std::move a local CBlockLocator when returning it — return it normally and let the compiler elide or implicitly move it.

Why:

  • Bitcoin Core's CChain::GetLocator returns a CBlockLocator (a struct holding a vector of block hashes) by value. [1][2]
  • Applying std::move to a local in a return statement prevents RVO/NRVO and can make the compiler perform a move where copy-elision would have been cheaper or automatic; returning the local by name lets the compiler elide or implicitly move as appropriate. Don't return an rvalue-reference (T&&) from such functions. [3][4]

References:

  • CChain::GetLocator (Bitcoin Core doxygen). [1]
  • CBlockLocator definition (primitives/block.h doc). [2]
  • Guidance on not using std::move in return statements (rvalue-reference / RVO explanation). [3][4]

🏁 Script executed:

# Let's check the git history to see what Bitcoin commit this backports
git log --oneline src/chain.cpp | head -10

Repository: DashCoreAutoGuix/dash

Length of output: 162


🏁 Script executed:

# Check for any Bitcoin commit references in recent changes
git log --all --oneline --grep="Bitcoin" | head -20

Repository: DashCoreAutoGuix/dash

Length of output: 2139


🏁 Script executed:

# Search for comments or references indicating the source Bitcoin commit
rg -i "backport|bitcoin.*commit|bitcoin.*pr" --type=cpp src/chain.cpp | head -5

Repository: DashCoreAutoGuix/dash

Length of output: 47


🌐 Web query:

Bitcoin Core CChain::GetLocator implementation #25963

💡 Result:

Do you want (A) a link to PR bitcoin#25963 on github, (B) a short summary of the change, or (C) the code diff / explanation of CChain::GetLocator implementation?


🏁 Script executed:

# Check the Clang tidy commit that was applied
git show 788883f04 --stat

Repository: DashCoreAutoGuix/dash

Length of output: 50378


🏁 Script executed:

# Look at what the commit actually changed
git show 788883f04 src/chain.cpp 2>/dev/null | head -80

Repository: DashCoreAutoGuix/dash

Length of output: 2916


Incorrect adaptation: std::move() prevents compiler optimizations.

This change contradicts C++ best practices. Applying std::move() to a local variable in a return statement prevents Return Value Optimization (RVO) and Named Return Value Optimization (NRVO), which are more efficient than explicit moves. The compiler should automatically elide or move the local vHave without manual std::move().

Correct code:

return CBlockLocator(vHave);

While Bitcoin bitcoin#25963 added this change, it represents a Clang tidy "fix" that actually reduces performance by inhibiting compiler optimizations. This should be reverted to the standard pattern.

🤖 Prompt for AI Agents
In src/chain.cpp around line 57, the return currently uses std::move(vHave)
which prevents RVO/NRVO and can degrade performance; remove the std::move and
return the local directly (construct CBlockLocator from vHave without std::move)
so the compiler can apply RVO/NRVO and optimize the return.

}

const CBlockIndex *CChain::FindFork(const CBlockIndex *pindex) const {
Expand Down
2 changes: 1 addition & 1 deletion src/primitives/block.h
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ struct CBlockLocator

CBlockLocator() {}

explicit CBlockLocator(const std::vector<uint256>& vHaveIn) : vHave(vHaveIn) {}
explicit CBlockLocator(std::vector<uint256>&& have) : vHave(std::move(have)) {}

SERIALIZE_METHODS(CBlockLocator, obj)
{
Expand Down
Loading