1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
use std::alloc::Layout;
use std::any::{Any, TypeId};
use std::cell::UnsafeCell;
use std::marker::PhantomData;
use std::mem::{self, MaybeUninit};
use std::ptr::{self, NonNull};
use std::slice;
use memo::MemoTable;
use rustc_hash::FxHashMap;
use crate::sync::atomic::{AtomicUsize, Ordering};
use crate::sync::{Arc, Mutex};
use crate::table::memo::{MemoTableTypes, MemoTableWithTypes, MemoTableWithTypesMut};
use crate::{Id, IngredientIndex, Revision};
pub(crate) mod memo;
const PAGE_LEN_BITS: usize = 10;
const PAGE_LEN_MASK: usize = PAGE_LEN - 1;
const PAGE_LEN: usize = 1 << PAGE_LEN_BITS;
const MAX_PAGES: usize = 1 << (u32::BITS as usize - PAGE_LEN_BITS);
/// A typed [`Page`] view.
pub(crate) struct PageView<'p, T: Slot>(&'p Page, PhantomData<&'p T>);
pub struct Table {
pages: boxcar::Vec<Page>,
/// Map from ingredient to non-full pages that are up for grabs
non_full_pages: Mutex<FxHashMap<IngredientIndex, Vec<PageIndex>>>,
}
/// # Safety
///
/// Implementors of this trait need to make sure that their type is unique with respect to
/// their owning ingredient as the allocation strategy relies on this.
pub unsafe trait Slot: Any + Send + Sync {
/// Access the [`MemoTable`][] for this slot.
///
/// # Safety condition
///
/// The current revision MUST be the current revision of the database containing this slot.
unsafe fn memos(slot: *const Self, current_revision: Revision) -> *const MemoTable;
/// Mutably access the [`MemoTable`] for this slot.
fn memos_mut(&mut self) -> &mut MemoTable;
}
/// [Slot::memos]
type SlotMemosFnErased = unsafe fn(*const (), current_revision: Revision) -> *const MemoTable;
/// [Slot::memos]
type SlotMemosFn<T> = unsafe fn(*const T, current_revision: Revision) -> *const MemoTable;
/// [Slot::memos_mut]
type SlotMemosMutFnErased = unsafe fn(*mut ()) -> *mut MemoTable;
/// [Slot::memos_mut]
type SlotMemosMutFn<T> = fn(&mut T) -> &mut MemoTable;
struct SlotVTable {
layout: Layout,
/// [`Slot`] methods
memos: SlotMemosFnErased,
memos_mut: SlotMemosMutFnErased,
/// The type name of what is stored as entries in data.
type_name: fn() -> &'static str,
/// A drop impl to call when the own page drops
/// SAFETY: The caller is required to supply a valid pointer to a `Box<PageDataEntry<T>>`, and
/// the correct initialized length and memo types.
drop_impl: unsafe fn(data: *mut (), initialized: usize, memo_types: &MemoTableTypes),
}
impl SlotVTable {
const fn of<T: Slot>() -> &'static Self {
const {
&Self {
drop_impl: |data, initialized, memo_types| {
// SAFETY: The caller is required to provide a valid data pointer.
let data = unsafe { Box::from_raw(data.cast::<PageData<T>>()) };
for i in 0..initialized {
let item = data[i].get().cast::<T>();
// SAFETY: The caller is required to provide a valid initialized length.
unsafe {
memo_types.attach_memos_mut((*item).memos_mut()).drop();
ptr::drop_in_place(item);
}
}
},
layout: Layout::new::<T>(),
type_name: std::any::type_name::<T>,
// SAFETY: The signatures are ABI-compatible.
memos: unsafe { mem::transmute::<SlotMemosFn<T>, SlotMemosFnErased>(T::memos) },
// SAFETY: The signatures are ABI-compatible.
memos_mut: unsafe {
mem::transmute::<SlotMemosMutFn<T>, SlotMemosMutFnErased>(T::memos_mut)
},
}
}
}
}
type PageDataEntry<T> = UnsafeCell<MaybeUninit<T>>;
type PageData<T> = [PageDataEntry<T>; PAGE_LEN];
struct Page {
/// The ingredient for elements on this page.
ingredient: IngredientIndex,
/// Number of elements of `data` that are initialized.
allocated: AtomicUsize,
/// The potentially uninitialized data of this page. As we initialize new entries, we increment `allocated`.
/// This is a box allocated `PageData<SlotType>`
data: NonNull<()>,
/// A vtable for the slot type stored in this page.
slot_vtable: &'static SlotVTable,
/// The type id of what is stored as entries in data.
// FIXME: Move this into SlotVTable once const stable
slot_type_id: TypeId,
memo_types: Arc<MemoTableTypes>,
}
// SAFETY: `Page` is `Send` as we make sure to only ever store `Slot` types in it which
// requires `Send`.`
unsafe impl Send for Page /* where for<M: Memo> M: Send */ {}
// SAFETY: `Page` is `Sync` as we make sure to only ever store `Slot` types in it which
// requires `Sync`.`
unsafe impl Sync for Page /* where for<M: Memo> M: Sync */ {}
#[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord)]
pub struct PageIndex(usize);
impl PageIndex {
#[inline]
fn new(idx: usize) -> Self {
debug_assert!(idx < MAX_PAGES);
Self(idx)
}
#[allow(dead_code)]
pub fn as_usize(&self) -> usize {
self.0
}
}
#[derive(Copy, Clone, Debug)]
pub struct SlotIndex(usize);
impl SlotIndex {
#[inline]
fn new(idx: usize) -> Self {
debug_assert!(idx < PAGE_LEN);
Self(idx)
}
}
impl Default for Table {
fn default() -> Self {
Self {
pages: boxcar::Vec::new(),
non_full_pages: Default::default(),
}
}
}
impl Table {
/// Returns the [`IngredientIndex`] for an [`Id`].
#[inline]
pub fn ingredient_index(&self, id: Id) -> IngredientIndex {
let (page_idx, _) = split_id(id);
self.pages[page_idx.0].ingredient
}
/// Get a reference to the data for `id`, which must have been allocated from this table with type `T`.
///
/// # Panics
///
/// If `id` is out of bounds or the does not have the type `T`.
pub(crate) fn get<T: Slot>(&self, id: Id) -> &T {
let (page, slot) = split_id(id);
let page_ref = self.page::<T>(page);
&page_ref.data()[slot.0]
}
/// Get a raw pointer to the data for `id`, which must have been allocated from this table.
///
/// # Panics
///
/// If `id` is out of bounds or the does not have the type `T`.
///
/// # Safety
///
/// See [`Page::get_raw`][].
pub(crate) fn get_raw<T: Slot>(&self, id: Id) -> *mut T {
let (page, slot) = split_id(id);
let page_ref = self.page::<T>(page);
page_ref.page_data()[slot.0].get().cast::<T>()
}
/// Returns the number of pages that have been allocated.
pub fn page_count(&self) -> usize {
self.pages.count()
}
/// Gets a reference to the page which has slots of type `T`
///
/// # Panics
///
/// If `page` is out of bounds or the type `T` is incorrect.
#[inline]
pub(crate) fn page<T: Slot>(&self, page: PageIndex) -> PageView<'_, T> {
self.pages[page.0].assert_type::<T>()
}
/// Force initialize the page at the given index.
///
/// If the page at the provided index was created using `push_uninit_page`, it
/// will be initialized using the provided ingredient data.
///
/// Otherwise, the page will be allocated.
///
/// # Panics
///
/// If `page` is out of bounds or the type `T` is incorrect.
#[inline]
#[allow(dead_code)]
pub(crate) fn force_page<T: Slot>(
&mut self,
page_idx: PageIndex,
ingredient: IngredientIndex,
memo_types: &Arc<MemoTableTypes>,
) {
let page = self.pages.get_mut(page_idx.0);
match page {
Some(page) => {
// Initialize the page if was created using `push_uninit_page`.
if page.slot_type_id == TypeId::of::<DummySlot>() {
*page = Page::new::<T>(ingredient, memo_types.clone());
}
// Ensure the page has the correct type.
page.assert_type::<T>();
}
None => {
// Create dummy pages until we reach the page we want.
while self.page_count() < page_idx.as_usize() {
// We make sure not to claim any intermediary pages for ourselves, as they may
// be required by a different ingredient when it is deserialized.
self.push_uninit_page();
}
let allocated_idx = self.push_page::<T>(ingredient, memo_types.clone());
assert_eq!(
allocated_idx, page_idx,
"allocated index does not match requested index"
);
}
};
}
/// Allocate a new page for the given ingredient and with slots of type `T`
#[inline]
pub(crate) fn push_page<T: Slot>(
&self,
ingredient: IngredientIndex,
memo_types: Arc<MemoTableTypes>,
) -> PageIndex {
PageIndex::new(self.pages.push(Page::new::<T>(ingredient, memo_types)))
}
/// Allocate an uninitialized page.
#[inline]
#[allow(dead_code)]
pub(crate) fn push_uninit_page(&self) -> PageIndex {
// Note that `DummySlot` is a ZST, so the memory wasted by any pages of ingredients
// that were not serialized should be negligible.
PageIndex::new(self.pages.push(Page::new::<DummySlot>(
IngredientIndex::new(0),
Arc::new(MemoTableTypes::default()),
)))
}
/// Get the memo table associated with `id` for the concrete type `T`.
///
/// # Safety
///
/// The parameter `current_revision` must be the current revision of the database
/// owning this table.
///
/// # Panics
///
/// If `page` is out of bounds or the type `T` is incorrect.
pub unsafe fn memos<T: Slot>(
&self,
id: Id,
current_revision: Revision,
) -> MemoTableWithTypes<'_> {
let (page, slot) = split_id(id);
let page = self.pages[page.0].assert_type::<T>();
let slot = &page.data()[slot.0];
// SAFETY: The caller is required to pass the `current_revision`.
let memos = unsafe { &*T::memos(slot, current_revision) };
// SAFETY: The `Page` keeps the correct memo types.
unsafe { page.0.memo_types.attach_memos(memos) }
}
/// Get the memo table associated with `id`.
///
/// Unlike `Table::memos`, this does not require a concrete type, and instead uses dynamic
/// dispatch.
///
/// # Safety
///
/// The parameter `current_revision` must be the current revision of the owner of database
/// owning this table.
pub unsafe fn dyn_memos(&self, id: Id, current_revision: Revision) -> MemoTableWithTypes<'_> {
let (page, slot) = split_id(id);
let page = &self.pages[page.0];
// SAFETY: We supply a proper slot pointer and the caller is required to pass the `current_revision`.
let memos = unsafe { &*(page.slot_vtable.memos)(page.get(slot), current_revision) };
// SAFETY: The `Page` keeps the correct memo types.
unsafe { page.memo_types.attach_memos(memos) }
}
/// Get the memo table associated with `id`
pub(crate) fn memos_mut(&mut self, id: Id) -> MemoTableWithTypesMut<'_> {
let (page, slot) = split_id(id);
let page_index = page.0;
let page = self
.pages
.get_mut(page_index)
.unwrap_or_else(|| panic!("index `{page_index}` is uninitialized"));
// SAFETY: We supply a proper slot pointer and the caller is required to pass the `current_revision`.
let memos = unsafe { &mut *(page.slot_vtable.memos_mut)(page.get(slot)) };
// SAFETY: The `Page` keeps the correct memo types.
unsafe { page.memo_types.attach_memos_mut(memos) }
}
pub(crate) fn slots_of<T: Slot>(&self) -> impl Iterator<Item = (Id, &T)> + '_ {
self.pages
.iter()
.filter_map(|(page_index, page)| Some((page_index, page.cast_type::<T>()?)))
.flat_map(move |(page_index, view)| {
view.data()
.iter()
.enumerate()
.map(move |(slot_index, value)| {
let id = make_id(PageIndex::new(page_index), SlotIndex::new(slot_index));
(id, value)
})
})
}
#[cold]
#[inline(never)]
pub(crate) fn fetch_or_push_page<T: Slot>(
&self,
ingredient: IngredientIndex,
memo_types: impl FnOnce() -> Arc<MemoTableTypes>,
) -> PageIndex {
if let Some(page) = self
.non_full_pages
.lock()
.get_mut(&ingredient)
.and_then(Vec::pop)
{
return page;
}
self.push_page::<T>(ingredient, memo_types())
}
pub(crate) fn record_unfilled_page(&self, ingredient: IngredientIndex, page: PageIndex) {
self.non_full_pages
.lock()
.entry(ingredient)
.or_default()
.push(page);
}
}
impl<'db, T: Slot> PageView<'db, T> {
#[inline]
fn page_data(&self) -> &'db [PageDataEntry<T>] {
let len = self.0.allocated.load(Ordering::Acquire);
// SAFETY: `len` is the initialized length of the page
unsafe { slice::from_raw_parts(self.0.data.cast::<PageDataEntry<T>>().as_ptr(), len) }
}
#[inline]
fn data(&self) -> &'db [T] {
let len = self.0.allocated.load(Ordering::Acquire);
// SAFETY: `len` is the initialized length of the page
unsafe { slice::from_raw_parts(self.0.data.cast::<T>().as_ptr(), len) }
}
/// Allocate a value in this page.
///
/// # Safety
///
/// The caller must be the unique writer to this page, i.e. `allocate` cannot be called
/// concurrently by multiple threads. Concurrent readers however, are fine.
#[inline]
pub(crate) unsafe fn allocate<V>(&self, page: PageIndex, value: V) -> Result<(Id, &'db T), V>
where
V: FnOnce(Id) -> T,
{
let index = self.0.allocated.load(Ordering::Acquire);
if index >= PAGE_LEN {
return Err(value);
}
// Initialize entry `index`
let id = make_id(page, SlotIndex::new(index));
let data = self.0.data.cast::<PageDataEntry<T>>();
// SAFETY: `index` is also guaranteed to be in bounds as per the check above.
let entry = unsafe { &*data.as_ptr().add(index) };
// SAFETY: The caller guarantees we are the unique writer, and readers will not attempt to
// access this index until we have updated the length.
unsafe { (*entry.get()).write(value(id)) };
// SAFETY: We just initialized the value above.
let value = unsafe { (*entry.get()).assume_init_ref() };
// Update the length now that we have initialized the value.
self.0.allocated.store(index + 1, Ordering::Release);
Ok((id, value))
}
}
impl Page {
#[inline]
fn new<T: Slot>(ingredient: IngredientIndex, memo_types: Arc<MemoTableTypes>) -> Self {
#[cfg(not(feature = "shuttle"))]
let data: Box<PageData<T>> =
Box::new([const { UnsafeCell::new(MaybeUninit::uninit()) }; PAGE_LEN]);
#[cfg(feature = "shuttle")]
let data = {
// Avoid stack overflows when using larger shuttle types.
let data = (0..PAGE_LEN)
.map(|_| UnsafeCell::new(MaybeUninit::uninit()))
.collect::<Box<[PageDataEntry<T>]>>();
let data: *mut [PageDataEntry<T>] = Box::into_raw(data);
// SAFETY: `*mut PageDataEntry<T>` and `*mut [PageDataEntry<T>; N]` have the same layout.
unsafe { Box::from_raw(data.cast::<PageDataEntry<T>>().cast::<PageData<T>>()) }
};
Self {
ingredient,
memo_types,
slot_vtable: SlotVTable::of::<T>(),
slot_type_id: TypeId::of::<T>(),
allocated: AtomicUsize::new(0),
data: NonNull::from(Box::leak(data)).cast::<()>(),
}
}
/// Retrieves the pointer for the given slot.
///
/// # Panics
///
/// If slot is out of bounds
fn get(&self, slot: SlotIndex) -> *mut () {
let len = self.allocated.load(Ordering::Acquire);
assert!(
slot.0 < len,
"out of bounds access `{slot:?}` (maximum slot `{len}`)"
);
// SAFETY: We have checked that the resulting pointer will be within bounds.
unsafe {
self.data
.as_ptr()
.byte_add(slot.0 * self.slot_vtable.layout.size())
}
}
#[inline]
fn assert_type<T: Slot>(&self) -> PageView<'_, T> {
if self.slot_type_id != TypeId::of::<T>() {
type_assert_failed::<T>(self);
}
PageView(self, PhantomData)
}
fn cast_type<T: Slot>(&self) -> Option<PageView<'_, T>> {
if self.slot_type_id == TypeId::of::<T>() {
Some(PageView(self, PhantomData))
} else {
None
}
}
}
/// This function is explicitly outlined to avoid debug machinery in the hot-path.
#[cold]
#[inline(never)]
fn type_assert_failed<T: 'static>(page: &Page) -> ! {
panic!(
"page has slot type `{:?}` but `{:?}` was expected",
(page.slot_vtable.type_name)(),
std::any::type_name::<T>(),
)
}
impl Drop for Page {
fn drop(&mut self) {
let len = *self.allocated.get_mut();
// SAFETY: We supply the data pointer and the initialized length
unsafe { (self.slot_vtable.drop_impl)(self.data.as_ptr(), len, &self.memo_types) };
}
}
/// A placeholder type representing the slots of an uninitialized `Page`.
struct DummySlot;
// SAFETY: The `DummySlot type is private.
unsafe impl Slot for DummySlot {
unsafe fn memos(_: *const Self, _: Revision) -> *const MemoTable {
unreachable!()
}
fn memos_mut(&mut self) -> &mut MemoTable {
unreachable!()
}
}
fn make_id(page: PageIndex, slot: SlotIndex) -> Id {
let page = page.0 as u32;
let slot = slot.0 as u32;
// SAFETY: `slot` is guaranteed to be small enough that the resulting Id won't be bigger than `Id::MAX_U32`
unsafe { Id::from_index((page << PAGE_LEN_BITS) | slot) }
}
#[inline]
pub fn split_id(id: Id) -> (PageIndex, SlotIndex) {
let index = id.index() as usize;
let slot = index & PAGE_LEN_MASK;
let page = index >> PAGE_LEN_BITS;
(PageIndex::new(page), SlotIndex::new(slot))
}