Skip to content

Deserialize subscription payloads in place - #1074

Merged
ahcorde merged 1 commit into
ros2:rollingfrom
Quchaosheng:perf-inplace-deserialization-buffer
Oct 22, 2026
Merged

ahcorde merged 1 commit into
ros2:rollingfrom
Quchaosheng:perf-inplace-deserialization-buffer

Conversation

@Quchaosheng

@Quchaosheng Quchaosheng commented Sep 30, 2026 •

Copy link
Copy Markdown
Contributor

Description

take_one_message() allocated a buffer of payload_data.size() * 4 + 65536 bytes and copied the payload into it before deserializing. The kilted, jazzy and humble branches already deserialize straight from the payload with FastBuffer, so this brings rolling in line with them and drops the per-message allocation and copy from the subscription hot path.

This is the first option in the issue's suggested handling, and the one the released branches use. Right-sizing by itself is not: as the issue measured, the oversized block is what keeps M_MMAP_THRESHOLD lifted, and with it M_TRIM_THRESHOLD, so removing the oversized block while still allocating costs 30-40% at 1 MiB because glibc returns the heap with brk between messages and the next one re-faults it. Removing the allocation takes the allocator out of the path instead of trading one policy for another.

Payload owns its bytes and msg_data stays alive for the whole deserialization, so the slice stays valid. FastCDR is bounded by the size passed to FastBuffer and raises NotEnoughMemoryException on overrun, so the 4x margin was never addressable by the deserializer.

Related to #1060

Is this user-facing behavior change?

No. Same deserialization result, minus a per-message allocation and copy.

Did you use Generative AI?

Yes, I used an LLM to review the change and to help write this description. The code change is mine.

take_one_message() allocated a buffer sized at four times the payload plus
64 KiB, copied the payload into it, and then built the FastBuffer over
payload_data.size(). The extra capacity was never addressable by the
deserializer: FastCDR is bounded by the size passed to FastBuffer and
raises NotEnoughMemoryException on overrun, so the margin could not be used.

Construct the FastBuffer over the received payload instead, as the released
branches do, and drop the allocation and the full-payload copy. Payload owns
its bytes and msg_data stays alive for the whole deserialization, so the
slice remains valid for as long as the message is processed.

The allocation and the copy were both introduced together in ros2#930. Removing
them restores the behaviour of the released branches while keeping the
endpoint-aware deserialization path that the same commit added.

Signed-off-by: Quchaosheng <quchaosheng000406@163.com>
@Yadunund

Yadunund commented Oct 6, 2026

Copy link
Copy Markdown
Member

@Quchaosheng thanks for the PR, could you update your PR description to follow the template?

@Quchaosheng

Quchaosheng commented Oct 9, 2026 •

Copy link
Copy Markdown
Contributor Author

@Yadunund thanks.Updated the description to follow the template, thanks for the pointer.

@Yadunund

Copy link
Copy Markdown
Member

@Quchaosheng sorry for the delay. Will take a look this week.

@Yadunund Yadunund left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. Let's also backport to lyrical.

@YuanYuYuan could you do a final pass review as well?

@YuanYuYuan

Copy link
Copy Markdown
Contributor

LGTM. Let's also backport to lyrical.

@YuanYuYuan could you do a final pass review as well?

Yep. As I stated in the issue, this patch does solve it. Thanks again @Quchaosheng !

@Yadunund

Copy link
Copy Markdown
Member

@ahcorde
ahcorde merged commit 678931f into ros2:rolling Oct 22, 2026
5 checks passed
@ahcorde

ahcorde commented Oct 22, 2026

Copy link
Copy Markdown
Contributor

https://github.com/Mergifyio backport lyrical

@mergify

mergify Bot commented Oct 22, 2026 •

Copy link
Copy Markdown

backport lyrical

✅ Backports have been created

Details

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants