facontidavide/ros_type_introspection

Parser::applyVisitorToBuffer() method is slow for sensor_msgs/Image

RamanZharkou opened this issue · 5 comments

I am trying to use ros_type_introspection to get a header of different image messages.
Found that Parser::applyVisitorToBuffer() method is slow for sensor_msgs/Image message type when image have high resolution (1920x1080).

Code snippet for subscriber callback:

void TopicMonitor::topicCallback(const topic_tools::ShapeShifter::ConstPtr& msg, const string 
    &topicName, const unsigned int topicIndex)
{
    const string&  datatype   =  msg->getDataType();
    const string&  definition =  msg->getMessageDefinition();
    parser_.registerMessageDefinition(topicName, RosIntrospection::ROSType(datatype), definition);
    static vector<uint8_t> buffer;

    buffer.resize(msg->size());
    ros::serialization::OStream stream(buffer.data(), buffer.size());
    msg->write(stream);

    std_msgs::Header header;

    const RosIntrospection::Parser::VisitingCallback deserializeHeader =
            [&header](const RosIntrospection::ROSType&, absl::Span<uint8_t>& buffer)
            {
                ros::serialization::IStream is( buffer.data(), buffer.size() );
                ros::serialization::deserialize(is, header);
            };

    const RosIntrospection::ROSType header_type(ros::message_traits::DataType<std_msgs::Header>::value());
    static absl::Span<uint8_t> buffer_span(buffer);

    parser_.applyVisitorToBuffer(topicName, header_type, buffer_span, deserializeHeader);

    ...
}

Thanks.

Thanks for reporting.

Extracting just the Header seems to be a common use case.
This is also related somehow to issue #34

Some brave soul might want to solve this. Right now I don't have much time, unfortunately

Fixed (and much simpler code):

static vector<uint8_t> buffer;
buffer.resize(msg->size());
ros::serialization::OStream stream(buffer.data(), buffer.size());
msg->write(stream);

absl::Span<uint8_t> buffer_span(buffer);
std_msgs::Header header = parser_.extractField<std_msgs::Header>(topicName, buffer_span);

Please confirm that it works for you

Yes, works fine now. Thanks.

I've tested the new code on various workloads, the results are great:
workload 1: 29.3s -> 3.3s
workload 2: 2.8s -> 0.64s
workload 3: 47.2s -> 3.0s