Avoid useless vector coping by move semantic and using referenceness qualifier
Open
Nobody has claimed this yet.
- Dominant language
- C++
- Stars
- 492
- Forks
- 130
- PR merge metrics
- No merged PRs in 30d
Description
For example:
const std::vector<uint8_t> &BlaBla::toBuffer() const {
return data_;
}
If we use it to make BlaBla and get std::vector<uint8_t> from that and that all.
std::vector<uint8_t> vec = BlaBla().toBuffer(); // <= copy here
Will be better to use ref-qualifier to move data from sigle-used temp object:
const std::vector<uint8_t> &BlaBla::toBuffer() const & { // <= method for case of usual object
return data_;
}
std::vector<uint8_t> BlaBla::toBuffer() const && { // <= method for case of temp object
return std::move(data_);
}
std::vector<uint8_t> &BlaBla::asBuffer() { // <= method to access internal vector
return data_;
}
const std::vector<uint8_t> &BlaBla::asBuffer() const { // <= method to RO-access internal vector
return data_;
}
Contributor guide
No contributing guide indexed for this repository
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by searching the codebase for BlaBla::toBuffer and the data_ member, then inspect how callers obtain buffers from temporary and non-temporary objects. Use the issue’s ref-qualified toBuffer and asBuffer examples as the behavioral target, and verify that temporary use avoids an unnecessary copy while ordinary access remains available.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- cpp
- Domain
- backend-api-design
- Issue type
- Refactor
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 25/100