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
use std::fmt;
use crate::{
cycle::CycleRecoveryStrategy,
hash::FxDashMap,
ingredient::{fmt_index, Ingredient, IngredientRequiresReset},
key::DependencyIndex,
runtime::local_state::QueryOrigin,
storage::HasJar,
DatabaseKeyIndex, Event, EventKind, IngredientIndex, Revision, Runtime,
};
pub trait Accumulator {
type Data: Clone;
type Jar;
fn accumulator_ingredient<Db>(db: &Db) -> &AccumulatorIngredient<Self::Data>
where
Db: ?Sized + HasJar<Self::Jar>;
}
pub struct AccumulatorIngredient<Data: Clone> {
index: IngredientIndex,
map: FxDashMap<DatabaseKeyIndex, AccumulatedValues<Data>>,
debug_name: &'static str,
}
struct AccumulatedValues<Data> {
produced_at: Revision,
values: Vec<Data>,
}
impl<Data: Clone> AccumulatorIngredient<Data> {
pub fn new(index: IngredientIndex, debug_name: &'static str) -> Self {
Self {
map: FxDashMap::default(),
index,
debug_name,
}
}
fn dependency_index(&self) -> DependencyIndex {
DependencyIndex {
ingredient_index: self.index,
key_index: None,
}
}
pub fn push(&self, runtime: &Runtime, value: Data) {
let current_revision = runtime.current_revision();
let (active_query, _) = match runtime.active_query() {
Some(pair) => pair,
None => {
panic!("cannot accumulate values outside of an active query")
}
};
let mut accumulated_values = self.map.entry(active_query).or_insert(AccumulatedValues {
values: vec![],
produced_at: current_revision,
});
if !runtime.is_output_of_active_query(self.dependency_index()) {
accumulated_values.values.truncate(0);
accumulated_values.produced_at = current_revision;
}
runtime.add_output(self.dependency_index());
accumulated_values.values.push(value);
}
pub(crate) fn produced_by(
&self,
runtime: &Runtime,
query: DatabaseKeyIndex,
output: &mut Vec<Data>,
) {
let current_revision = runtime.current_revision();
if let Some(v) = self.map.get(&query) {
runtime.report_untracked_read();
let AccumulatedValues {
values,
produced_at,
} = v.value();
if *produced_at == current_revision {
output.extend(values.iter().cloned());
}
}
}
}
impl<DB: ?Sized, Data> Ingredient<DB> for AccumulatorIngredient<Data>
where
DB: crate::Database,
Data: Clone,
{
fn maybe_changed_after(&self, _db: &DB, _input: DependencyIndex, _revision: Revision) -> bool {
panic!("nothing should ever depend on an accumulator directly")
}
fn cycle_recovery_strategy(&self) -> CycleRecoveryStrategy {
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>,
) {
assert!(output_key.is_none());
let current_revision = db.runtime().current_revision();
if let Some(mut v) = self.map.get_mut(&executor) {
v.produced_at = current_revision;
}
}
fn remove_stale_output(
&self,
db: &DB,
executor: DatabaseKeyIndex,
stale_output_key: Option<crate::Id>,
) {
assert!(stale_output_key.is_none());
if self.map.remove(&executor).is_some() {
db.salsa_event(Event {
runtime_id: db.runtime().id(),
kind: EventKind::DidDiscardAccumulated {
executor_key: executor,
accumulator: self.dependency_index(),
},
})
}
}
fn reset_for_new_revision(&mut self) {
panic!("unexpected reset on accumulator")
}
fn salsa_struct_deleted(&self, _db: &DB, _id: crate::Id) {
panic!("unexpected call: accumulator is not registered as a dependent fn");
}
fn fmt_index(&self, index: Option<crate::Id>, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt_index(self.debug_name, index, fmt)
}
}
impl<Data> IngredientRequiresReset for AccumulatorIngredient<Data>
where
Data: Clone,
{
const RESET_ON_NEW_REVISION: bool = false;
}