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
use std::borrow::Cow;
use std::ptr::NonNull;
use crate::views::DatabaseDownCaster;
use crate::zalsa::{IngredientIndex, ZalsaDatabase};
use crate::zalsa_local::CancellationToken;
use crate::{Durability, Revision};
#[derive(Copy, Clone)]
pub struct RawDatabase<'db> {
pub(crate) ptr: NonNull<()>,
_marker: std::marker::PhantomData<&'db dyn Database>,
}
impl<'db, Db: Database + ?Sized> From<&'db Db> for RawDatabase<'db> {
#[inline]
fn from(db: &'db Db) -> Self {
RawDatabase {
ptr: NonNull::from(db).cast(),
_marker: std::marker::PhantomData,
}
}
}
impl<'db, Db: Database + ?Sized> From<&'db mut Db> for RawDatabase<'db> {
#[inline]
fn from(db: &'db mut Db) -> Self {
RawDatabase {
ptr: NonNull::from(db).cast(),
_marker: std::marker::PhantomData,
}
}
}
/// The trait implemented by all Salsa databases.
/// You can create your own subtraits of this trait using the `#[salsa::db]`(`crate::db`) procedural macro.
pub trait Database: Send + ZalsaDatabase + AsDynDatabase {
/// Enforces current LRU limits, evicting entries if necessary.
///
/// **WARNING:** Just like an ordinary write, this method triggers
/// cancellation. If you invoke it while a snapshot exists, it
/// will block until that snapshot is dropped -- if that snapshot
/// is owned by the current thread, this could trigger deadlock.
fn trigger_lru_eviction(&mut self) {
let zalsa_mut = self.zalsa_mut();
zalsa_mut.evict_lru();
}
/// A "synthetic write" causes the system to act *as though* some
/// input of durability `durability` has changed, triggering a new revision.
/// This is mostly useful for profiling scenarios.
///
/// **WARNING:** Just like an ordinary write, this method triggers
/// cancellation. If you invoke it while a snapshot exists, it
/// will block until that snapshot is dropped -- if that snapshot
/// is owned by the current thread, this could trigger deadlock.
fn synthetic_write(&mut self, durability: Durability) {
let zalsa_mut = self.zalsa_mut();
zalsa_mut.new_revision();
zalsa_mut.runtime_mut().report_tracked_write(durability);
}
/// This method cancels all outstanding computations.
/// If you invoke it while a snapshot exists, it
/// will block until that snapshot is dropped -- if that snapshot
/// is owned by the current thread, this could trigger deadlock.
fn trigger_cancellation(&mut self) {
let _ = self.zalsa_mut();
}
/// Retrieves a [`CancellationToken`] for the current database handle.
fn cancellation_token(&self) -> CancellationToken {
self.zalsa_local().cancellation_token()
}
/// Reports that the query depends on some state unknown to salsa.
///
/// Queries which report untracked reads will be re-executed in the next
/// revision.
fn report_untracked_read(&self) {
let (zalsa, zalsa_local) = self.zalsas();
zalsa_local.report_untracked_read(zalsa.current_revision())
}
/// Return the "debug name" (i.e., the struct name, etc) for an "ingredient",
/// which are the fine-grained components we use to track data. This is intended
/// for debugging and the contents of the returned string are not semver-guaranteed.
///
/// Ingredient indices can be extracted from [`DatabaseKeyIndex`](`crate::DatabaseKeyIndex`) values.
fn ingredient_debug_name(&self, ingredient_index: IngredientIndex) -> Cow<'_, str> {
Cow::Borrowed(
self.zalsa()
.lookup_ingredient(ingredient_index)
.debug_name(),
)
}
/// Starts unwinding the stack if the current revision is cancelled.
///
/// This method can be called by query implementations that perform
/// potentially expensive computations, in order to speed up propagation of
/// cancellation.
///
/// Cancellation will automatically be triggered by salsa on any query
/// invocation.
///
/// This method should not be overridden by `Database` implementors. A
/// `salsa_event` is emitted when this method is called, so that should be
/// used instead.
fn unwind_if_revision_cancelled(&self) {
let (zalsa, zalsa_local) = self.zalsas();
zalsa.unwind_if_revision_cancelled(zalsa_local);
}
/// Execute `op` with the database in thread-local storage for debug print-outs.
#[inline(always)]
fn attach<R>(&self, op: impl FnOnce(&Self) -> R) -> R
where
Self: Sized,
{
crate::attach::attach(self, || op(self))
}
#[cold]
#[inline(never)]
#[doc(hidden)]
fn zalsa_register_downcaster(&self) -> &DatabaseDownCaster<dyn Database> {
self.zalsa().views().downcaster_for::<dyn Database>()
// The no-op downcaster is special cased in view caster construction.
}
#[doc(hidden)]
#[inline(always)]
fn downcast(&self) -> &dyn Database
where
Self: Sized,
{
// No-op
self
}
}
/// Upcast to a `dyn Database`.
///
/// Only required because upcasting does not work for unsized generic parameters.
pub trait AsDynDatabase {
fn as_dyn_database(&self) -> &dyn Database;
}
impl<T: Database> AsDynDatabase for T {
#[inline(always)]
fn as_dyn_database(&self) -> &dyn Database {
self
}
}
pub fn current_revision<Db: ?Sized + Database>(db: &Db) -> Revision {
db.zalsa().current_revision()
}
#[cfg(feature = "persistence")]
mod persistence {
use crate::plumbing::Ingredient;
use crate::zalsa::Zalsa;
use crate::{Database, IngredientIndex, Runtime};
use std::fmt;
use serde::de::{self, DeserializeSeed, SeqAccess};
use serde::ser::SerializeMap;
impl dyn Database {
/// Returns a type implementing [`serde::Serialize`], that can be used to serialize the
/// current state of the database.
pub fn as_serialize(&mut self) -> impl serde::Serialize + '_ {
SerializeDatabase {
runtime: self.zalsa().runtime(),
ingredients: SerializeIngredients(self.zalsa()),
}
}
/// Deserialize the database using a [`serde::Deserializer`].
///
/// This method will modify the database in-place based on the serialized data.
pub fn deserialize<'db, D>(&mut self, deserializer: D) -> Result<(), D::Error>
where
D: serde::Deserializer<'db>,
{
DeserializeDatabase(self.zalsa_mut()).deserialize(deserializer)
}
}
#[derive(serde::Serialize)]
#[serde(rename = "Database")]
pub struct SerializeDatabase<'db> {
pub runtime: &'db Runtime,
pub ingredients: SerializeIngredients<'db>,
}
pub struct SerializeIngredients<'db>(pub &'db Zalsa);
impl serde::Serialize for SerializeIngredients<'_> {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
let SerializeIngredients(zalsa) = self;
let mut ingredients = zalsa
.ingredients()
.filter(|ingredient| ingredient.should_serialize(zalsa))
.collect::<Vec<_>>();
// Ensure structs are serialized before tracked functions, as deserializing a
// memo requires its input struct to have been deserialized.
ingredients.sort_by_key(|ingredient| ingredient.jar_kind());
let mut map = serializer.serialize_map(Some(ingredients.len()))?;
for ingredient in ingredients {
map.serialize_entry(
&ingredient.ingredient_index().as_u32(),
&SerializeIngredient(ingredient, zalsa),
)?;
}
map.end()
}
}
struct SerializeIngredient<'db>(&'db dyn Ingredient, &'db Zalsa);
impl serde::Serialize for SerializeIngredient<'_> {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
let mut result = None;
let mut serializer = Some(serializer);
// SAFETY: `<dyn Database>::as_serialize` take `&mut self`.
unsafe {
self.0.serialize(self.1, &mut |serialize| {
let serializer = serializer.take().expect(
"`Ingredient::serialize` must invoke the serialization callback only once",
);
result = Some(erased_serde::serialize(&serialize, serializer))
})
};
result.expect("`Ingredient::serialize` must invoke the serialization callback")
}
}
#[derive(serde::Deserialize)]
#[serde(field_identifier, rename_all = "lowercase")]
enum DatabaseField {
Runtime,
Ingredients,
}
pub struct DeserializeDatabase<'db>(pub &'db mut Zalsa);
impl<'de> de::DeserializeSeed<'de> for DeserializeDatabase<'_> {
type Value = ();
fn deserialize<D>(self, deserializer: D) -> Result<Self::Value, D::Error>
where
D: de::Deserializer<'de>,
{
// Note that we have to deserialize using a manual visitor here because the
// `Deserialize` derive does not support fields that use `DeserializeSeed`.
deserializer.deserialize_struct("Database", &["runtime", "ingredients"], self)
}
}
impl<'de> serde::de::Visitor<'de> for DeserializeDatabase<'_> {
type Value = ();
fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
formatter.write_str("struct Database")
}
fn visit_seq<V>(self, mut seq: V) -> Result<(), V::Error>
where
V: SeqAccess<'de>,
{
let mut runtime = seq
.next_element()?
.ok_or_else(|| de::Error::invalid_length(0, &self))?;
let () = seq
.next_element_seed(DeserializeIngredients(self.0))?
.ok_or_else(|| de::Error::invalid_length(1, &self))?;
self.0.runtime_mut().deserialize_from(&mut runtime);
Ok(())
}
fn visit_map<V>(self, mut map: V) -> Result<(), V::Error>
where
V: serde::de::MapAccess<'de>,
{
let mut runtime = None;
let mut ingredients = None;
while let Some(key) = map.next_key()? {
match key {
DatabaseField::Runtime => {
if runtime.is_some() {
return Err(serde::de::Error::duplicate_field("runtime"));
}
runtime = Some(map.next_value()?);
}
DatabaseField::Ingredients => {
if ingredients.is_some() {
return Err(serde::de::Error::duplicate_field("ingredients"));
}
ingredients = Some(map.next_value_seed(DeserializeIngredients(self.0))?);
}
}
}
let mut runtime = runtime.ok_or_else(|| serde::de::Error::missing_field("runtime"))?;
let () = ingredients.ok_or_else(|| serde::de::Error::missing_field("ingredients"))?;
self.0.runtime_mut().deserialize_from(&mut runtime);
Ok(())
}
}
struct DeserializeIngredients<'db>(&'db mut Zalsa);
impl<'de> serde::de::Visitor<'de> for DeserializeIngredients<'_> {
type Value = ();
fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
formatter.write_str("a map")
}
fn visit_map<M>(self, mut access: M) -> Result<Self::Value, M::Error>
where
M: serde::de::MapAccess<'de>,
{
let DeserializeIngredients(zalsa) = self;
while let Some(index) = access.next_key::<u32>()? {
let index = IngredientIndex::new(index);
// Remove the ingredient temporarily, to avoid holding an overlapping mutable borrow
// to the ingredient as well as the database.
let mut ingredient = zalsa.take_ingredient(index);
// Deserialize the ingredient.
access.next_value_seed(DeserializeIngredient(&mut *ingredient, zalsa))?;
zalsa.replace_ingredient(index, ingredient);
}
Ok(())
}
}
impl<'de> serde::de::DeserializeSeed<'de> for DeserializeIngredients<'_> {
type Value = ();
fn deserialize<D>(self, deserializer: D) -> Result<Self::Value, D::Error>
where
D: serde::Deserializer<'de>,
{
deserializer.deserialize_map(self)
}
}
struct DeserializeIngredient<'db>(&'db mut dyn Ingredient, &'db mut Zalsa);
impl<'de> serde::de::DeserializeSeed<'de> for DeserializeIngredient<'_> {
type Value = ();
fn deserialize<D>(self, deserializer: D) -> Result<Self::Value, D::Error>
where
D: serde::Deserializer<'de>,
{
let deserializer = &mut <dyn erased_serde::Deserializer>::erase(deserializer);
self.0
.deserialize(self.1, deserializer)
.map_err(serde::de::Error::custom)
}
}
}
#[cfg(feature = "salsa_unstable")]
pub use memory_usage::IngredientInfo;
#[cfg(feature = "salsa_unstable")]
pub(crate) use memory_usage::{MemoInfo, SlotInfo};
#[cfg(feature = "salsa_unstable")]
mod memory_usage {
use hashbrown::HashMap;
use crate::Database;
impl dyn Database {
/// Returns memory usage information about ingredients in the database.
pub fn memory_usage(&self) -> DatabaseInfo {
let mut queries = HashMap::new();
let mut structs = Vec::new();
for input_ingredient in self.zalsa().ingredients() {
let Some(input_info) = input_ingredient.memory_usage(self) else {
continue;
};
let mut size_of_fields = 0;
let mut size_of_metadata = 0;
let mut count = 0;
let mut heap_size_of_fields = None;
for input_slot in input_info {
count += 1;
size_of_fields += input_slot.size_of_fields;
size_of_metadata += input_slot.size_of_metadata;
if let Some(slot_heap_size) = input_slot.heap_size_of_fields {
heap_size_of_fields =
Some(heap_size_of_fields.unwrap_or_default() + slot_heap_size);
}
for memo in input_slot.memos {
let info = queries.entry(memo.debug_name).or_insert(IngredientInfo {
debug_name: memo.output.debug_name,
..Default::default()
});
info.count += 1;
info.size_of_fields += memo.output.size_of_fields;
info.size_of_metadata += memo.output.size_of_metadata;
if let Some(memo_heap_size) = memo.output.heap_size_of_fields {
info.heap_size_of_fields =
Some(info.heap_size_of_fields.unwrap_or_default() + memo_heap_size);
}
}
}
structs.push(IngredientInfo {
count,
size_of_fields,
size_of_metadata,
heap_size_of_fields,
debug_name: input_ingredient.debug_name(),
});
}
DatabaseInfo { structs, queries }
}
}
/// Memory usage information about ingredients in the Salsa database.
pub struct DatabaseInfo {
/// Information about any Salsa structs.
pub structs: Vec<IngredientInfo>,
/// Memory usage information for memoized values of a given query, keyed
/// by the query function name.
pub queries: HashMap<&'static str, IngredientInfo>,
}
/// Information about instances of a particular Salsa ingredient.
#[derive(Default, Debug, PartialEq, Eq, PartialOrd, Ord)]
pub struct IngredientInfo {
debug_name: &'static str,
count: usize,
size_of_metadata: usize,
size_of_fields: usize,
heap_size_of_fields: Option<usize>,
}
impl IngredientInfo {
/// Returns the debug name of the ingredient.
pub fn debug_name(&self) -> &'static str {
self.debug_name
}
/// Returns the total stack size of the fields of any instances of this ingredient, in bytes.
pub fn size_of_fields(&self) -> usize {
self.size_of_fields
}
/// Returns the total heap size of the fields of any instances of this ingredient, in bytes.
///
/// Returns `None` if the ingredient doesn't specify a `heap_size` function.
pub fn heap_size_of_fields(&self) -> Option<usize> {
self.heap_size_of_fields
}
/// Returns the total size of Salsa metadata of any instances of this ingredient, in bytes.
pub fn size_of_metadata(&self) -> usize {
self.size_of_metadata
}
/// Returns the number of instances of this ingredient.
pub fn count(&self) -> usize {
self.count
}
}
/// Memory usage information about a particular instance of struct, input or output.
pub struct SlotInfo {
pub(crate) debug_name: &'static str,
pub(crate) size_of_metadata: usize,
pub(crate) size_of_fields: usize,
pub(crate) heap_size_of_fields: Option<usize>,
pub(crate) memos: Vec<MemoInfo>,
}
/// Memory usage information about a particular memo.
pub struct MemoInfo {
pub(crate) debug_name: &'static str,
pub(crate) output: SlotInfo,
}
}