@@ -290,12 +290,15 @@ impl ZalsaLocal {
290
290
) ;
291
291
self . with_query_stack ( |stack| {
292
292
let top_query = stack. last ( ) . unwrap ( ) ;
293
- top_query. tracked_struct_ids . get ( key_struct) . cloned ( )
293
+ top_query
294
+ . tracked_struct_ids
295
+ . get ( key_struct)
296
+ . map ( |index| index. key_index ( ) )
294
297
} )
295
298
}
296
299
297
300
#[ track_caller]
298
- pub ( crate ) fn store_tracked_struct_id ( & self , key_struct : KeyStruct , id : Id ) {
301
+ pub ( crate ) fn store_tracked_struct_id ( & self , key_struct : KeyStruct , id : DatabaseKeyIndex ) {
299
302
debug_assert ! (
300
303
self . query_in_progress( ) ,
301
304
"cannot create a tracked struct disambiguator outside of a tracked function"
@@ -358,9 +361,23 @@ pub(crate) struct QueryRevisions {
358
361
pub ( crate ) origin : QueryOrigin ,
359
362
360
363
/// The ids of tracked structs created by this query.
361
- /// This is used to seed the next round if the query is
362
- /// re-executed.
363
- pub ( super ) tracked_struct_ids : FxHashMap < KeyStruct , Id > ,
364
+ ///
365
+ /// This table plays an important role when queries are
366
+ /// re-executed:
367
+ /// * A clone of this field is used as the initial set of
368
+ /// `TrackedStructId`s for the query on the next execution.
369
+ /// * The query will thus re-use the same ids if it creates
370
+ /// tracked structs with the same `KeyStruct` as before.
371
+ /// It may also create new tracked structs.
372
+ /// * One tricky case involves deleted structs. If
373
+ /// the old revision created a struct S but the new
374
+ /// revision did not, there will still be a map entry
375
+ /// for S. This is because queries only ever grow the map
376
+ /// and they start with the same entries as from the
377
+ /// previous revision. To handle this, `diff_outputs` compares
378
+ /// the structs from the old/new revision and retains
379
+ /// only entries that appeared in the new revision.
380
+ pub ( super ) tracked_struct_ids : FxHashMap < KeyStruct , DatabaseKeyIndex > ,
364
381
365
382
pub ( super ) accumulated : AccumulatedMap ,
366
383
}
@@ -519,7 +536,10 @@ impl ActiveQueryGuard<'_> {
519
536
}
520
537
521
538
/// Initialize the tracked struct ids with the values from the prior execution.
522
- pub ( crate ) fn seed_tracked_struct_ids ( & self , tracked_struct_ids : & FxHashMap < KeyStruct , Id > ) {
539
+ pub ( crate ) fn seed_tracked_struct_ids (
540
+ & self ,
541
+ tracked_struct_ids : & FxHashMap < KeyStruct , DatabaseKeyIndex > ,
542
+ ) {
523
543
self . local_state . with_query_stack ( |stack| {
524
544
assert_eq ! ( stack. len( ) , self . push_len) ;
525
545
let frame = stack. last_mut ( ) . unwrap ( ) ;
0 commit comments