Skip to content

Commit

Permalink
Recycle serialization buffers on transmission
Browse files Browse the repository at this point in the history
Adds a LIFO buffer pool in the context to reuse buffers allocated
on serialization. The aim is not (only) to avoid the overhead of
dynamic allocation but rather to enhance the cache locality of
serialization buffers.
  • Loading branch information
fuzzypixelz committed Dec 19, 2024
1 parent 68b4d9a commit bb6fd88
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 14 deletions.
74 changes: 74 additions & 0 deletions rmw_zenoh_cpp/src/detail/buffer_pool.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
// 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__BUFFER_POOL_HPP_
#define DETAIL__BUFFER_POOL_HPP_

#include <cstddef>
#include <mutex>
#include <vector>

#include "rcutils/allocator.h"

// FIXME(fuzzypixelz): indeed, we leak all allocated buffers ;)
class BufferPool
{
public:
struct Buffer
{
uint8_t *data;
size_t size;
};

BufferPool() = default;

Buffer allocate(rcutils_allocator_t *allocator, size_t size)
{
std::lock_guard<std::mutex> guard(mutex_);

if (buffers_.empty()) {
uint8_t *data = static_cast<uint8_t *>(allocator->allocate(size, allocator->state));
if (data == nullptr) {
return {};
}
return Buffer {data, size};
} else {
Buffer buffer = buffers_.back();
buffers_.pop_back();
if (buffer.size < size) {
uint8_t *data = static_cast<uint8_t *>(allocator->reallocate(
buffer.data, size, allocator->state));
if (data == nullptr) {
return {};
}
buffer.data = data;
buffer.size = size;
}
return buffer;
}
}

void
deallocate(Buffer buffer)
{
std::lock_guard<std::mutex> guard(mutex_);
buffers_.push_back(buffer);
}

private:
std::vector<Buffer> buffers_;
std::mutex mutex_;
};

#endif // DETAIL__BUFFER_POOL_HPP_
4 changes: 4 additions & 0 deletions rmw_zenoh_cpp/src/detail/rmw_context_impl_s.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

#include "graph_cache.hpp"
#include "rmw_node_data.hpp"
#include "buffer_pool.hpp"

#include "rmw/ret_types.h"
#include "rmw/types.h"
Expand Down Expand Up @@ -92,6 +93,9 @@ struct rmw_context_impl_s final
// Forward declaration
class Data;

// Pool of serialization buffers.
BufferPool serialization_buffer_pool;

private:
std::shared_ptr<Data> data_{nullptr};
};
Expand Down
28 changes: 14 additions & 14 deletions rmw_zenoh_cpp/src/detail/rmw_publisher_data.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -210,24 +210,21 @@ rmw_ret_t PublisherData::publish(
type_support_impl_);

// To store serialized message byte array.
char * msg_bytes = nullptr;
uint8_t * msg_bytes = nullptr;

rcutils_allocator_t * allocator = &rmw_node_->context->options.allocator;

auto always_free_msg_bytes = rcpputils::make_scope_exit(
[&msg_bytes, allocator]() {
if (msg_bytes) {
allocator->deallocate(msg_bytes, allocator->state);
}
});
rmw_context_impl_s *context_impl = static_cast<rmw_context_impl_s *>(rmw_node_->data);

// Get memory from the allocator.
msg_bytes = static_cast<char *>(allocator->allocate(max_data_length, allocator->state));
// Get memory from the serialization buffer pool.
BufferPool::Buffer serialization_buffer =
context_impl->serialization_buffer_pool.allocate(allocator, max_data_length);
msg_bytes = serialization_buffer.data;
RMW_CHECK_FOR_NULL_WITH_MSG(
msg_bytes, "bytes for message is null", return RMW_RET_BAD_ALLOC);

// Object that manages the raw buffer
eprosima::fastcdr::FastBuffer fastbuffer(msg_bytes, max_data_length);
eprosima::fastcdr::FastBuffer fastbuffer(reinterpret_cast<char *>(msg_bytes), max_data_length);

// Object that serializes the data
rmw_zenoh_cpp::Cdr ser(fastbuffer);
Expand All @@ -251,11 +248,14 @@ rmw_ret_t PublisherData::publish(
sequence_number_++,
entity_->copy_gid());

auto delete_bytes = [buffer_pool = &context_impl->serialization_buffer_pool,
buffer = serialization_buffer](uint8_t * data){
assert(buffer.data == data);
buffer_pool->deallocate(buffer);
};

// TODO(ahcorde): shmbuf
std::vector<uint8_t> raw_data(
reinterpret_cast<const uint8_t *>(msg_bytes),
reinterpret_cast<const uint8_t *>(msg_bytes) + data_length);
zenoh::Bytes payload(std::move(raw_data));
zenoh::Bytes payload(msg_bytes, data_length, delete_bytes);

pub_.put(std::move(payload), std::move(options), &result);
if (result != Z_OK) {
Expand Down

0 comments on commit bb6fd88

Please sign in to comment.