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
|
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(crate) 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(&self, current_revision: Revision) -> &MemoTable;
/// Mutably access the [`MemoTable`] for this slot.
fn memos_mut(&mut self) -> &mut MemoTable;
}
/// [Slot::memos]
type SlotMemosFnRaw = unsafe fn(*const (), current_revision: Revision) -> *const MemoTable;
/// [Slot::memos]
type SlotMemosFn<T> = unsafe fn(&T, current_revision: Revision) -> &MemoTable;
/// [Slot::memos_mut]
type SlotMemosMutFnRaw = unsafe fn(*mut ()) -> *mut MemoTable;
/// [Slot::memos_mut]
type SlotMemosMutFn<T> = fn(&mut T) -> &mut MemoTable;
struct SlotVTable {
layout: Layout,
/// [`Slot`] methods
memos: SlotMemosFnRaw,
memos_mut: SlotMemosMutFnRaw,
/// A drop impl to call when the own page drops
/// SAFETY: The caller is required to supply a correct data pointer to a `Box<PageDataEntry<T>>` and initialized length,
/// and correct 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 supply a correct data pointer and initialized length
unsafe {
let data = Box::from_raw(data.cast::<PageData<T>>());
for i in 0..initialized {
let item = data[i].get().cast::<T>();
memo_types.attach_memos_mut((*item).memos_mut()).drop();
ptr::drop_in_place(item);
}
},
layout: Layout::new::<T>(),
// SAFETY: The signatures are compatible
memos: unsafe { mem::transmute::<SlotMemosFn<T>, SlotMemosFnRaw>(T::memos) },
// SAFETY: The signatures are compatible
memos_mut: unsafe {
mem::transmute::<SlotMemosMutFn<T>, SlotMemosMutFnRaw>(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 "allocation lock" is held when we allocate a new entry.
///
/// It ensures that we can load the index, initialize it, and then update the length atomically
/// with respect to other allocations.
///
/// We could avoid it if we wanted, we'd just have to be a bit fancier in our reasoning
/// (for example, the bounds check in `Page::get` no longer suffices to truly guarantee
/// that the data is initialized).
allocation_lock: Mutex<()>,
/// 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,
/// The type name of what is stored as entries in data.
// FIXME: Move this into SlotVTable once const stable
slot_type_name: &'static str,
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)]
pub struct PageIndex(usize);
impl PageIndex {
#[inline]
fn new(idx: usize) -> Self {
debug_assert!(idx < MAX_PAGES);
Self(idx)
}
}
#[derive(Copy, Clone, Debug)]
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>()
}
/// 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>()
}
/// 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)))
}
/// Get the memo table associated with `id`
///
/// # Safety condition
///
/// The parameter `current_revision` MUST be the current revision
/// of the owner of database owning this table.
pub(crate) unsafe fn 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) }
}
#[cfg(feature = "salsa_unstable")]
pub(crate) fn slots_of<T: Slot>(&self) -> impl Iterator<Item = &T> + '_ {
self.pages
.iter()
.filter_map(|(_, page)| page.cast_type::<T>())
.flat_map(|view| view.data())
}
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<'p, T: Slot> PageView<'p, T> {
#[inline]
fn page_data(&self) -> &'p [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) -> &'p [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) }
}
pub(crate) fn allocate<V>(&self, page: PageIndex, value: V) -> Result<Id, V>
where
V: FnOnce(Id) -> T,
{
let _guard = self.0.allocation_lock.lock();
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: We acquired the allocation lock, so we have unique access to the UnsafeCell
// interior
unsafe { (*entry.get()).write(value(id)) };
// Update the length (this must be done after initialization as otherwise an uninitialized
// read could occur!)
self.0.allocated.store(index + 1, Ordering::Release);
Ok(id)
}
}
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 {
slot_vtable: SlotVTable::of::<T>(),
slot_type_id: TypeId::of::<T>(),
slot_type_name: std::any::type_name::<T>(),
ingredient,
allocated: Default::default(),
allocation_lock: Default::default(),
data: NonNull::from(Box::leak(data)).cast::<()>(),
memo_types,
}
}
/// 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> {
assert_eq!(
self.slot_type_id,
TypeId::of::<T>(),
"page has slot type `{:?}` but `{:?}` was expected",
self.slot_type_name,
std::any::type_name::<T>(),
);
PageView(self, PhantomData)
}
#[cfg(feature = "salsa_unstable")]
fn cast_type<T: Slot>(&self) -> Option<PageView<'_, T>> {
if self.slot_type_id == TypeId::of::<T>() {
Some(PageView(self, PhantomData))
} else {
None
}
}
}
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) };
}
}
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]
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))
}
|