Skip to content

Commit f602d9f

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 7fe054e commit f602d9f

File tree

1 file changed

+12
-5
lines changed

1 file changed

+12
-5
lines changed

src/table.rs

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

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

296296
fn data(&self) -> &'p [T] {
297-
let len = self.0.allocated.load(Ordering::Acquire);
297+
let len = self.0.allocated();
298298
// SAFETY: `len` is the initialized length of the page
299299
unsafe { slice::from_raw_parts(self.0.data.cast::<T>().as_ptr(), len) }
300300
}
@@ -304,7 +304,7 @@ impl<'p, T: Slot> PageView<'p, T> {
304304
V: FnOnce(Id) -> T,
305305
{
306306
let _guard = self.0.allocation_lock.lock();
307-
let index = self.0.allocated.load(Ordering::Acquire);
307+
let index = self.0.allocated();
308308
if index >= PAGE_LEN {
309309
return Err(value);
310310
}
@@ -319,7 +319,8 @@ impl<'p, T: Slot> PageView<'p, T> {
319319

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

324325
Ok(id)
325326
}
@@ -340,13 +341,19 @@ impl Page {
340341
}
341342
}
342343

344+
fn allocated(&self) -> usize {
345+
// Ordering: Relaxed is fine as the `allocation_lock` establishes a happens-before
346+
// relationship
347+
self.allocated.load(Ordering::Relaxed)
348+
}
349+
343350
/// Retrieves the pointer for the given slot.
344351
///
345352
/// # Panics
346353
///
347354
/// If slot is out of bounds
348355
fn get(&self, slot: SlotIndex) -> *mut () {
349-
let len = self.allocated.load(Ordering::Acquire);
356+
let len = self.allocated();
350357
assert!(
351358
slot.0 < len,
352359
"out of bounds access `{slot:?}` (maximum slot `{len}`)"

0 commit comments

Comments
 (0)