From 8f6dcaf6328ce09f024db8a27c90d9303cebb8c4 Mon Sep 17 00:00:00 2001 From: Micha Reiser Date: Wed, 4 Dec 2024 09:34:35 +0100 Subject: [PATCH 1/2] Avoid unnecessary resizes of `visisted` set --- src/function/accumulated.rs | 26 +++++++++++++++++++++----- 1 file changed, 21 insertions(+), 5 deletions(-) diff --git a/src/function/accumulated.rs b/src/function/accumulated.rs index 83379a918..8af7eb0bd 100644 --- a/src/function/accumulated.rs +++ b/src/function/accumulated.rs @@ -69,15 +69,31 @@ where // output vector, we want to push in execution order, so reverse order to // ensure the first child that was executed will be the first child popped // from the stack. - let origin = zalsa + let Some(origin) = zalsa .lookup_ingredient(k.ingredient_index) - .origin(db, k.key_index); - let inputs = origin.iter().flat_map(|origin| origin.inputs()); + .origin(db, k.key_index) + else { + continue; + }; + + let estimated_inputs = match &origin { + QueryOrigin::Assigned(_) | QueryOrigin::BaseInput => 0, + QueryOrigin::Derived(edges) | QueryOrigin::DerivedUntracked(edges) => { + edges.input_outputs.len() + } + }; + + stack.reserve(estimated_inputs); + visited.reserve(estimated_inputs); + stack.extend( - inputs - .flat_map(|input| TryInto::::try_into(input).into_iter()) + origin + .inputs() + .filter_map(|input| TryInto::::try_into(input).ok()) .rev(), ); + + visited.reserve(stack.len()); } output From 5314d4ccc98870c1bce1fae5a0309a5a71ad35dc Mon Sep 17 00:00:00 2001 From: Micha Reiser Date: Wed, 4 Dec 2024 09:45:23 +0100 Subject: [PATCH 2/2] Use `inputs_outputs` len as approximation --- src/function/accumulated.rs | 16 +++++----------- 1 file changed, 5 insertions(+), 11 deletions(-) diff --git a/src/function/accumulated.rs b/src/function/accumulated.rs index 8af7eb0bd..d8b0fcfc9 100644 --- a/src/function/accumulated.rs +++ b/src/function/accumulated.rs @@ -1,3 +1,5 @@ +use super::{Configuration, IngredientImpl}; +use crate::zalsa_local::QueryOrigin; use crate::{ accumulator::{self, accumulated_map::AccumulatedMap}, hash::FxHashSet, @@ -5,8 +7,6 @@ use crate::{ AsDynDatabase, DatabaseKeyIndex, Id, }; -use super::{Configuration, IngredientImpl}; - impl IngredientImpl where C: Configuration, @@ -76,15 +76,9 @@ where continue; }; - let estimated_inputs = match &origin { - QueryOrigin::Assigned(_) | QueryOrigin::BaseInput => 0, - QueryOrigin::Derived(edges) | QueryOrigin::DerivedUntracked(edges) => { - edges.input_outputs.len() - } - }; - - stack.reserve(estimated_inputs); - visited.reserve(estimated_inputs); + if let QueryOrigin::Derived(edges) | QueryOrigin::DerivedUntracked(edges) = &origin { + stack.reserve(edges.input_outputs.len()); + } stack.extend( origin