mirror of
https://github.com/ETLCPP/etl.git
synced 2026-11-09 14:49:53 +08:00
Small optimisations for computing indexes for queues & cyclic_value.
This commit is contained in:
parent
1b5da4a4b2
commit
eb49582837
@ -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:
|
||||
|
||||
@ -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;
|
||||
}
|
||||
|
||||
@ -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;
|
||||
}
|
||||
|
||||
@ -185,7 +185,7 @@ namespace etl
|
||||
{
|
||||
++index;
|
||||
|
||||
if (index == maximum)
|
||||
if (index == maximum) ETL_UNLIKELY
|
||||
{
|
||||
index = 0;
|
||||
}
|
||||
|
||||
@ -89,7 +89,7 @@ namespace etl
|
||||
{
|
||||
++index;
|
||||
|
||||
if (index == maximum)
|
||||
if (index == maximum) ETL_UNLIKELY
|
||||
{
|
||||
index = 0;
|
||||
}
|
||||
|
||||
@ -141,7 +141,7 @@ namespace etl
|
||||
{
|
||||
++index;
|
||||
|
||||
if (index == maximum)
|
||||
if (index == maximum) ETL_UNLIKELY
|
||||
{
|
||||
index = 0;
|
||||
}
|
||||
|
||||
@ -421,7 +421,7 @@ namespace etl
|
||||
{
|
||||
++index;
|
||||
|
||||
if (index == maximum)
|
||||
if (index == maximum) ETL_UNLIKELY
|
||||
{
|
||||
index = 0;
|
||||
}
|
||||
|
||||
@ -118,7 +118,7 @@ namespace etl
|
||||
{
|
||||
++index;
|
||||
|
||||
if (index == maximum)
|
||||
if (index == maximum) ETL_UNLIKELY
|
||||
{
|
||||
index = 0;
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user