Skip to content

Commit 4687e94

Browse files
committed
unchecked page access
1 parent 40d7844 commit 4687e94

File tree

2 files changed

+10
-25
lines changed

2 files changed

+10
-25
lines changed

src/table.rs

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ impl Table {
167167
#[inline]
168168
pub fn ingredient_index(&self, id: Id) -> IngredientIndex {
169169
let (page_idx, _) = split_id(id);
170-
self.pages[page_idx.0].ingredient
170+
unsafe { self.pages.get_unchecked(page_idx.0).ingredient }
171171
}
172172

173173
/// Get a reference to the data for `id`, which must have been allocated from this table with type `T`.
@@ -178,7 +178,7 @@ impl Table {
178178
pub(crate) fn get<T: Slot>(&self, id: Id) -> &T {
179179
let (page, slot) = split_id(id);
180180
let page_ref = self.page::<T>(page);
181-
&page_ref.data()[slot.0]
181+
unsafe { page_ref.data().get_unchecked(slot.0) }
182182
}
183183

184184
/// Get a raw pointer to the data for `id`, which must have been allocated from this table.
@@ -193,7 +193,7 @@ impl Table {
193193
pub(crate) fn get_raw<T: Slot>(&self, id: Id) -> *mut T {
194194
let (page, slot) = split_id(id);
195195
let page_ref = self.page::<T>(page);
196-
page_ref.page_data()[slot.0].get().cast::<T>()
196+
unsafe { page_ref.page_data().get_unchecked(slot.0).get().cast::<T>() }
197197
}
198198

199199
/// Gets a reference to the page which has slots of type `T`
@@ -203,7 +203,7 @@ impl Table {
203203
/// If `page` is out of bounds or the type `T` is incorrect.
204204
#[inline]
205205
pub(crate) fn page<T: Slot>(&self, page: PageIndex) -> PageView<'_, T> {
206-
self.pages[page.0].assert_type::<T>()
206+
unsafe { self.pages.get_unchecked(page.0).assert_type::<T>() }
207207
}
208208

209209
/// Allocate a new page for the given ingredient and with slots of type `T`
@@ -228,7 +228,7 @@ impl Table {
228228
current_revision: Revision,
229229
) -> MemoTableWithTypes<'_> {
230230
let (page, slot) = split_id(id);
231-
let page = &self.pages[page.0];
231+
let page = unsafe { self.pages.get_unchecked(page.0) };
232232
// SAFETY: We supply a proper slot pointer and the caller is required to pass the `current_revision`.
233233
let memos = unsafe { &*(page.slot_vtable.memos)(page.get(slot), current_revision) };
234234
// SAFETY: The `Page` keeps the correct memo types.
@@ -239,10 +239,7 @@ impl Table {
239239
pub(crate) fn memos_mut(&mut self, id: Id) -> MemoTableWithTypesMut<'_> {
240240
let (page, slot) = split_id(id);
241241
let page_index = page.0;
242-
let page = self
243-
.pages
244-
.get_mut(page_index)
245-
.unwrap_or_else(|| panic!("index `{page_index}` is uninitialized"));
242+
let page = unsafe { self.pages.get_unchecked_mut(page_index) };
246243
// SAFETY: We supply a proper slot pointer and the caller is required to pass the `current_revision`.
247244
let memos = unsafe { &mut *(page.slot_vtable.memos_mut)(page.get(slot)) };
248245
// SAFETY: The `Page` keeps the correct memo types.

src/zalsa.rs

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -203,10 +203,7 @@ impl Zalsa {
203203
#[inline]
204204
pub(crate) fn lookup_ingredient(&self, index: IngredientIndex) -> &dyn Ingredient {
205205
let index = index.as_usize();
206-
self.ingredients_vec
207-
.get(index)
208-
.unwrap_or_else(|| panic!("index `{index}` is uninitialized"))
209-
.as_ref()
206+
unsafe { self.ingredients_vec.get_unchecked(index).as_ref() }
210207
}
211208

212209
pub(crate) fn ingredient_index_for_memo(
@@ -329,10 +326,7 @@ impl Zalsa {
329326
index: IngredientIndex,
330327
) -> (&mut dyn Ingredient, &mut Runtime) {
331328
let index = index.as_usize();
332-
let ingredient = self
333-
.ingredients_vec
334-
.get_mut(index)
335-
.unwrap_or_else(|| panic!("index `{index}` is uninitialized"));
329+
let ingredient = unsafe { self.ingredients_vec.get_unchecked_mut(index) };
336330
(ingredient.as_mut(), &mut self.runtime)
337331
}
338332

@@ -359,11 +353,7 @@ impl Zalsa {
359353

360354
for (_, index) in self.ingredients_requiring_reset.iter() {
361355
let index = index.as_usize();
362-
let ingredient = self
363-
.ingredients_vec
364-
.get_mut(index)
365-
.unwrap_or_else(|| panic!("index `{index}` is uninitialized"));
366-
356+
let ingredient = unsafe { self.ingredients_vec.get_unchecked_mut(index) };
367357
ingredient.reset_for_new_revision(self.runtime.table_mut());
368358
}
369359

@@ -376,9 +366,7 @@ impl Zalsa {
376366
let _span = tracing::debug_span!("evict_lru").entered();
377367
for (_, index) in self.ingredients_requiring_reset.iter() {
378368
let index = index.as_usize();
379-
self.ingredients_vec
380-
.get_mut(index)
381-
.unwrap_or_else(|| panic!("index `{index}` is uninitialized"))
369+
unsafe { self.ingredients_vec.get_unchecked_mut(index) }
382370
.reset_for_new_revision(self.runtime.table_mut());
383371
}
384372
}

0 commit comments

Comments
 (0)