Skip to content

Commit 31c310f

Browse files
committed
Lazy fetching
1 parent 124e62f commit 31c310f

File tree

5 files changed

+32
-32
lines changed

5 files changed

+32
-32
lines changed

src/function.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use crate::{
55
cycle::CycleRecoveryStrategy,
66
ingredient::{fmt_index, MaybeChangedAfter},
77
key::DatabaseKeyIndex,
8+
plumbing::MemoIngredientMap,
89
salsa_struct::SalsaStructInDb,
910
table::Table,
1011
zalsa::{IngredientIndex, MemoIngredientIndex, Zalsa},
@@ -194,7 +195,7 @@ where
194195

195196
#[inline]
196197
fn memo_ingredient_index(&self, zalsa: &Zalsa, id: Id) -> MemoIngredientIndex {
197-
self.memo_ingredient_indices[zalsa.ingredient_index(id)]
198+
self.memo_ingredient_indices.get_zalsa_id(zalsa, id)
198199
}
199200
}
200201

@@ -254,7 +255,7 @@ where
254255
Self::evict_value_from_memo_for(
255256
table.memos_mut(evict),
256257
&self.deleted_entries,
257-
self.memo_ingredient_indices[ingredient_index],
258+
self.memo_ingredient_indices.get(ingredient_index),
258259
)
259260
});
260261
std::mem::take(&mut self.deleted_entries);

src/function/maybe_changed_after.rs

+4-10
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ use crate::{
22
accumulator::accumulated_map::InputAccumulatedValues,
33
ingredient::MaybeChangedAfter,
44
key::DatabaseKeyIndex,
5-
plumbing::ZalsaLocal,
65
zalsa::{MemoIngredientIndex, Zalsa, ZalsaDatabase},
76
zalsa_local::{ActiveQueryGuard, QueryEdge, QueryOrigin},
87
AsDynDatabase as _, Id, Revision,
@@ -40,14 +39,9 @@ where
4039
};
4140
}
4241
drop(memo_guard); // release the arc-swap guard before cold path
43-
if let Some(mcs) = self.maybe_changed_after_cold(
44-
zalsa,
45-
db.zalsa_local(),
46-
db,
47-
id,
48-
revision,
49-
memo_ingredient_index,
50-
) {
42+
if let Some(mcs) =
43+
self.maybe_changed_after_cold(zalsa, db, id, revision, memo_ingredient_index)
44+
{
5145
return mcs;
5246
} else {
5347
// We failed to claim, have to retry.
@@ -62,14 +56,14 @@ where
6256
fn maybe_changed_after_cold<'db>(
6357
&'db self,
6458
zalsa: &Zalsa,
65-
zalsa_local: &ZalsaLocal,
6659
db: &'db C::DbView,
6760
key_index: Id,
6861
revision: Revision,
6962
memo_ingredient_index: MemoIngredientIndex,
7063
) -> Option<MaybeChangedAfter> {
7164
let database_key_index = self.database_key_index(key_index);
7265

66+
let zalsa_local = db.zalsa_local();
7367
let _claim_guard = zalsa.sync_table_for(key_index).claim(
7468
db.as_dyn_database(),
7569
zalsa,

src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ pub mod plumbing {
9292
pub use crate::ingredient::Jar;
9393
pub use crate::key::DatabaseKeyIndex;
9494
pub use crate::memo_ingredient_indices::{
95-
IngredientIndices, MemoIngredientIndices, MemoIngredientSingletonIndex,
95+
IngredientIndices, MemoIngredientIndices, MemoIngredientMap, MemoIngredientSingletonIndex,
9696
};
9797
pub use crate::revision::Revision;
9898
pub use crate::runtime::stamp;

src/memo_ingredient_indices.rs

+22-15
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
1-
use std::ops;
2-
31
use crate::zalsa::{MemoIngredientIndex, Zalsa};
4-
use crate::IngredientIndex;
2+
use crate::{Id, IngredientIndex};
53

64
/// An ingredient has an [ingredient index][IngredientIndex]. However, Salsa also supports
75
/// enums of salsa structs (and other salsa enums), and those don't have a constant ingredient index,
@@ -89,24 +87,28 @@ pub struct MemoIngredientIndices {
8987
indices: Box<[MemoIngredientIndex]>,
9088
}
9189

92-
impl ops::Index<IngredientIndex> for MemoIngredientIndices {
93-
type Output = MemoIngredientIndex;
94-
95-
#[inline]
96-
fn index(&self, index: IngredientIndex) -> &Self::Output {
97-
&self.indices[index.as_usize()]
90+
impl MemoIngredientMap for MemoIngredientIndices {
91+
#[inline(always)]
92+
fn get_zalsa_id(&self, zalsa: &Zalsa, id: Id) -> MemoIngredientIndex {
93+
self.get(zalsa.ingredient_index(id))
94+
}
95+
#[inline(always)]
96+
fn get(&self, index: IngredientIndex) -> MemoIngredientIndex {
97+
self.indices[index.as_usize()]
9898
}
9999
}
100100

101101
#[derive(Debug)]
102102
pub struct MemoIngredientSingletonIndex(MemoIngredientIndex);
103103

104-
impl ops::Index<IngredientIndex> for MemoIngredientSingletonIndex {
105-
type Output = MemoIngredientIndex;
106-
107-
#[inline]
108-
fn index(&self, _: IngredientIndex) -> &Self::Output {
109-
&self.0
104+
impl MemoIngredientMap for MemoIngredientSingletonIndex {
105+
#[inline(always)]
106+
fn get_zalsa_id(&self, _: &Zalsa, _: Id) -> MemoIngredientIndex {
107+
self.0
108+
}
109+
#[inline(always)]
110+
fn get(&self, _: IngredientIndex) -> MemoIngredientIndex {
111+
self.0
110112
}
111113
}
112114

@@ -120,3 +122,8 @@ impl From<(&Zalsa, IngredientIndices, IngredientIndex)> for MemoIngredientSingle
120122
Self(zalsa.next_memo_ingredient_index(struct_ingredient, ingredient))
121123
}
122124
}
125+
126+
pub trait MemoIngredientMap: Send + Sync {
127+
fn get_zalsa_id(&self, zalsa: &Zalsa, id: Id) -> MemoIngredientIndex;
128+
fn get(&self, index: IngredientIndex) -> MemoIngredientIndex;
129+
}

src/salsa_struct.rs

+2-4
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
11
use std::any::TypeId;
22

3-
use crate::memo_ingredient_indices::IngredientIndices;
3+
use crate::memo_ingredient_indices::{IngredientIndices, MemoIngredientMap};
44
use crate::zalsa::Zalsa;
55
use crate::Id;
66

77
pub trait SalsaStructInDb: Sized {
8-
type MemoIngredientMap: std::ops::Index<crate::IngredientIndex, Output = crate::zalsa::MemoIngredientIndex>
9-
+ Send
10-
+ Sync;
8+
type MemoIngredientMap: MemoIngredientMap;
119

1210
/// Lookup or create ingredient indices.
1311
///

0 commit comments

Comments
 (0)