From e7c7f386fe4b6c87fb610389cf32d89613551bf8 Mon Sep 17 00:00:00 2001 From: XFFXFF <1247714429@qq.com> Date: Sun, 25 Sep 2022 00:24:16 +0000 Subject: [PATCH] fix accumulator: clear the outdated accumulated values --- components/salsa-2022/src/accumulator.rs | 6 ++++-- components/salsa-2022/src/function/specify.rs | 3 ++- components/salsa-2022/src/runtime.rs | 2 +- components/salsa-2022/src/runtime/active_query.rs | 3 +-- components/salsa-2022/src/runtime/local_state.rs | 2 +- salsa-2022-tests/tests/accumulate-reuse-workaround.rs | 1 - 6 files changed, 9 insertions(+), 8 deletions(-) diff --git a/components/salsa-2022/src/accumulator.rs b/components/salsa-2022/src/accumulator.rs index 3898f9d5..69ea998c 100644 --- a/components/salsa-2022/src/accumulator.rs +++ b/components/salsa-2022/src/accumulator.rs @@ -61,8 +61,10 @@ impl AccumulatorIngredient { produced_at: current_revision, }); - // This is the first push in a new revision. Reset. - if accumulated_values.produced_at != current_revision { + // When we call `push' in a query, we will add the accumulator to the output of the query. + // If we find here that this accumulator is not the output of the query, + // we can say that the accumulated values we stored for this query is out of date. + if !runtime.is_output_of_active_query(self.dependency_index()) { accumulated_values.values.truncate(0); accumulated_values.produced_at = current_revision; } diff --git a/components/salsa-2022/src/function/specify.rs b/components/salsa-2022/src/function/specify.rs index 0c215383..1904cc63 100644 --- a/components/salsa-2022/src/function/specify.rs +++ b/components/salsa-2022/src/function/specify.rs @@ -46,7 +46,8 @@ where // // Now, if We invoke Q3 first, We get one result for Q2, but if We invoke Q4 first, We get a different value. That's no good. let database_key_index = key.database_key_index(db); - if !runtime.is_output_of_active_query(database_key_index) { + let dependency_index = database_key_index.into(); + if !runtime.is_output_of_active_query(dependency_index) { panic!("can only use `specfiy` on entities created during current query"); } diff --git a/components/salsa-2022/src/runtime.rs b/components/salsa-2022/src/runtime.rs index 9bc7f12b..a5e36648 100644 --- a/components/salsa-2022/src/runtime.rs +++ b/components/salsa-2022/src/runtime.rs @@ -149,7 +149,7 @@ impl Runtime { } /// Check whether `entity` is contained the list of outputs written by the current query. - pub(super) fn is_output_of_active_query(&self, entity: DatabaseKeyIndex) -> bool { + pub(super) fn is_output_of_active_query(&self, entity: DependencyIndex) -> bool { self.local_state.is_output(entity) } diff --git a/components/salsa-2022/src/runtime/active_query.rs b/components/salsa-2022/src/runtime/active_query.rs index a97bf62f..5ffcae6a 100644 --- a/components/salsa-2022/src/runtime/active_query.rs +++ b/components/salsa-2022/src/runtime/active_query.rs @@ -82,8 +82,7 @@ impl ActiveQuery { } /// True if the given key was output by this query. - pub(super) fn is_output(&self, key: DatabaseKeyIndex) -> bool { - let key: DependencyIndex = key.into(); + pub(super) fn is_output(&self, key: DependencyIndex) -> bool { self.input_outputs.contains(&(EdgeKind::Output, key)) } diff --git a/components/salsa-2022/src/runtime/local_state.rs b/components/salsa-2022/src/runtime/local_state.rs index caa3ba91..272ca934 100644 --- a/components/salsa-2022/src/runtime/local_state.rs +++ b/components/salsa-2022/src/runtime/local_state.rs @@ -190,7 +190,7 @@ impl LocalState { }) } - pub(super) fn is_output(&self, entity: DatabaseKeyIndex) -> bool { + pub(super) fn is_output(&self, entity: DependencyIndex) -> bool { self.with_query_stack(|stack| { if let Some(top_query) = stack.last_mut() { top_query.is_output(entity) diff --git a/salsa-2022-tests/tests/accumulate-reuse-workaround.rs b/salsa-2022-tests/tests/accumulate-reuse-workaround.rs index f06ebb06..e960db1b 100644 --- a/salsa-2022-tests/tests/accumulate-reuse-workaround.rs +++ b/salsa-2022-tests/tests/accumulate-reuse-workaround.rs @@ -89,6 +89,5 @@ fn test1() { [ "accumulated(List(Id { value: 1 }))", "compute(List(Id { value: 1 }))", - "compute(List(Id { value: 2 }))", ]"#]]); }