Skip to content

perf: read header.stamp directly from CDR bytes without deserialization#89

Merged
otenim merged 1 commit intomainfrom
perf/header-stamp-zero-copy
Apr 9, 2026
Merged

perf: read header.stamp directly from CDR bytes without deserialization#89
otenim merged 1 commit intomainfrom
perf/header-stamp-zero-copy

Conversation

@otenim
Copy link
Copy Markdown
Collaborator

@otenim otenim commented Apr 9, 2026

Summary

  • Replace full message deserialization in extract_header_stamp() with direct CDR byte reads at fixed offsets (4-11)
  • All 18 types in kTypesWithHeader have std_msgs/msg/Header as their first field, so stamp.sec and stamp.nanosec are at known CDR offsets
  • Remove 18 ROS message includes and template instantiations from header_stamp.cpp
  • Validates CDR little-endian encoding and minimum buffer size

Before

Every message was fully deserialized to read 8 bytes of timestamp. For sensor_msgs/msg/Image (multi-MB), this allocated and copied the entire pixel buffer.

After

Two memcpy calls read sec and nanosec directly from the serialized buffer. Zero heap allocation, zero data copying.

Test plan

  • Build succeeds (colcon build)
  • Convert a bag with --use-header-stamp-as-log-time and verify output timestamps match previous behavior
  • Convert a bag with mixed topics (Image, IMU, LiDAR) and compare output against baseline
  • Verify no regression in bags without --use-header-stamp-as-log-time (code path not reached)

Replace full message deserialization in extract_header_stamp() with
direct CDR byte reads at fixed offsets. All types in kTypesWithHeader
have std_msgs/msg/Header as their first field, so stamp.sec (offset 4)
and stamp.nanosec (offset 8) can be read via memcpy.

This eliminates heap allocation and data copying for every message when
--use-header-stamp-as-log-time is enabled. The impact is especially
significant for large messages like sensor_msgs/msg/Image, where
megabytes of pixel data were previously deserialized just to read 8
bytes of timestamp.

Also removes 18 ROS message type includes and template instantiations
from header_stamp.cpp, reducing compile time.
@otenim otenim merged commit 059b257 into main Apr 9, 2026
2 checks passed
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.

1 participant