Skip to content

Commit fecc37d

Browse files
committed
serialize ingredients by index
1 parent b7e5658 commit fecc37d

File tree

3 files changed

+56
-58
lines changed

3 files changed

+56
-58
lines changed

src/database.rs

Lines changed: 9 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ pub fn current_revision<Db: ?Sized + Database>(db: &Db) -> Revision {
156156
mod persistence {
157157
use crate::plumbing::Ingredient;
158158
use crate::zalsa::Zalsa;
159-
use crate::{Database, Runtime};
159+
use crate::{Database, IngredientIndex, Runtime};
160160

161161
use std::fmt;
162162

@@ -205,21 +205,14 @@ mod persistence {
205205
.filter(|ingredient| ingredient.should_serialize(zalsa))
206206
.collect::<Vec<_>>();
207207

208-
ingredients.sort_by(|a, b| {
209-
// Ensure structs are serialized before tracked functions, as deserializing a
210-
// memo requires its input struct to have been deserialized.
211-
//
212-
// We also further sort by debug name, to maintain a consistent ordering across
213-
// builds.
214-
a.jar_kind()
215-
.cmp(&b.jar_kind())
216-
.then(a.debug_name().cmp(b.debug_name()))
217-
});
208+
// Ensure structs are serialized before tracked functions, as deserializing a
209+
// memo requires its input struct to have been deserialized.
210+
ingredients.sort_by_key(|ingredient| ingredient.jar_kind());
218211

219212
let mut map = serializer.serialize_map(Some(ingredients.len()))?;
220213
for ingredient in ingredients {
221214
map.serialize_entry(
222-
&ingredient.debug_name(),
215+
&ingredient.ingredient_index().as_u32(),
223216
&SerializeIngredient(ingredient, zalsa),
224217
)?;
225218
}
@@ -332,21 +325,17 @@ mod persistence {
332325
{
333326
let DeserializeIngredients(zalsa) = self;
334327

335-
while let Some(name) = access.next_key::<&str>()? {
336-
// TODO: Serialize the ingredient index directly.
337-
let ingredient = zalsa
338-
.ingredients()
339-
.find(|ingredient| ingredient.debug_name() == name)
340-
.unwrap();
328+
while let Some(index) = access.next_key::<u32>()? {
329+
let index = IngredientIndex::new(index);
341330

342331
// Remove the ingredient temporarily, to avoid holding an overlapping mutable borrow
343332
// to the ingredient as well as the database.
344-
let mut ingredient = zalsa.take_ingredient(ingredient.ingredient_index());
333+
let mut ingredient = zalsa.take_ingredient(index);
345334

346335
// Deserialize the ingredient.
347336
access.next_value_seed(DeserializeIngredient(&mut *ingredient, zalsa))?;
348337

349-
zalsa.replace_ingredient(ingredient.ingredient_index(), ingredient);
338+
zalsa.replace_ingredient(index, ingredient);
350339
}
351340

352341
Ok(())

src/zalsa.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,10 @@ impl Zalsa {
208208
let mut jars = jars;
209209

210210
// Ensure structs are initialized before tracked functions.
211-
jars.sort_by_key(|jar| jar.kind);
211+
//
212+
// We also further sort by debug name, to maintain a consistent ordering across
213+
// builds.
214+
jars.sort_by(|a, b| a.kind.cmp(&b.kind).then(a.type_name().cmp(b.type_name())));
212215

213216
for jar in jars {
214217
zalsa.insert_jar(jar);
@@ -490,6 +493,7 @@ impl Zalsa {
490493
pub struct ErasedJar {
491494
kind: JarKind,
492495
type_id: fn() -> TypeId,
496+
type_name: fn() -> &'static str,
493497
id_struct_type_id: fn() -> TypeId,
494498
create_ingredients: fn(&mut Zalsa, IngredientIndex) -> Vec<Box<dyn Ingredient>>,
495499
}
@@ -514,10 +518,15 @@ impl ErasedJar {
514518
Self {
515519
kind: I::KIND,
516520
type_id: TypeId::of::<I::Jar>,
521+
type_name: std::any::type_name::<I::Jar>,
517522
create_ingredients: <I::Jar>::create_ingredients,
518523
id_struct_type_id: <I::Jar>::id_struct_type_id,
519524
}
520525
}
526+
527+
pub fn type_name(&self) -> &'static str {
528+
(self.type_name)()
529+
}
521530
}
522531

523532
/// A salsa ingredient that can be registered in the database.

tests/persistence.rs

Lines changed: 37 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ fn everything() {
5555
]
5656
},
5757
"ingredients": {
58-
"MyInput": {
58+
"0": {
5959
"1": {
6060
"durabilities": [
6161
"Low"
@@ -104,7 +104,7 @@ fn everything() {
104104
]
105105
},
106106
"ingredients": {
107-
"MyInput": {
107+
"0": {
108108
"1": {
109109
"durabilities": [
110110
"Low"
@@ -150,7 +150,7 @@ fn everything() {
150150
]
151151
}
152152
},
153-
"MyInterned": {
153+
"2": {
154154
"2049": {
155155
"durability": "High",
156156
"last_interned_at": 1,
@@ -159,7 +159,7 @@ fn everything() {
159159
]
160160
}
161161
},
162-
"MyTracked": {
162+
"3": {
163163
"3073": {
164164
"durability": "Low",
165165
"updated_at": 1,
@@ -169,7 +169,14 @@ fn everything() {
169169
]
170170
}
171171
},
172-
"input_pair_to_string::interned_arguments": {
172+
"6": {
173+
"1025": {
174+
"durability": "High",
175+
"last_interned_at": 18446744073709551615,
176+
"fields": null
177+
}
178+
},
179+
"9": {
173180
"4097": {
174181
"durability": "High",
175182
"last_interned_at": 18446744073709551615,
@@ -185,38 +192,25 @@ fn everything() {
185192
]
186193
}
187194
},
188-
"unit_to_interned::interned_arguments": {
189-
"1025": {
190-
"durability": "High",
191-
"last_interned_at": 18446744073709551615,
192-
"fields": null
193-
}
194-
},
195-
"input_pair_to_string": {
196-
"9:4097": {
197-
"value": "aaa",
195+
"5": {
196+
"6:1025": {
197+
"value": {
198+
"index": 2049,
199+
"generation": 0
200+
},
198201
"verified_at": 1,
199202
"revisions": {
200203
"changed_at": 1,
201-
"durability": "Low",
204+
"durability": "High",
202205
"origin": {
203206
"Derived": [
204207
{
205208
"key": {
206209
"key_index": {
207-
"index": 3,
208-
"generation": 0
209-
},
210-
"ingredient_index": 1
211-
}
212-
},
213-
{
214-
"key": {
215-
"key_index": {
216-
"index": 4,
210+
"index": 2049,
217211
"generation": 0
218212
},
219-
"ingredient_index": 1
213+
"ingredient_index": 2
220214
}
221215
}
222216
]
@@ -226,7 +220,7 @@ fn everything() {
226220
}
227221
}
228222
},
229-
"input_to_tracked": {
223+
"7": {
230224
"0:3": {
231225
"value": {
232226
"index": 3073,
@@ -279,25 +273,31 @@ fn everything() {
279273
}
280274
}
281275
},
282-
"unit_to_interned": {
283-
"6:1025": {
284-
"value": {
285-
"index": 2049,
286-
"generation": 0
287-
},
276+
"8": {
277+
"9:4097": {
278+
"value": "aaa",
288279
"verified_at": 1,
289280
"revisions": {
290281
"changed_at": 1,
291-
"durability": "High",
282+
"durability": "Low",
292283
"origin": {
293284
"Derived": [
294285
{
295286
"key": {
296287
"key_index": {
297-
"index": 2049,
288+
"index": 3,
298289
"generation": 0
299290
},
300-
"ingredient_index": 2
291+
"ingredient_index": 1
292+
}
293+
},
294+
{
295+
"key": {
296+
"key_index": {
297+
"index": 4,
298+
"generation": 0
299+
},
300+
"ingredient_index": 1
301301
}
302302
}
303303
]

0 commit comments

Comments
 (0)