Skip to content

Commit 8dd9bf5

Browse files
committed
Recycle serialization buffers on transmission
Adds a bounded 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.
1 parent 79fc5ce commit 8dd9bf5

File tree

4 files changed

+204
-17
lines changed

4 files changed

+204
-17
lines changed
+104
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
// Copyright 2024 Open Source Robotics Foundation, Inc.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
#include "buffer_pool.hpp"
16+
17+
#include <cstddef>
18+
#include <cstdlib>
19+
#include <mutex>
20+
#include <vector>
21+
22+
#include "rcutils/allocator.h"
23+
#include "rcutils/env.h"
24+
#include "logging_macros.hpp"
25+
26+
namespace rmw_zenoh_cpp
27+
{
28+
///=============================================================================
29+
BufferPool::BufferPool()
30+
: buffers_(), mutex_()
31+
{
32+
const char * env_value;
33+
const char * error_str = rcutils_get_env("RMW_ZENOH_BUFFER_POOL_MAX_SIZE_BYTES", &env_value);
34+
if (error_str != nullptr) {
35+
RMW_ZENOH_LOG_ERROR_NAMED(
36+
"rmw_zenoh_cpp",
37+
"Unable to read maximum buffer pool size, falling back to default.");
38+
max_size_ = DEFAULT_MAX_SIZE;
39+
} else {
40+
max_size_ = std::atoll(env_value);
41+
}
42+
size_ = 0;
43+
}
44+
45+
///=============================================================================
46+
BufferPool::~BufferPool()
47+
{
48+
rcutils_allocator_t allocator = rcutils_get_default_allocator();
49+
50+
for (Buffer & buffer : buffers_) {
51+
allocator.deallocate(buffer.data, allocator.state);
52+
}
53+
}
54+
55+
///=============================================================================
56+
BufferPool::Buffer BufferPool::allocate(size_t size)
57+
{
58+
std::lock_guard<std::mutex> guard(mutex_);
59+
60+
rcutils_allocator_t allocator = rcutils_get_default_allocator();
61+
62+
if (buffers_.empty()) {
63+
if (size_ + size > max_size_) {
64+
return {};
65+
} else {
66+
size_ += size;
67+
}
68+
uint8_t * data = static_cast<uint8_t *>(allocator.allocate(size, allocator.state));
69+
if (data == nullptr) {
70+
return {};
71+
} else {
72+
return Buffer {data, size};
73+
}
74+
} else {
75+
Buffer buffer = buffers_.back();
76+
buffers_.pop_back();
77+
if (buffer.size < size) {
78+
size_t size_diff = size - buffer.size;
79+
if (size_ + size_diff > max_size_) {
80+
return {};
81+
} else {
82+
size_ += size_diff;
83+
}
84+
uint8_t * data = static_cast<uint8_t *>(allocator.reallocate(
85+
buffer.data, size, allocator.state));
86+
if (data == nullptr) {
87+
return {};
88+
} else {
89+
buffer.data = data;
90+
buffer.size = size;
91+
}
92+
}
93+
return buffer;
94+
}
95+
}
96+
97+
///=============================================================================
98+
void BufferPool::deallocate(BufferPool::Buffer buffer)
99+
{
100+
std::lock_guard<std::mutex> guard(mutex_);
101+
buffers_.push_back(buffer);
102+
}
103+
104+
} // namespace rmw_zenoh_cpp
+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
// Copyright 2024 Open Source Robotics Foundation, Inc.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
#ifndef DETAIL__BUFFER_POOL_HPP_
16+
#define DETAIL__BUFFER_POOL_HPP_
17+
18+
#include <cstddef>
19+
#include <cstdlib>
20+
#include <mutex>
21+
#include <vector>
22+
23+
#include "rcutils/allocator.h"
24+
#include "rcutils/env.h"
25+
#include "logging_macros.hpp"
26+
27+
namespace rmw_zenoh_cpp
28+
{
29+
///=============================================================================
30+
class BufferPool
31+
{
32+
public:
33+
struct Buffer
34+
{
35+
uint8_t * data;
36+
size_t size;
37+
};
38+
39+
BufferPool();
40+
41+
~BufferPool();
42+
43+
Buffer allocate(size_t size);
44+
45+
void deallocate(Buffer buffer);
46+
47+
private:
48+
std::vector<Buffer> buffers_;
49+
std::mutex mutex_;
50+
size_t max_size_;
51+
size_t size_;
52+
// NOTE(fuzzypixelz): Pooled buffers are recycled with the expectation that they would reside in
53+
// cache, thus this this value should be comparable to the size of a modern CPU cache. The default
54+
// value (16 MiB) is relatively conservative as CPU cache sizes range from a few MiB to a few
55+
// hundred MiB.
56+
const size_t DEFAULT_MAX_SIZE = 16 * 1024 * 1024;
57+
};
58+
} // namespace rmw_zenoh_cpp
59+
60+
#endif // DETAIL__BUFFER_POOL_HPP_

