Skip to content
Snippets Groups Projects
Commit 09682676 authored by Jorge Aparicio's avatar Jorge Aparicio
Browse files

don't use indexing to elide bound checks

parent 7e91814c
No related branches found
No related tags found
No related merge requests found
...@@ -48,7 +48,7 @@ where ...@@ -48,7 +48,7 @@ where
let buffer: &[T] = unsafe { self.buffer.as_ref() }; let buffer: &[T] = unsafe { self.buffer.as_ref() };
if self.head != self.tail { if self.head != self.tail {
let item = unsafe { ptr::read(&buffer[self.head]) }; let item = unsafe { ptr::read(buffer.as_ptr().offset(self.head as isize)) };
self.head = (self.head + 1) % n; self.head = (self.head + 1) % n;
Some(item) Some(item)
} else { } else {
...@@ -64,7 +64,7 @@ where ...@@ -64,7 +64,7 @@ where
if next_tail != self.head { if next_tail != self.head {
// NOTE(ptr::write) the memory slot that we are about to write to is uninitialized. We // NOTE(ptr::write) the memory slot that we are about to write to is uninitialized. We
// use `ptr::write` to avoid running `T`'s destructor on the uninitialized memory // use `ptr::write` to avoid running `T`'s destructor on the uninitialized memory
unsafe { ptr::write(&mut buffer[self.tail], item) } unsafe { ptr::write(buffer.as_mut_ptr().offset(self.tail as isize), item) }
self.tail = next_tail; self.tail = next_tail;
Ok(()) Ok(())
} else { } else {
......
...@@ -43,7 +43,7 @@ where ...@@ -43,7 +43,7 @@ where
// NOTE(volatile) the value of `tail` can change at any time in the context of the consumer // NOTE(volatile) the value of `tail` can change at any time in the context of the consumer
// so we inform this to the compiler using a volatile load // so we inform this to the compiler using a volatile load
if rb.head != unsafe { ptr::read_volatile(&rb.tail) } { if rb.head != unsafe { ptr::read_volatile(&rb.tail) } {
let item = unsafe { ptr::read(&buffer[rb.head]) }; let item = unsafe { ptr::read(buffer.as_ptr().offset(rb.head as isize)) };
rb.head = (rb.head + 1) % n; rb.head = (rb.head + 1) % n;
Some(item) Some(item)
} else { } else {
...@@ -78,7 +78,7 @@ where ...@@ -78,7 +78,7 @@ where
if next_tail != unsafe { ptr::read_volatile(&rb.head) } { if next_tail != unsafe { ptr::read_volatile(&rb.head) } {
// NOTE(ptr::write) the memory slot that we are about to write to is uninitialized. We // NOTE(ptr::write) the memory slot that we are about to write to is uninitialized. We
// use `ptr::write` to avoid running `T`'s destructor on the uninitialized memory // use `ptr::write` to avoid running `T`'s destructor on the uninitialized memory
unsafe { ptr::write(&mut buffer[rb.tail], item) } unsafe { ptr::write(buffer.as_mut_ptr().offset(rb.tail as isize), item) }
rb.tail = next_tail; rb.tail = next_tail;
Ok(()) Ok(())
} else { } else {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment