Skip to content

Commit 0b2a614

Browse files
committed
Relax atomic loads and stores in Table
The allocation lock already enforces a happens-before relationship for the initialized length
1 parent 20a347e commit 0b2a614

File tree

1 file changed

+13
-5
lines changed

1 file changed

+13
-5
lines changed

src/table.rs

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -289,13 +289,13 @@ impl Table {
289289

290290
impl<'p, T: Slot> PageView<'p, T> {
291291
fn page_data(&self) -> &[PageDataEntry<T>] {
292-
let len = self.0.allocated.load(Ordering::Acquire);
292+
let len = self.0.allocated();
293293
// SAFETY: `len` is the initialized length of the page
294294
unsafe { slice::from_raw_parts(self.0.data.cast::<PageDataEntry<T>>().as_ptr(), len) }
295295
}
296296

297297
fn data(&self) -> &'p [T] {
298-
let len = self.0.allocated.load(Ordering::Acquire);
298+
let len = self.0.allocated();
299299
// SAFETY: `len` is the initialized length of the page
300300
unsafe { slice::from_raw_parts(self.0.data.cast::<T>().as_ptr(), len) }
301301
}
@@ -305,7 +305,7 @@ impl<'p, T: Slot> PageView<'p, T> {
305305
V: FnOnce(Id) -> T,
306306
{
307307
let _guard = self.0.allocation_lock.lock();
308-
let index = self.0.allocated.load(Ordering::Acquire);
308+
let index = self.0.allocated();
309309
if index >= PAGE_LEN {
310310
return Err(value);
311311
}
@@ -320,7 +320,8 @@ impl<'p, T: Slot> PageView<'p, T> {
320320

321321
// Update the length (this must be done after initialization as otherwise an uninitialized
322322
// read could occur!)
323-
self.0.allocated.store(index + 1, Ordering::Release);
323+
// Ordering: Relaxed is fine as the `allocation_lock` establishes a happens-before relationship
324+
self.0.allocated.store(index + 1, Ordering::Relaxed);
324325

325326
Ok(id)
326327
}
@@ -341,13 +342,20 @@ impl Page {
341342
}
342343
}
343344

345+
#[inline]
346+
fn allocated(&self) -> usize {
347+
// Ordering: Relaxed is fine as the `allocation_lock` establishes a happens-before
348+
// relationship
349+
self.allocated.load(Ordering::Relaxed)
350+
}
351+
344352
/// Retrieves the pointer for the given slot.
345353
///
346354
/// # Panics
347355
///
348356
/// If slot is out of bounds
349357
fn get(&self, slot: SlotIndex) -> *mut () {
350-
let len = self.allocated.load(Ordering::Acquire);
358+
let len = self.allocated();
351359
assert!(
352360
slot.0 < len,
353361
"out of bounds access `{slot:?}` (maximum slot `{len}`)"

0 commit comments

Comments
 (0)