Skip to content

Fix RingBuffer::push() loading wrong atomic index#697

Merged
Samahu merged 1 commit intoouster-lidar:masterfrom
EasonNYC:fix/ring-buffer-push-uses-wrong-index
Feb 23, 2026
Merged

Fix RingBuffer::push() loading wrong atomic index#697
Samahu merged 1 commit intoouster-lidar:masterfrom
EasonNYC:fix/ring-buffer-push-uses-wrong-index

Conversation

@EasonNYC
Copy link
Contributor

Summary

RingBuffer::push() in ouster_client/include/ouster/impl/ring_buffer.h loads r_idx_ (the read index) instead of w_idx_ (the write index) before the compare_exchange_strong loop. This causes the CAS to always fail on the first iteration when the buffer is non-empty, adding an unnecessary retry on every push.

The fix changes line 151 from:

size_t write_idx = r_idx_.load();

to:

size_t write_idx = w_idx_.load();

This matches the correct pattern used in pop(), which loads r_idx_ before its CAS loop on r_idx_.

Impact

Eliminates one wasted CAS iteration per push() call on non-empty buffers.

In push(), the initial load reads r_idx_ (read index) instead of w_idx_ (write index) before the compare_exchange_strong loop on
w_idx_. This causes the CAS to always fail on the first iteration when the buffer is non-empty, since the expected value comes from
the wrong index. The CAS self-corrects on retry (failure loads the actual w_idx_ value), so this is a performance bug rather than a correctness bug, but the intent is clearly to load w_idx_ — matching the pattern in pop() which correctly loads r_idx_.
throw std::overflow_error("pushed a full ring buffer");
}
size_t write_idx = r_idx_.load();
size_t write_idx = w_idx_.load();
Copy link
Collaborator

Choose a reason for hiding this comment

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

Make sense.. makes me wonder how this wasn't causing problem thus far.

@Samahu Samahu merged commit 409b452 into ouster-lidar:master Feb 23, 2026
1 check passed
@Samahu
Copy link
Collaborator

Samahu commented Feb 23, 2026

@EasonNYC We have merged the PR but please note this object might be removed on the next update (since the current code doesn't depend on it).

@Samahu Samahu removed their assignment Feb 23, 2026
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