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
use crossbeam::atomic::AtomicCell;
use crossbeam::queue::SegQueue;
use std::fmt;
use std::hash::Hash;
use std::marker::PhantomData;
use crate::durability::Durability;
use crate::id::AsId;
use crate::ingredient::{fmt_index, IngredientRequiresReset};
use crate::key::DependencyIndex;
use crate::runtime::local_state::QueryOrigin;
use crate::runtime::Runtime;
use crate::DatabaseKeyIndex;
use super::hash::FxDashMap;
use super::ingredient::Ingredient;
use super::routes::IngredientIndex;
use super::Revision;
pub trait InternedId: AsId {}
impl<T: AsId> InternedId for T {}
pub trait InternedData: Sized + Eq + Hash + Clone {}
impl<T: Eq + Hash + Clone> InternedData for T {}
pub struct InternedIngredient<Id: InternedId, Data: InternedData> {
ingredient_index: IngredientIndex,
key_map: FxDashMap<Data, Id>,
value_map: FxDashMap<Id, Box<Data>>,
counter: AtomicCell<u32>,
reset_at: Revision,
deleted_entries: SegQueue<Box<Data>>,
debug_name: &'static str,
}
impl<Id, Data> InternedIngredient<Id, Data>
where
Id: InternedId,
Data: InternedData,
{
pub fn new(ingredient_index: IngredientIndex, debug_name: &'static str) -> Self {
Self {
ingredient_index,
key_map: Default::default(),
value_map: Default::default(),
counter: AtomicCell::default(),
reset_at: Revision::start(),
deleted_entries: Default::default(),
debug_name,
}
}
pub fn intern(&self, runtime: &Runtime, data: Data) -> Id {
runtime.report_tracked_read(
DependencyIndex::for_table(self.ingredient_index),
Durability::MAX,
self.reset_at,
);
if let Some(id) = self.key_map.get(&data) {
return *id;
}
match self.key_map.entry(data.clone()) {
dashmap::mapref::entry::Entry::Occupied(entry) => *entry.get(),
dashmap::mapref::entry::Entry::Vacant(entry) => {
let next_id = self.counter.fetch_add(1);
let next_id = Id::from_id(crate::id::Id::from_u32(next_id));
let old_value = self.value_map.insert(next_id, Box::new(data));
assert!(
old_value.is_none(),
"next_id is guaranteed to be unique, bar overflow"
);
entry.insert(next_id);
next_id
}
}
}
pub(crate) fn reset_at(&self) -> Revision {
self.reset_at
}
pub fn reset(&mut self, revision: Revision) {
assert!(revision > self.reset_at);
self.reset_at = revision;
self.key_map.clear();
self.value_map.clear();
}
#[track_caller]
pub fn data<'db>(&'db self, runtime: &'db Runtime, id: Id) -> &'db Data {
runtime.report_tracked_read(
DependencyIndex::for_table(self.ingredient_index),
Durability::MAX,
self.reset_at,
);
let data = match self.value_map.get(&id) {
Some(d) => d,
None => {
panic!("no data found for id `{:?}`", id)
}
};
unsafe { transmute_lifetime(self, &**data) }
}
pub(super) fn ingredient_index(&self) -> IngredientIndex {
self.ingredient_index
}
pub(crate) fn delete_index(&self, id: Id) {
let (_, key) = self
.value_map
.remove(&id)
.unwrap_or_else(|| panic!("No entry for id `{:?}`", id));
self.key_map.remove(&key);
self.deleted_entries.push(key);
}
pub(crate) fn clear_deleted_indices(&mut self) {
std::mem::take(&mut self.deleted_entries);
}
}
unsafe fn transmute_lifetime<'t, 'u, T, U>(_t: &'t T, u: &'u U) -> &'t U {
std::mem::transmute(u)
}
impl<DB: ?Sized, Id, Data> Ingredient<DB> for InternedIngredient<Id, Data>
where
Id: InternedId,
Data: InternedData,
{
fn maybe_changed_after(&self, _db: &DB, _input: DependencyIndex, revision: Revision) -> bool {
revision < self.reset_at
}
fn cycle_recovery_strategy(&self) -> crate::cycle::CycleRecoveryStrategy {
crate::cycle::CycleRecoveryStrategy::Panic
}
fn origin(&self, _key_index: crate::Id) -> Option<QueryOrigin> {
None
}
fn mark_validated_output(
&self,
_db: &DB,
executor: DatabaseKeyIndex,
output_key: Option<crate::Id>,
) {
unreachable!(
"mark_validated_output({:?}, {:?}): input cannot be the output of a tracked function",
executor, output_key
);
}
fn remove_stale_output(
&self,
_db: &DB,
executor: DatabaseKeyIndex,
stale_output_key: Option<crate::Id>,
) {
unreachable!(
"remove_stale_output({:?}, {:?}): interned ids are not outputs",
executor, stale_output_key
);
}
fn reset_for_new_revision(&mut self) {
panic!("unexpected call to `reset_for_new_revision`")
}
fn salsa_struct_deleted(&self, _db: &DB, _id: crate::Id) {
panic!("unexpected call: interned ingredients do not register for salsa struct deletion events");
}
fn fmt_index(&self, index: Option<crate::Id>, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt_index(self.debug_name, index, fmt)
}
}
impl<Id, Data> IngredientRequiresReset for InternedIngredient<Id, Data>
where
Id: InternedId,
Data: InternedData,
{
const RESET_ON_NEW_REVISION: bool = false;
}
pub struct IdentityInterner<Id: AsId> {
data: PhantomData<Id>,
}
impl<Id: AsId> IdentityInterner<Id> {
#[allow(clippy::new_without_default)]
pub fn new() -> Self {
IdentityInterner { data: PhantomData }
}
pub fn intern(&self, _runtime: &Runtime, id: Id) -> Id {
id
}
pub fn data(&self, _runtime: &Runtime, id: Id) -> (Id,) {
(id,)
}
}