libp2p / libp2p/cpp-libp2p

Avoid useless vector coping by move semantic and using referenceness qualifier

Open
#95 0 comments 1 reaction 0 assignees View on GitHub

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

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. 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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.