New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[c++11] Use std::unique_ptr for block creation. #8223
Conversation
CreateNewBlock returns a pointer for which the caller takes ownership. Use std::unique_ptr to make this explicit and simplify handling of these objects in getblocktemplate.
@@ -164,7 +165,7 @@ CBlockTemplate* BlockAssembler::CreateNewBlock(const CScript& scriptPubKeyIn) | |||
throw std::runtime_error(strprintf("%s: TestBlockValidity failed: %s", __func__, FormatStateMessage(state))); | |||
} | |||
|
|||
return pblocktemplate.release(); | |||
return std::move(pblocktemplate); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Return value optimization applies here; I think using std::move is discouraged in that case.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No, it does not. pblocktemplate
is a member variable and not a local one, I think the code does not compile without std::move
.
Oops, I forgot we just merged the refactor that turned it into a class. You're right. |
Concept ACK. But it might make more sense to use a shared_ptr so TestBlockValidity can be split off to a new thread while GBT returns... |
utACK 9fce062 |
@@ -160,7 +160,7 @@ class BlockAssembler | |||
public: | |||
BlockAssembler(const CChainParams& chainparams); | |||
/** Construct a new block template with coinbase to scriptPubKeyIn */ | |||
CBlockTemplate* CreateNewBlock(const CScript& scriptPubKeyIn); | |||
std::unique_ptr<CBlockTemplate> CreateNewBlock(const CScript& scriptPubKeyIn); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shouldn't the caller decide in what kind of pointer to wrap the object?
What if they want to e.g. have a shared pointer instead?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Converting from unique_ptr to shared_ptr is relatively cheap (specifically, does not require copying the object).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
But does this mean we want all functions that currently return a bare pointer that will become caller ownership to returning unique_ptr
?
This is probably the kind of c++11 refactoring that we want to leave for later.
utACK 9fce062 |
9fce062 [c++11] Use std::unique_ptr for block creation. (Daniel Kraft)
9fce062 [c++11] Use std::unique_ptr for block creation. (Daniel Kraft)
9fce062 [c++11] Use std::unique_ptr for block creation. (Daniel Kraft)
CreateNewBlock
returns a pointer for which the caller takes ownership. Usestd::unique_ptr
to make this explicit and simplify handling of these objects ingetblocktemplate
.