-
Notifications
You must be signed in to change notification settings - Fork 43
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Don't copy contiguous bytes on reception
This uses the slices iterator API of zenoh-cpp to avoid unecessarily copying bytes into a vecotr, if and only if the bytes is made up of exactly one slice.
- Loading branch information
1 parent
df6ec26
commit d32f34a
Showing
3 changed files
with
94 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
// Copyright 2024 Open Source Robotics Foundation, Inc. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
#ifndef DETAIL__PAYLOAD_HPP_ | ||
#define DETAIL__PAYLOAD_HPP_ | ||
|
||
#include <zenoh.h> | ||
|
||
#include <variant> | ||
#include <utility> | ||
#include <vector> | ||
|
||
namespace rmw_zenoh_cpp | ||
{ | ||
///============================================================================= | ||
class Payload | ||
{ | ||
public: | ||
explicit Payload(const zenoh::Bytes & bytes) | ||
{ | ||
auto slices = bytes.slice_iter(); | ||
auto slice = slices.next(); | ||
if (!slice.has_value()) { | ||
bytes_ = nullptr; | ||
} else { | ||
if (!slices.next().has_value()) { | ||
bytes_ = Contiguous {slice.value(), bytes.clone()}; | ||
} else { | ||
bytes_ = bytes.as_vector(); | ||
} | ||
} | ||
} | ||
|
||
~Payload() = default; | ||
|
||
const uint8_t * data() | ||
{ | ||
if (std::holds_alternative<Empty>(bytes_)) { | ||
return nullptr; | ||
} else if (std::holds_alternative<NonContiguous>(bytes_)) { | ||
return std::get<NonContiguous>(bytes_).data(); | ||
} else { | ||
return std::get<Contiguous>(bytes_).slice.data; | ||
} | ||
} | ||
|
||
size_t size() | ||
{ | ||
if (std::holds_alternative<Empty>(bytes_)) { | ||
return 0; | ||
} else if (std::holds_alternative<NonContiguous>(bytes_)) { | ||
return std::get<NonContiguous>(bytes_).size(); | ||
} else { | ||
return std::get<Contiguous>(bytes_).slice.len; | ||
} | ||
} | ||
|
||
bool empty() | ||
{ | ||
return std::holds_alternative<Empty>(bytes_); | ||
} | ||
|
||
private: | ||
struct Contiguous | ||
{ | ||
zenoh::Slice slice; | ||
zenoh::Bytes bytes; | ||
}; | ||
using NonContiguous = std::vector<uint8_t>; | ||
using Empty = std::nullptr_t; | ||
// Is `std::vector<uint8_t>` in case of a non-contiguous payload | ||
// and `zenoh::Slice` plus a `zenoh::Bytes` otherwise. | ||
std::variant<NonContiguous, Contiguous, Empty> bytes_; | ||
}; | ||
} // namespace rmw_zenoh_cpp | ||
|
||
#endif // DETAIL__PAYLOAD_HPP_ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters