-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Recycle serialization buffers on transmission
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
1 parent
68b4d9a
commit 21006d0
Showing
4 changed files
with
356 additions
and
288 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,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 <mutex> | ||
#include <cassert> | ||
#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_ |
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
Oops, something went wrong.