Skip to content

Commit 950eb1a

Browse files
committed
unchecked page access
1 parent 8aaeb70 commit 950eb1a

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
@@ -220,10 +220,7 @@ impl Zalsa {
220220
#[inline]
221221
pub(crate) fn lookup_ingredient(&self, index: IngredientIndex) -> &dyn Ingredient {
222222
let index = index.as_u32() as usize;
223-
self.ingredients_vec
224-
.get(index)
225-
.unwrap_or_else(|| panic!("index `{index}` is uninitialized"))
226-
.as_ref()
223+
unsafe { self.ingredients_vec.get_unchecked(index).as_ref() }
227224
}
228225

229226
pub(crate) fn ingredient_index_for_memo(
@@ -346,10 +343,7 @@ impl Zalsa {
346343
index: IngredientIndex,
347344
) -> (&mut dyn Ingredient, &mut Runtime) {
348345
let index = index.as_u32() as usize;
349-
let ingredient = self
350-
.ingredients_vec
351-
.get_mut(index)
352-
.unwrap_or_else(|| panic!("index `{index}` is uninitialized"));
346+
let ingredient = unsafe { self.ingredients_vec.get_unchecked_mut(index) };
353347
(ingredient.as_mut(), &mut self.runtime)
354348
}
355349

@@ -376,11 +370,7 @@ impl Zalsa {
376370

377371
for (_, index) in self.ingredients_requiring_reset.iter() {
378372
let index = index.as_u32() as usize;
379-
let ingredient = self
380-
.ingredients_vec
381-
.get_mut(index)
382-
.unwrap_or_else(|| panic!("index `{index}` is uninitialized"));
383-
373+
let ingredient = unsafe { self.ingredients_vec.get_unchecked_mut(index) };
384374
ingredient.reset_for_new_revision(self.runtime.table_mut());
385375
}
386376

@@ -393,9 +383,7 @@ impl Zalsa {
393383
let _span = tracing::debug_span!("evict_lru").entered();
394384
for (_, index) in self.ingredients_requiring_reset.iter() {
395385
let index = index.as_u32() as usize;
396-
self.ingredients_vec
397-
.get_mut(index)
398-
.unwrap_or_else(|| panic!("index `{index}` is uninitialized"))
386+
unsafe { self.ingredients_vec.get_unchecked_mut(index) }
399387
.reset_for_new_revision(self.runtime.table_mut());
400388
}
401389
}

0 commit comments

Comments
 (0)