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
pub use imp::IngredientCache;
#[cfg(feature = "inventory")]
mod imp {
use crate::IngredientIndex;
use crate::plumbing::Ingredient;
use crate::sync::atomic::{self, AtomicU32, Ordering};
use crate::zalsa::Zalsa;
use std::marker::PhantomData;
/// Caches an ingredient index.
///
/// Note that all ingredients are statically registered with `inventory`, so their
/// indices should be stable across any databases.
pub struct IngredientCache<I>
where
I: Ingredient,
{
ingredient_index: AtomicU32,
phantom: PhantomData<fn() -> I>,
}
impl<I> Default for IngredientCache<I>
where
I: Ingredient,
{
fn default() -> Self {
Self::new()
}
}
impl<I> IngredientCache<I>
where
I: Ingredient,
{
const UNINITIALIZED: u32 = u32::MAX;
/// Create a new cache
pub const fn new() -> Self {
Self {
ingredient_index: atomic::AtomicU32::new(Self::UNINITIALIZED),
phantom: PhantomData,
}
}
/// Get a reference to the ingredient in the database.
///
/// If the ingredient index is not already in the cache, it will be loaded and cached.
///
/// # Safety
///
/// The `IngredientIndex` returned by the closure must reference a valid ingredient of
/// type `I` in the provided zalsa database.
pub unsafe fn get_or_create<'db>(
&self,
zalsa: &'db Zalsa,
load_index: impl Fn() -> IngredientIndex,
) -> &'db I {
let mut ingredient_index = self.ingredient_index.load(Ordering::Acquire);
if ingredient_index == Self::UNINITIALIZED {
ingredient_index = self.get_or_create_index_slow(load_index).as_u32();
};
// SAFETY: `ingredient_index` is initialized from a valid `IngredientIndex`.
let ingredient_index = unsafe { IngredientIndex::new_unchecked(ingredient_index) };
// SAFETY: There are a two cases here:
// - The `create_index` closure was called due to the data being uncached. In this
// case, the caller guarantees the index is in-bounds and has the correct type.
// - The index was cached. While the current database might not be the same database
// the ingredient was initially loaded from, the `inventory` feature is enabled, so
// ingredient indices are stable across databases. Thus the index is still in-bounds
// and has the correct type.
unsafe {
zalsa
.lookup_ingredient_unchecked(ingredient_index)
.assert_type_unchecked()
}
}
#[cold]
#[inline(never)]
fn get_or_create_index_slow(
&self,
load_index: impl Fn() -> IngredientIndex,
) -> IngredientIndex {
let ingredient_index = load_index();
// It doesn't matter if we overwrite any stores, as `create_index` should
// always return the same index when the `inventory` feature is enabled.
self.ingredient_index
.store(ingredient_index.as_u32(), Ordering::Release);
ingredient_index
}
}
}
#[cfg(not(feature = "inventory"))]
mod imp {
use crate::IngredientIndex;
use crate::nonce::Nonce;
use crate::plumbing::Ingredient;
use crate::sync::atomic::{AtomicU64, Ordering};
use crate::zalsa::{StorageNonce, Zalsa};
use std::marker::PhantomData;
use std::mem;
/// Caches an ingredient index.
///
/// With manual registration, ingredient indices can vary across databases,
/// but we can retain most of the benefit by optimizing for the the case of
/// a single database.
pub struct IngredientCache<I>
where
I: Ingredient,
{
// A packed representation of `Option<(Nonce<StorageNonce>, IngredientIndex)>`.
//
// This allows us to replace a lock in favor of an atomic load. This works thanks to `Nonce`
// having a niche, which means the entire type can fit into an `AtomicU64`.
cached_data: AtomicU64,
phantom: PhantomData<fn() -> I>,
}
impl<I> Default for IngredientCache<I>
where
I: Ingredient,
{
fn default() -> Self {
Self::new()
}
}
impl<I> IngredientCache<I>
where
I: Ingredient,
{
const UNINITIALIZED: u64 = 0;
/// Create a new cache
pub const fn new() -> Self {
Self {
cached_data: AtomicU64::new(Self::UNINITIALIZED),
phantom: PhantomData,
}
}
/// Get a reference to the ingredient in the database.
///
/// If the ingredient is not already in the cache, it will be created.
///
/// # Safety
///
/// The `IngredientIndex` returned by the closure must reference a valid ingredient of
/// type `I` in the provided zalsa database.
#[inline(always)]
pub unsafe fn get_or_create<'db>(
&self,
zalsa: &'db Zalsa,
create_index: impl Fn() -> IngredientIndex,
) -> &'db I {
let index = self.get_or_create_index(zalsa, create_index);
// SAFETY: There are a two cases here:
// - The `create_index` closure was called due to the data being uncached for the
// provided database. In this case, the caller guarantees the index is in-bounds
// and has the correct type.
// - We verified the index was cached for the same database, by the nonce check.
// Thus the initial safety argument still applies.
unsafe {
zalsa
.lookup_ingredient_unchecked(index)
.assert_type_unchecked::<I>()
}
}
pub fn get_or_create_index(
&self,
zalsa: &Zalsa,
create_index: impl Fn() -> IngredientIndex,
) -> IngredientIndex {
const _: () = assert!(
mem::size_of::<(Nonce<StorageNonce>, IngredientIndex)>() == mem::size_of::<u64>()
);
let cached_data = self.cached_data.load(Ordering::Acquire);
if cached_data == Self::UNINITIALIZED {
return self.get_or_create_index_slow(zalsa, create_index);
};
// Unpack our `u64` into the nonce and index.
//
// SAFETY: The lower bits of `cached_data` are initialized from a valid `IngredientIndex`.
let index = unsafe { IngredientIndex::new_unchecked(cached_data as u32) };
// SAFETY: We've checked against `UNINITIALIZED` (0) above and so the upper bits must be non-zero.
let nonce = crate::nonce::Nonce::<StorageNonce>::from_u32(unsafe {
std::num::NonZeroU32::new_unchecked((cached_data >> u32::BITS) as u32)
});
// The data was cached for a different database, we have to ensure the ingredient was
// created in ours.
if zalsa.nonce() != nonce {
return create_index();
}
index
}
#[cold]
#[inline(never)]
fn get_or_create_index_slow(
&self,
zalsa: &Zalsa,
create_index: impl Fn() -> IngredientIndex,
) -> IngredientIndex {
let index = create_index();
let nonce = zalsa.nonce().into_u32().get() as u64;
let packed = (nonce << u32::BITS) | (index.as_u32() as u64);
debug_assert_ne!(packed, IngredientCache::<I>::UNINITIALIZED);
// Discard the result, whether we won over the cache or not doesn't matter.
_ = self.cached_data.compare_exchange(
IngredientCache::<I>::UNINITIALIZED,
packed,
Ordering::Release,
Ordering::Relaxed,
);
// Use our locally computed index regardless of which one was cached.
index
}
}
}