Skip to content

feat: add Snapshot() for non-destructive FIFO copy#10

Merged
maxence2997 merged 12 commits into
mainfrom
feat/snapshot-api
May 11, 2026
Merged

feat: add Snapshot() for non-destructive FIFO copy#10
maxence2997 merged 12 commits into
mainfrom
feat/snapshot-api

Conversation

@maxence2997
Copy link
Copy Markdown
Owner

Summary

Adds Snapshot() to both RingBuffer[T] and RingQueue[T] — a non-destructive FIFO copy that fills the gap between Peek() (one item) and Drain() (all items, mutating). Foundational primitive for replay / inspect / filter-without-consuming use cases.

Related issues

Closes #9.

Changes

  • refactor: extract RingBuffer segments helper — pull wrap-detection out of Drain/Clear into an unexported segments() helper. Behavior-preserving; BenchmarkRingBuffer_Drain shows no regression (~1290 ns/op vs ~1296 ns/op baseline). Drain's per-segment copy → clear interleaving preserves cacheline locality.
  • feat: add RingBuffer Snapshot — non-destructive FIFO copy using segments(). Tests I1–I7 cover empty, non-wrap FIFO, wrap-around FIFO, non-mutation, caller / buffer isolation, full-capacity round-trip.
  • feat: add RingQueue Snapshot — lock + delegate to RingBuffer.Snapshot(). No user code ever runs under q.mu. Tests G1–G5 cover empty, FIFO, non-mutation, concurrent enqueue + snapshot (race-clean, monotonicity invariant), and snapshot-after-close.
  • test: add Snapshot benchmarks — three benchmarks: BenchmarkRingBuffer_Snapshot (non-wrap), BenchmarkRingBuffer_Snapshot_Wrap (split region), BenchmarkRingQueue_Snapshot. All 1 alloc/op; wrap and non-wrap within noise, confirming the wrap path stays on bulk copy.
  • doc: changelog Snapshot entry — Unreleased section.
  • style: rename bench const capcap shadowed the builtin; renamed to bufCap.
  • fix: preserve Clear no-op on empty buffer — restored the if rb.size == 0 { return } guard that the refactor inadvertently dropped, matching pre-refactor contract.
  • doc: clarify Snapshot shallow copy and lock cost — note that independence is shallow for pointer/pointer-containing T; document RingQueue.Snapshot's O(N) lock-hold cost.
  • sync: register Snapshot in bench docs tables — added rows for the new benchmarks to cmd/benchsync/defaultTargets so make bench-sync keeps docs/ringbuffer.md and docs/ringqueue.md current.

Out of scope (deferred — see #9 for rationale): AppendTo, ForEach, At, MaxAge, Resize.

Checklist

Required

  • make check passes (fmtlinttest)
  • Each commit represents exactly one logical change
  • Commit messages follow the format in commit-message-instructions.md
  • No unrelated code reformatting in this PR

Conditional

  • New public API: includes GoDoc comments
  • Performance-sensitive change: benchmark included (make bench before/after); Drain refactor shows no regression, Snapshot benches added

1.duplicated wrap branch -> share via segments()
2.keep cache locality -> copy+clear per segment
1.gap between Peek and Drain -> add Snapshot
2.wrap region needs bulk copy -> use segments
1.no thread-safe non-destructive read -> add it
2.no user code under lock -> delegate-only impl
1.no perf baseline for Snapshot -> add benches
2.wrap branch could regress -> add Wrap variant
1.unreleased section empty -> add Snapshot
2.link to tracking issue -> reference #9
1.cap shadows builtin -> rename to bufCap
1.refactor dropped early return -> restore guard
2.pre-refactor was true no-op -> match contract
1.pointer T sharing implicit -> note shallowness
2.O(N) lock hold not stated -> note in GoDoc
1.new Snapshot benches absent in docs -> add rows
2.narrative claim was stale -> note alloc cost
Copilot AI review requested due to automatic review settings May 11, 2026 14:01
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR adds a new non-destructive bulk-read primitive, Snapshot(), to both core data structures (RingBuffer[T] and the concurrent RingQueue[T]) to complement Peek() and Drain() while preserving FIFO order and buffer contents.

Changes:

  • Added Snapshot() to RingBuffer[T] and RingQueue[T], including API docs and comprehensive tests/benchmarks.
  • Refactored RingBuffer bulk operations by extracting a wrap-aware segments() helper used by Drain()/Clear()/Snapshot().
  • Updated benchmark sync targets, benchmark docs tables, and the changelog to reflect the new operation.

Reviewed changes

Copilot reviewed 9 out of 10 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
ringbuffer.go Refactors drain/clear via segments() and introduces RingBuffer.Snapshot().
ringbuffer_test.go Adds Snapshot test matrix (I1–I7) and Snapshot benchmarks.
ringqueue.go Adds RingQueue.Snapshot() with mutex-held copy semantics.
ringqueue_test.go Adds Snapshot tests (G1–G5) and a queue Snapshot benchmark.
cmd/benchsync/main.go Registers Snapshot benchmarks in default benchsync targets.
cmd/benchsync/main_test.go Updates golden output to include Snapshot benchmarks.
docs/ringbuffer.md Updates benchmark table and notes to include Snapshot performance.
docs/ringqueue.md Updates benchmark table to include Snapshot.
CHANGELOG.md Adds Unreleased changelog entry for Snapshot APIs.
.gitignore Ignores .mx/.

Comment thread docs/ringbuffer.md
Comment thread docs/ringqueue.md
1.G4 monotonicity passes if len(snap) < 2
2.no direct test of RingQueue wrap path -> G6
1.bench table mentioned it -> Methods missed it
2.shallow + lock semantics user-facing -> note
Copilot AI review requested due to automatic review settings May 11, 2026 14:27
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 9 out of 10 changed files in this pull request and generated no new comments.

@maxence2997 maxence2997 merged commit 8f589e3 into main May 11, 2026
5 checks passed
@maxence2997 maxence2997 deleted the feat/snapshot-api branch May 11, 2026 15:12
@maxence2997 maxence2997 mentioned this pull request May 11, 2026
4 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

feat: add Snapshot() for non-destructive FIFO copy

2 participants