rmw_zenoh_cpp/src/detail/rmw_context_impl_s.hpp

+4
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424

2525
#include "graph_cache.hpp"
2626
#include "rmw_node_data.hpp"
27+
#include "buffer_pool.hpp"
2728

2829
#include "rmw/ret_types.h"
2930
#include "rmw/types.h"
@@ -92,6 +93,9 @@ struct rmw_context_impl_s final
9293
// Forward declaration
9394
class Data;
9495

96+
// Pool of serialization buffers.
97+
rmw_zenoh_cpp::BufferPool serialization_buffer_pool;
98+
9599
private:
96100
std::shared_ptr<Data> data_{nullptr};
97101
};

rmw_zenoh_cpp/src/detail/rmw_publisher_data.cpp

+36-17
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include <cinttypes>
2121
#include <memory>
2222
#include <mutex>
23+
#include <optional>
2324
#include <string>
2425
#include <utility>
2526
#include <vector>
@@ -216,24 +217,33 @@ rmw_ret_t PublisherData::publish(
216217
type_support_impl_);
217218

218219
// To store serialized message byte array.
219-
char * msg_bytes = nullptr;
220-
221-
rcutils_allocator_t * allocator = &rmw_node_->context->options.allocator;
220+
uint8_t * msg_bytes = nullptr;
221+
222+
rmw_context_impl_s *context_impl = static_cast<rmw_context_impl_s *>(rmw_node_->data);
223+
224+
rcutils_allocator_t allocator = rcutils_get_default_allocator();
225+
226+
// Try to get memory from the serialization buffer pool.
227+
BufferPool::Buffer serialization_buffer =
228+
context_impl->serialization_buffer_pool.allocate(max_data_length);
229+
if (serialization_buffer.data == nullptr) {
230+
void * data = allocator.allocate(max_data_length, allocator.state);
231+
RMW_CHECK_FOR_NULL_WITH_MSG(
232+
msg_bytes, "failed to allocate serialization buffer", return RMW_RET_BAD_ALLOC);
233+
msg_bytes = static_cast<uint8_t *>(data);
234+
} else {
235+
msg_bytes = serialization_buffer.data;
236+
}
222237

223-
auto always_free_msg_bytes = rcpputils::make_scope_exit(
224-
[&msg_bytes, allocator]() {
225-
if (msg_bytes) {
226-
allocator->deallocate(msg_bytes, allocator->state);
238+
auto delete_msg_bytes = rcpputils::make_scope_exit(
239+
[&allocator, &msg_bytes, &serialization_buffer]() {
240+
if (serialization_buffer.data == nullptr) {
241+
allocator.deallocate(msg_bytes, allocator.state);
227242
}
228243
});
229244

230-
// Get memory from the allocator.
231-
msg_bytes = static_cast<char *>(allocator->allocate(max_data_length, allocator->state));
232-
RMW_CHECK_FOR_NULL_WITH_MSG(
233-
msg_bytes, "bytes for message is null", return RMW_RET_BAD_ALLOC);
234-
235245
// Object that manages the raw buffer
236-
eprosima::fastcdr::FastBuffer fastbuffer(msg_bytes, max_data_length);
246+
eprosima::fastcdr::FastBuffer fastbuffer(reinterpret_cast<char *>(msg_bytes), max_data_length);
237247

238248
// Object that serializes the data
239249
rmw_zenoh_cpp::Cdr ser(fastbuffer);
@@ -258,10 +268,19 @@ rmw_ret_t PublisherData::publish(
258268
sequence_number_++, source_timestamp, entity_->copy_gid()).serialize_to_zbytes();
259269

260270
// TODO(ahcorde): shmbuf
261-
std::vector<uint8_t> raw_data(
262-
reinterpret_cast<const uint8_t *>(msg_bytes),
263-
reinterpret_cast<const uint8_t *>(msg_bytes) + data_length);
264-
zenoh::Bytes payload(std::move(raw_data));
271+
zenoh::Bytes payload;
272+
if (serialization_buffer.data == nullptr) {
273+
std::vector<uint8_t> raw_data(
274+
reinterpret_cast<const uint8_t *>(msg_bytes),
275+
reinterpret_cast<const uint8_t *>(msg_bytes) + data_length);
276+
payload = zenoh::Bytes(std::move(raw_data));
277+
} else {
278+
auto deleter = [buffer_pool = &context_impl->serialization_buffer_pool,
279+
buffer = serialization_buffer](uint8_t *){
280+
buffer_pool->deallocate(buffer);
281+
};
282+
payload = zenoh::Bytes(msg_bytes, data_length, deleter);
283+
}
265284

266285
TRACETOOLS_TRACEPOINT(
267286
rmw_publish, static_cast<const void *>(rmw_publisher_), ros_message, source_timestamp);

0 commit comments

Comments
 (0)