Skip to content

ringbuf: add zero-copy read with deferred consumer advancement#1968

Open
orishuss wants to merge 1 commit intocilium:mainfrom
orishuss:ringbuf-zero-copy-commit
Open

ringbuf: add zero-copy read with deferred consumer advancement#1968
orishuss wants to merge 1 commit intocilium:mainfrom
orishuss:ringbuf-zero-copy-commit

Conversation

@orishuss
Copy link
Copy Markdown
Contributor

@orishuss orishuss commented Mar 23, 2026

Summary

Adds ReadZeroCopy and Commit to ringbuf.Reader, enabling zero-copy reads from the mmap'd ring buffer with batched consumer position advancement.

Motivation

ReadInto copies every record from mmap into a user buffer and advances the consumer position per-record via atomic store. In high-throughput scenarios (100K+ events/sec), the memmove and per-record atomics become a measurable CPU cost.

This change allows callers to:

  1. Read records directly from the mmap'd region (no copy)
  2. Process multiple records
  3. Advance the consumer position once via Commit

Changes

  • Reader.ReadZeroCopy(rec *Record) error — sets rec.RawSample to a slice of the mmap'd ring, does not advance consumer position
  • Reader.Commit() — single atomic store to release all consumed space
  • readRecord refactored to delegate to readRecordZeroCopy + copy + advance (no duplication)
  • ReadInto and ReadZeroCopy share poll logic via readWait helper
  • Windows: real zero-copy via ringReader delegation (same as Linux)
  • Tests: single read, multi read with discards, commit releases space, deadline, no-op commit, benchmark

API

// Zero-copy read — slice valid until Commit.
err := reader.ReadZeroCopy(&rec)

// Batch commit after processing N records.
reader.Commit()

Existing Read/ReadInto semantics are unchanged.

Related

This PR differs by deferring consumer advancement, eliminating per-record atomic stores in addition to the copy.

@orishuss orishuss requested review from a team and florianl as code owners March 23, 2026 10:14
@orishuss orishuss force-pushed the ringbuf-zero-copy-commit branch 3 times, most recently from 2c99f76 to da08000 Compare March 23, 2026 10:46
Signed-off-by: Ori Shussman <orishuss@gmail.com>
@orishuss orishuss force-pushed the ringbuf-zero-copy-commit branch from da08000 to 609e92e Compare March 23, 2026 10:50
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