Skip to content

Commit 0706be7

Browse files
committed
thread group index to storage initializers
1 parent 1f2801d commit 0706be7

File tree

7 files changed

+47
-75
lines changed

7 files changed

+47
-75
lines changed

components/salsa-macros/src/database_storage.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ pub(crate) fn database(args: TokenStream, input: TokenStream) -> TokenStream {
5353
.zip(&query_group_names_snake)
5454
.zip(&query_group_storage_names)
5555
.zip(&query_group_key_names)
56-
.zip(0_usize..)
56+
.zip(0_u16..)
5757
{
5858
let group_path = &query_group.group_path;
5959
let group_name = query_group.name();

components/salsa-macros/src/query_group.rs

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ pub(crate) fn query_group(args: TokenStream, input: TokenStream) -> TokenStream
192192
let mut query_descriptor_variants = proc_macro2::TokenStream::new();
193193
let mut group_data_elements = vec![];
194194
let mut storage_fields = proc_macro2::TokenStream::new();
195-
let mut storage_defaults = proc_macro2::TokenStream::new();
195+
let mut queries_with_storage = vec![];
196196
for query in &queries {
197197
let key_names: &Vec<_> = &(0..query.keys.len())
198198
.map(|i| Ident::new(&format!("key{}", i), Span::call_site()))
@@ -220,6 +220,8 @@ pub(crate) fn query_group(args: TokenStream, input: TokenStream) -> TokenStream
220220
continue;
221221
}
222222

223+
queries_with_storage.push(fn_name);
224+
223225
query_fn_definitions.extend(quote! {
224226
fn #fn_name(&self, #(#key_names: #keys),*) -> #value {
225227
<Self as salsa::plumbing::GetQueryTable<#qt>>::get_query_table(self).get((#(#key_names),*))
@@ -295,7 +297,6 @@ pub(crate) fn query_group(args: TokenStream, input: TokenStream) -> TokenStream
295297
storage_fields.extend(quote! {
296298
pub #fn_name: std::sync::Arc<<#qt as salsa::Query<DB__>>::Storage>,
297299
});
298-
storage_defaults.extend(quote! { #fn_name: Default::default(), });
299300
}
300301

301302
// Emit the trait itself.
@@ -349,7 +350,7 @@ pub(crate) fn query_group(args: TokenStream, input: TokenStream) -> TokenStream
349350
});
350351

351352
// Emit the query types.
352-
for query in &queries {
353+
for (query, query_index) in queries.iter().zip(0_u16..) {
353354
let fn_name = &query.fn_name;
354355
let qt = &query.query_type;
355356

@@ -388,6 +389,8 @@ pub(crate) fn query_group(args: TokenStream, input: TokenStream) -> TokenStream
388389
type GroupStorage = #group_storage<#db>;
389390
type GroupKey = #group_key;
390391

392+
const QUERY_INDEX: u16 = #query_index;
393+
391394
fn query_storage(
392395
group_storage: &Self::GroupStorage,
393396
) -> &std::sync::Arc<Self::Storage> {
@@ -466,8 +469,6 @@ pub(crate) fn query_group(args: TokenStream, input: TokenStream) -> TokenStream
466469
}
467470

468471
// Emit query group storage struct
469-
// It would derive Default, but then all database structs would have to implement Default
470-
// as the derived version includes an unused `+ Default` constraint.
471472
output.extend(quote! {
472473
#trait_vis struct #group_storage<DB__>
473474
where
@@ -484,9 +485,12 @@ pub(crate) fn query_group(args: TokenStream, input: TokenStream) -> TokenStream
484485
DB__: salsa::plumbing::HasQueryGroup<#group_struct>,
485486
DB__: salsa::Database,
486487
{
487-
#trait_vis fn new(group_index: usize) -> Self {
488+
#trait_vis fn new(group_index: u16) -> Self {
488489
#group_storage {
489-
#storage_defaults
490+
#(
491+
#queries_with_storage:
492+
std::sync::Arc::new(salsa::plumbing::QueryStorageOps::new(group_index)),
493+
)*
490494
}
491495
}
492496
}

src/derived.rs

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -91,21 +91,6 @@ where
9191
}
9292
}
9393

94-
impl<DB, Q, MP> Default for DerivedStorage<DB, Q, MP>
95-
where
96-
Q: QueryFunction<DB>,
97-
DB: Database + HasQueryGroup<Q::Group>,
98-
MP: MemoizationPolicy<DB, Q>,
99-
{
100-
fn default() -> Self {
101-
DerivedStorage {
102-
slot_map: RwLock::new(FxHashMap::default()),
103-
lru_list: Default::default(),
104-
policy: PhantomData,
105-
}
106-
}
107-
}
108-
10994
impl<DB, Q, MP> DerivedStorage<DB, Q, MP>
11095
where
11196
Q: QueryFunction<DB>,
@@ -131,6 +116,14 @@ where
131116
DB: Database + HasQueryGroup<Q::Group>,
132117
MP: MemoizationPolicy<DB, Q>,
133118
{
119+
fn new(_group_index: u16) -> Self {
120+
DerivedStorage {
121+
slot_map: RwLock::new(FxHashMap::default()),
122+
lru_list: Default::default(),
123+
policy: PhantomData,
124+
}
125+
}
126+
134127
fn try_fetch(&self, db: &DB, key: &Q::Key) -> Result<Q::Value, CycleError<DB::DatabaseKey>> {
135128
let slot = self.slot(key);
136129
let StampedValue {

src/input.rs

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -47,18 +47,6 @@ where
4747
{
4848
}
4949

50-
impl<DB, Q> Default for InputStorage<DB, Q>
51-
where
52-
Q: Query<DB>,
53-
DB: Database,
54-
{
55-
fn default() -> Self {
56-
InputStorage {
57-
slots: Default::default(),
58-
}
59-
}
60-
}
61-
6250
impl<DB, Q> InputStorage<DB, Q>
6351
where
6452
Q: Query<DB>,
@@ -74,6 +62,12 @@ where
7462
Q: Query<DB>,
7563
DB: Database,
7664
{
65+
fn new(_group_index: u16) -> Self {
66+
InputStorage {
67+
slots: Default::default(),
68+
}
69+
}
70+
7771
fn try_fetch(&self, db: &DB, key: &Q::Key) -> Result<Q::Value, CycleError<DB::DatabaseKey>> {
7872
let slot = self
7973
.slot(key)

src/interned.rs

Lines changed: 12 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -128,42 +128,6 @@ where
128128
{
129129
}
130130

131-
impl<DB, Q> Default for InternedStorage<DB, Q>
132-
where
133-
Q: Query<DB>,
134-
Q::Key: Eq + Hash,
135-
Q::Value: InternKey,
136-
DB: Database,
137-
{
138-
fn default() -> Self {
139-
InternedStorage {
140-
tables: RwLock::new(InternTables::default()),
141-
}
142-
}
143-
}
144-
145-
impl<DB, Q, IQ> Default for LookupInternedStorage<DB, Q, IQ>
146-
where
147-
Q: Query<DB>,
148-
Q::Key: InternKey,
149-
Q::Value: Eq + Hash,
150-
IQ: Query<
151-
DB,
152-
Key = Q::Value,
153-
Value = Q::Key,
154-
Group = Q::Group,
155-
GroupStorage = Q::GroupStorage,
156-
GroupKey = Q::GroupKey,
157-
>,
158-
DB: Database,
159-
{
160-
fn default() -> Self {
161-
LookupInternedStorage {
162-
phantom: std::marker::PhantomData,
163-
}
164-
}
165-
}
166-
167131
impl<K: Debug + Hash + Eq> InternTables<K> {
168132
/// Returns the slot for the given key.
169133
///
@@ -320,6 +284,12 @@ where
320284
Q::Value: InternKey,
321285
DB: Database,
322286
{
287+
fn new(_group_index: u16) -> Self {
288+
InternedStorage {
289+
tables: RwLock::new(InternTables::default()),
290+
}
291+
}
292+
323293
fn try_fetch(&self, db: &DB, key: &Q::Key) -> Result<Q::Value, CycleError<DB::DatabaseKey>> {
324294
let slot = self.intern_index(db, key);
325295
let changed_at = slot.interned_at;
@@ -419,6 +389,12 @@ where
419389
>,
420390
DB: Database + HasQueryGroup<Q::Group>,
421391
{
392+
fn new(_group_index: u16) -> Self {
393+
LookupInternedStorage {
394+
phantom: std::marker::PhantomData,
395+
}
396+
}
397+
422398
fn try_fetch(&self, db: &DB, key: &Q::Key) -> Result<Q::Value, CycleError<DB::DatabaseKey>> {
423399
let index = key.as_intern_id();
424400
let group_storage = <DB as HasQueryGroup<Q::Group>>::group_storage(db);

src/lib.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -441,6 +441,9 @@ pub unsafe trait Query<DB: Database>: Debug + Default + Sized + 'static {
441441
/// Type that identifies a particular query within the group + its key.
442442
type GroupKey;
443443

444+
/// A unique index identifying this query within the group.
445+
const QUERY_INDEX: u16;
446+
444447
/// Extact storage for this query from the storage for its group.
445448
fn query_storage(group_storage: &Self::GroupStorage) -> &Arc<Self::Storage>;
446449

src/plumbing.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -142,12 +142,14 @@ where
142142
fn database_key(group_key: G::GroupKey) -> Self::DatabaseKey;
143143
}
144144

145-
pub trait QueryStorageOps<DB, Q>: Default
145+
pub trait QueryStorageOps<DB, Q>
146146
where
147147
Self: QueryStorageMassOps<DB>,
148148
DB: Database,
149149
Q: Query<DB>,
150150
{
151+
fn new(group_index: u16) -> Self;
152+
151153
/// Execute the query, returning the result (often, the result
152154
/// will be memoized). This is the "main method" for
153155
/// queries.
@@ -169,7 +171,7 @@ where
169171
/// An optional trait that is implemented for "user mutable" storage:
170172
/// that is, storage whose value is not derived from other storage but
171173
/// is set independently.
172-
pub trait InputQueryStorageOps<DB, Q>: Default
174+
pub trait InputQueryStorageOps<DB, Q>
173175
where
174176
DB: Database,
175177
Q: Query<DB>,
@@ -187,11 +189,11 @@ where
187189
/// An optional trait that is implemented for "user mutable" storage:
188190
/// that is, storage whose value is not derived from other storage but
189191
/// is set independently.
190-
pub trait LruQueryStorageOps: Default {
192+
pub trait LruQueryStorageOps {
191193
fn set_lru_capacity(&self, new_capacity: usize);
192194
}
193195

194-
pub trait DerivedQueryStorageOps<DB, Q>: Default
196+
pub trait DerivedQueryStorageOps<DB, Q>
195197
where
196198
DB: Database,
197199
Q: Query<DB>,

0 commit comments

Comments
 (0)