Skip to content

Commit

Permalink
Fix performance
Browse files Browse the repository at this point in the history
  • Loading branch information
torrentg committed Jun 22, 2024
1 parent be4bc55 commit eb48581
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ firefox coverage/index.html &
| [Gerard Torrent](https://github.com/torrentg/) | Initial work<br/>Code maintainer|
| [G. Sliepen](https://codereview.stackexchange.com/users/129343/g-sliepen) | [Code review (I)](https://codereview.stackexchange.com/questions/281005/simple-c-circular-queue)<br/>[Code review (II)](https://codereview.stackexchange.com/questions/281152/a-not-so-simple-c20-circular-queue) |
| [Toby Speight](https://codereview.stackexchange.com/users/75307/toby-speight) | [Code review (II)](https://codereview.stackexchange.com/questions/281152/a-not-so-simple-c20-circular-queue) |
| [372046933](https://github.com/372046933) | [Fix compile](https://github.com/torrentg/cqueue/pull/3)<br/>[Fix performance issue](https://github.com/torrentg/cqueue/pull/4) |


## License
Expand Down
11 changes: 9 additions & 2 deletions cqueue.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
#include <memory>
#include <limits>
#include <compare>
#include <cstddef>
#include <utility>
#include <iterator>
#include <concepts>
#include <algorithm>
#include <stdexcept>
Expand All @@ -21,7 +23,7 @@ namespace gto {
* @see https://en.wikipedia.org/wiki/Circular_buffer
* @see https://github.com/torrentg/cqueue
* @note This class is not thread-safe.
* @version 1.0.5
* @version 1.0.6
* @tparam T Items type.
* @tparam Allocator Allocator.
*/
Expand Down Expand Up @@ -321,7 +323,12 @@ constexpr auto gto::cqueue<T, Allocator>::operator=(const cqueue &other) -> cque
*/
template<std::copyable T, typename Allocator>
constexpr auto gto::cqueue<T, Allocator>::getUncheckedIndex(size_type pos) const noexcept {
return (mFront + pos) & (mReserved - 1);
// case power of two (performance improvement x5)
if (mReserved > 1 && (mReserved & (mReserved - 1)) == 0) {
[[likely]]
return ((mFront + pos) & (mReserved - 1));
}
return ((mFront + pos) % (mReserved == 0 ? 1 : mReserved));
}

/**
Expand Down

0 comments on commit eb48581

Please sign in to comment.