mirror of
https://github.com/rust-lang/rust.git
synced 2024-11-25 08:13:41 +00:00
Make circular buffer use only power-of-two sizes, cheaper arithmetic.
This commit is contained in:
parent
d786469785
commit
60b0486c11
@ -4,6 +4,14 @@
|
||||
|
||||
#include "rust_internal.h"
|
||||
|
||||
bool
|
||||
is_power_of_two(size_t value) {
|
||||
if (value > 0) {
|
||||
return (value & (value - 1)) == 0;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
circular_buffer::circular_buffer(rust_dom *dom, size_t unit_sz) :
|
||||
dom(dom),
|
||||
_buffer_sz(INITIAL_CIRCULAR_BUFFFER_SIZE_IN_UNITS * unit_sz),
|
||||
@ -37,9 +45,10 @@ circular_buffer::~circular_buffer() {
|
||||
void
|
||||
circular_buffer::transfer(void *dst) {
|
||||
I(dom, dst);
|
||||
I(dom, is_power_of_two(_buffer_sz));
|
||||
uint8_t *ptr = (uint8_t *) dst;
|
||||
for (size_t i = 0; i < _unread; i += _unit_sz) {
|
||||
memcpy(&ptr[i], &_buffer[(_next + i) % _buffer_sz], _unit_sz);
|
||||
memcpy(&ptr[i], &_buffer[(_next + i) & (_buffer_sz - 1)], _unit_sz);
|
||||
}
|
||||
}
|
||||
|
||||
@ -67,11 +76,12 @@ circular_buffer::enqueue(void *src) {
|
||||
"unread: %d, buffer_sz: %d, unit_sz: %d",
|
||||
_unread, _buffer_sz, _unit_sz);
|
||||
|
||||
I(dom, is_power_of_two(_buffer_sz));
|
||||
I(dom, _unread < _buffer_sz);
|
||||
I(dom, _unread + _unit_sz <= _buffer_sz);
|
||||
|
||||
// Copy data
|
||||
size_t i = (_next + _unread) % _buffer_sz;
|
||||
size_t i = (_next + _unread) & (_buffer_sz - 1);
|
||||
memcpy(&_buffer[i], src, _unit_sz);
|
||||
_unread += _unit_sz;
|
||||
|
||||
|
@ -20,7 +20,9 @@ public:
|
||||
bool is_empty();
|
||||
|
||||
private:
|
||||
// Size of the buffer in bytes.
|
||||
// Size of the buffer in bytes, should always be a power of two so that
|
||||
// modulo arithmetic (x % _buffer_sz) can optimized away with
|
||||
// (x & (_buffer_sz - 1)).
|
||||
size_t _buffer_sz;
|
||||
|
||||
// Size of the data unit in bytes.
|
||||
|
Loading…
Reference in New Issue
Block a user