Skip to content

Circular buffer should use if statement instead of % to handle index overruns #7701

@cmorty

Description

@cmorty

Description

CircularBuffer uses modulo to handle index overruns of _tail and _head. Unless Buffersize is 2^x or some other magic number the compiler must make a division, which is pretty expensive in terms of computation power, even with hardware support.
As _tail and _head are only ever incremented by one, an if(_tail == BufferSize) {_tail = 0}; should suffice. This will provide much better performance, especially with -O0 and -O1.
I can provide a pull-request if requested.

Issue request type

[ ] Question
[ x ] Enhancement
[ ] Bug

Metadata

Metadata

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions