Reuse FastBuffer managed buffer for each endpoint. - #904
Open
dskkato wants to merge 1 commit into
Open
Conversation
dskkato
force-pushed
the
dskkato/reduce_extra_heap_allocations
branch
from
August 11, 2026 08:25
08d53a6 to
6e58ea5
Compare
dskkato
marked this pull request as ready for review
August 11, 2026 11:18
|
Tick the box to add this pull request to the merge queue (same as
|
Previously, heap allocation happend for each endpoint with std::vector<uint8_t>. This introduced extra memset via its constructor. For recent ROS 2, where rosidl::Buffer was introduced, sometimes the actual payload and message doesn't match because only buffer descriptors may be sent. In such a case, now `get_serialized_size()` seems to be unreliable, and hard to pre-allocate the buffer in that path. Signed-off-by: dskkato <kato.daisuke429@gmail.com>
dskkato
force-pushed
the
dskkato/reduce_extra_heap_allocations
branch
from
August 11, 2026 13:14
6e58ea5 to
9426421
Compare
Author
|
I shared a result of this patch here for a reference. |
nvcyc
approved these changes
Aug 20, 2026
nvcyc
left a comment
Contributor
There was a problem hiding this comment.
Thanks for the PR!
The fix looks good to me.
I also recently found the same allocation overhead issue from some benchmarks and I've verified that this PR does eliminate the unnecessary memory allocation overhead that grows with the underlying data size.
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
Previously, heap allocation happend for each endpoint with std::vector<uint8_t>. This introduced extra memset via its constructor.
For recent ROS 2, where rosidl::Buffer was introduced, sometimes the actual payload size and message size may not match because only buffer descriptors may be sent. In such a case, now
get_serialized_size()seems to be unreliable, and hard to pre-allocate the buffer in that path.Fixes #903
Is this user-facing behavior change?
Possibly yes.
Previously, it is guaranteed that the heap allocation only happens once per endpoint, through externally managed buffer (allocated with
std::vector<uint8_t>). Now the number of allocations depends on combinations of buffer's strategies, and Cdr's usage. So, in small message cases, this patch will introduce an extra overhead of buffer reallocation.For rosidl::Buffer usage, this patch will dramatically reduce extra allocation and zero-initialization when only the optimized message paths exist.
Did you use Generative AI?
I used CodeX to investigate the cause analysis, and FastBuffer/Cdr behavior.
Additional Information
This patch is trying to resolve two issues at same time:
std::vector<uint8_t>constructor.What I originally found is the second point, so if we wish more conservative way, it is possible to reserve the
fast_bufferas previouslly. Still, I believe it is better to reuse that buffer accross endpoints, in my perspective...Since this is my first PR to
rmw_fastrtpsrepository, let me know if I missed anything.