Skip to content

Update ring_buffer.go#2

Merged
peterbourgon merged 2 commits into
peterbourgon:mainfrom
yshngg:patch-1
May 27, 2025
Merged

Update ring_buffer.go#2
peterbourgon merged 2 commits into
peterbourgon:mainfrom
yshngg:patch-1

Conversation

@yshngg

@yshngg yshngg commented May 6, 2025

Copy link
Copy Markdown
Contributor

The read tail is len-1 values back from the read head.

The read tail is len-1 values back from the read head.
@peterbourgon

peterbourgon commented May 22, 2025

Copy link
Copy Markdown
Owner
tailidx := headidx - rb.len + 1

Is the read tail (i.e. tailidx) not len+1 values back from the read head (i.e. headidx)?

@yshngg

yshngg commented May 22, 2025

Copy link
Copy Markdown
Contributor Author

Hi @peterbourgon , thank you for responding!

Oh, sorry, I misunderstood "back from". I think you meant "back relative to the read direction".

@yshngg yshngg closed this May 22, 2025
@yshngg

yshngg commented May 26, 2025

Copy link
Copy Markdown
Contributor Author

Sorry to bother you again — I think I'm getting confused once more. 😂

This seems to be a comment error.

tailidx := headidx - rb.len + 1
// is equivalent to
tailidx := headidx - (rb.len - 1)

So the read tail is rb.len-1 values back from the read head.

Example:

- Partially filled buffer (e.g., len=3, cap=5):
  Buffer: [1, 2, 3, _, _]
  headidx: 2 (value 3)
  tailidx: 0 (value 1)
  The read tail is 2 values back from the read head.

- Fully filled buffer (e.g., len=5, cap=5):
  Buffer: [1, 2, 3, 4, 5]
  headidx: 4 (value 5)
  tailidx: 0 (value 1)
  The read tail is 4 values back from the read head.

@yshngg yshngg reopened this May 26, 2025
@peterbourgon

Copy link
Copy Markdown
Owner

You're right, the comment should read len-1, and the expr should be tailidx := headidx - (rb.len - 1). Please do make those changes in the PR, and sorry for the back-and-forth.

@yshngg

yshngg commented May 27, 2025

Copy link
Copy Markdown
Contributor Author

@peterbourgon , review please. 🙏

@peterbourgon peterbourgon merged commit 251c0e9 into peterbourgon:main May 27, 2025
2 checks passed
@peterbourgon

Copy link
Copy Markdown
Owner

Thanks!

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.

2 participants