Skip to content

Conversation

@lmbarros
Copy link

This PR contains some optimizations that we use (or plan to start using soon) in production at belena.io. To summarize, these are the main changes included here:

  • Add methods to efficiently read and write a single byte at a time
  • Avoid allocating memory on Bytes() (at the expense of having a second, pre-allocated buffer)
  • Add a faster code path for buffers whose size is a power-of-two.

Check the individual commit messages for details.

For our use case (in which we use a circular buffer on a tight loop), each of these optimizations brought considerable performance improvements.

dfunckt and others added 4 commits May 17, 2021 15:32
This reduces allocations and greatly improves performance when circbuf is used in tight loops that work with single byte streams.

Signed-off-by: Akis Kesoglou <akiskesoglou@gmail.com>
Signed-off-by: Akis Kesoglou <akiskesoglou@gmail.com>
Signed-off-by: Akis Kesoglou <akiskesoglou@gmail.com>
Some operations on the circular buffer make use of modulo arithmetic,
which was implemented (not surprisingly!) with the modulo operator `%`.
It turns out that the modulo operator is quite slow and therefore, on
tight loops, a substantial portion of the run time may be spent on it.

This commit adds a separate code path for buffers with power-of-two
sizes. For these, we can implement modulo arithmetic substantially more
efficiently using some bit manipulation.

This is not a breaking change: the public interface is exactly the same
as before. An optimized buffer is transparently created under the hood
whenever the requested size is a power-of-two.

The commit also adds benchmarks that perform various operations on
buffers that are and that are not power-of-two-sized.
@likebike
Copy link

Very nice PR. 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.

3 participants