From eb49582837b9d37193c1b9830f5cf0d05659b977 Mon Sep 17 00:00:00 2001 From: John Wellbelove Date: Sat, 26 Mar 2022 18:30:33 +0000 Subject: [PATCH] Small optimisations for computing indexes for queues & cyclic_value. --- include/etl/circular_buffer.h | 42 ++++++++++++++++++++++++++++----- include/etl/cyclic_value.h | 4 ++-- include/etl/queue.h | 4 ++-- include/etl/queue_lockable.h | 2 +- include/etl/queue_mpmc_mutex.h | 2 +- include/etl/queue_spsc_atomic.h | 2 +- include/etl/queue_spsc_isr.h | 2 +- include/etl/queue_spsc_locked.h | 2 +- 8 files changed, 45 insertions(+), 15 deletions(-) diff --git a/include/etl/circular_buffer.h b/include/etl/circular_buffer.h index f042f621..42abcc61 100644 --- a/include/etl/circular_buffer.h +++ b/include/etl/circular_buffer.h @@ -95,7 +95,15 @@ namespace etl //************************************************************************* bool full() const { - return (in + 1U) % BUFFER_SIZE == out; + size_t i = in; + + ++i; + if (i == BUFFER_SIZE) ETL_UNLIKELY + { + i = 0U; + } + + return i == out; } //************************************************************************* @@ -126,6 +134,26 @@ namespace etl { } + //************************************************************************* + void increment_in() + { + ++in; + if (in == BUFFER_SIZE) ETL_UNLIKELY + { + in = 0U; + } + } + + //************************************************************************* + void increment_out() + { + ++out; + if (out == BUFFER_SIZE) ETL_UNLIKELY + { + out = 0U; + } + } + const size_type BUFFER_SIZE; size_type in; ///< Index to the next write. size_type out; ///< Index to the next read. @@ -835,14 +863,14 @@ namespace etl void push(const_reference item) { ::new (&pbuffer[in]) T(item); - in = (in + 1U) % BUFFER_SIZE; + increment_in(); // Did we catch up with the 'out' index? if (in == out) { // Forget about the oldest one. pbuffer[out].~T(); - out = (out + 1U) % BUFFER_SIZE; + this->increment_out(); } else { @@ -859,14 +887,14 @@ namespace etl void push(rvalue_reference item) { ::new (&pbuffer[in]) T(etl::move(item)); - in = (in + 1U) % BUFFER_SIZE; + increment_in(); // Did we catch up with the 'out' index? if (in == out) { // Forget about the oldest item. pbuffer[out].~T(); - out = (out + 1U) % BUFFER_SIZE; + increment_out(); } else { @@ -895,7 +923,7 @@ namespace etl { ETL_ASSERT(!empty(), ETL_ERROR(circular_buffer_empty)); pbuffer[out].~T(); - out = (out + 1U) % BUFFER_SIZE; + increment_out(); ETL_DECREMENT_DEBUG_COUNT } @@ -1013,6 +1041,8 @@ namespace etl } } + + pointer pbuffer; private: diff --git a/include/etl/cyclic_value.h b/include/etl/cyclic_value.h index 000b40ed..2c08f357 100644 --- a/include/etl/cyclic_value.h +++ b/include/etl/cyclic_value.h @@ -172,7 +172,7 @@ namespace etl //************************************************************************* cyclic_value& operator ++() { - if (value >= LAST) + if (value >= LAST) ETL_UNLIKELY { value = FIRST; } @@ -201,7 +201,7 @@ namespace etl //************************************************************************* cyclic_value& operator --() { - if (value <= FIRST) + if (value <= FIRST) ETL_UNLIKELY { value = LAST; } diff --git a/include/etl/queue.h b/include/etl/queue.h index cc498b88..b9eb00b9 100644 --- a/include/etl/queue.h +++ b/include/etl/queue.h @@ -187,7 +187,7 @@ namespace etl //************************************************************************* void add_in() { - if (++in == CAPACITY) + if (++in == CAPACITY) ETL_UNLIKELY { in = 0; } @@ -201,7 +201,7 @@ namespace etl //************************************************************************* void del_out() { - if (++out == CAPACITY) + if (++out == CAPACITY) ETL_UNLIKELY { out = 0; } diff --git a/include/etl/queue_lockable.h b/include/etl/queue_lockable.h index db795a7a..02ac59a8 100644 --- a/include/etl/queue_lockable.h +++ b/include/etl/queue_lockable.h @@ -185,7 +185,7 @@ namespace etl { ++index; - if (index == maximum) + if (index == maximum) ETL_UNLIKELY { index = 0; } diff --git a/include/etl/queue_mpmc_mutex.h b/include/etl/queue_mpmc_mutex.h index 16b2cc45..9b27179d 100644 --- a/include/etl/queue_mpmc_mutex.h +++ b/include/etl/queue_mpmc_mutex.h @@ -89,7 +89,7 @@ namespace etl { ++index; - if (index == maximum) + if (index == maximum) ETL_UNLIKELY { index = 0; } diff --git a/include/etl/queue_spsc_atomic.h b/include/etl/queue_spsc_atomic.h index 574e048c..c4dc9056 100644 --- a/include/etl/queue_spsc_atomic.h +++ b/include/etl/queue_spsc_atomic.h @@ -141,7 +141,7 @@ namespace etl { ++index; - if (index == maximum) + if (index == maximum) ETL_UNLIKELY { index = 0; } diff --git a/include/etl/queue_spsc_isr.h b/include/etl/queue_spsc_isr.h index 3e93fa8b..071ce2ee 100644 --- a/include/etl/queue_spsc_isr.h +++ b/include/etl/queue_spsc_isr.h @@ -421,7 +421,7 @@ namespace etl { ++index; - if (index == maximum) + if (index == maximum) ETL_UNLIKELY { index = 0; } diff --git a/include/etl/queue_spsc_locked.h b/include/etl/queue_spsc_locked.h index 185f64a6..facddeb5 100644 --- a/include/etl/queue_spsc_locked.h +++ b/include/etl/queue_spsc_locked.h @@ -118,7 +118,7 @@ namespace etl { ++index; - if (index == maximum) + if (index == maximum) ETL_UNLIKELY { index = 0; }