Skip to content

Why increase capacity to next power of 2 - 1? #5

@EkardNT

Description

@EkardNT

The documentation states that the size of the circular buffer is increased to the next power of 2 above or including the provided capacity, and I've confirmed that behavior with the following test.

#[cfg(test)]
#[test]
fn capacity_demo() {
    let mut c = CircBuf::with_capacity(10).unwrap();
    assert_eq!(c.cap(), 15);
    assert_eq!(c.get_avail()[0].len(), 15);
}

The test also shows the behavior of the actual available capacity being one less than the power of 2. My question is, why are both of these behaviors present?

  • Why is the capacity rounded to the next power of 2?
  • Why is the available capacity one less than the next power of 2?

For the first question, I thought maybe the package was using bitmasking of some form that only works on power-of-2 array lengths, but I looked through the code and wasn't able to find anything of that nature. For example, advance_read and advance_write both use modular arithmetic which would work just as well with non-power-of-2 lengths as with, as far as I can tell.

For the second question, there is this explanation in the code: [The capacity] is equal to one less than the length of the underlying buffer because we cannot let the write cursor to ever circle back to being equal with the read cursor. That's clear enough, but I don't understand why not just increase the length of the buffer by 1 to hide this implementation detail from the user. Perhaps it is tied to the need to have power-of-2 lengths? Which leads back to the first question.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions