All posts

ORK / R&D

How We Made rust-mavlink Frame Reading More Than 3× Faster

How we cut frame-reading overhead in rust-mavlink 0.19.0 with read-ahead and SIMD-powered marker search.

Before a robot can act on a command, its software has to receive, recognize and decode it. That work happens continuously, alongside everything else the robot needs to do.

We use MAVLink throughout our flight-control architecture. Cerebrum, our flight brain, uses it to communicate with PX4, ArduPilot and Kaptan. Kaptan, our bare-metal robotics firmware, uses it for external control and communication.

Both systems have to deliver high performance with limited processing power and memory. With hundreds of reads per second on each side, small costs add up quickly. We want that processing time available for perception, navigation and control.

This work extends beyond our own products. We contribute to the open-source projects we depend on, working alongside their maintainers and communities.

We recently released rust-mavlink 0.19.0, with various improvements and extensions from contributors across the project. We rewrote one of the library’s core operations: frame reading. Our benchmarks showed 2.73–3.85× faster frame reading.

The previous reader requested the start marker, header and remaining frame in separate steps. It already had a buffer, but still made repeated small read requests.

The new reader reads ahead into one reusable buffer. A single read can contain several frames, with the next frames already waiting in memory. For unsigned messages, it also parses the payload directly from that buffer instead of copying it first.

Reading frame

MarkerHeaderPayloadCRC
Before 30/30frames
Now 30/30frames

In the field, electrical interference and poor wiring can disrupt serial communication. A receiver may also reconnect halfway through a transmission. The incoming stream can contain corrupted bytes or part of a frame, so the reader must find the next start marker.

The old reader searched byte by byte. The new reader first checks whether the marker is already at the current position. If it needs to search further, its SIMD-powered search can compare several bytes at once on supported processors. That cuts down the work between frames, especially when the stream contains more noise.

Finding marker

Before · scalar
Now · SIMD

This work started with the needs of Cerebrum and Kaptan. It now ships in rust-mavlink for everyone who depends on it. We will keep contributing improvements across the stack we share.