Skip to content

Conversation

@alienx5499
Copy link
Contributor

Overview

Implements the move-friendly accessor improvements requested in issue #95. Adds ref-qualified and rvalue overloads so temporaries can return buffers by move, eliminating unnecessary copies while keeping lvalue behavior unchanged.

Problem

  • toBuffer/toVector returned const references only, forcing an extra copy when called on temporaries.
  • Multihash stored data in a const shared_ptr, blocking safe move-out even when uniquely owned.

Solution

  • Add const&/&& overloads for toBuffer and toVector on Multihash, UVarint, and PeerId.
  • Allow Multihash to move out its byte buffer when uniquely owned; fall back to const& otherwise.
  • Preserve existing API surface for lvalues; only move semantics change for rvalues.

Key Changes

  • include/libp2p/multi/multihash.hpp / src/multi/multihash.cpp: ref-qualified accessors; move-out path when Data is uniquely owned.
  • include/libp2p/multi/uvarint.hpp / src/multi/uvarint.cpp: add rvalue toVector to move backing bytes from temporaries.
  • include/libp2p/peer/peer_id.hpp / src/peer/peer_id.cpp: rvalue toVector delegates to moved Multihash.

Closes #95

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Avoid useless vector coping by move semantic and using referenceness qualifier

1 participant