From fb946fcdc7f4e478f21617ad75bd60f0e5f20a54 Mon Sep 17 00:00:00 2001 From: Tony Garnock-Jones Date: Mon, 15 Jun 2020 21:14:01 +0200 Subject: [PATCH] Elide intermediate consing --- src/dataspace.rs | 30 ++++++------------------------ src/skeleton.rs | 37 ++++++++++++++++++++++--------------- 2 files changed, 28 insertions(+), 39 deletions(-) diff --git a/src/dataspace.rs b/src/dataspace.rs index 60c0a94..516ea69 100644 --- a/src/dataspace.rs +++ b/src/dataspace.rs @@ -1,6 +1,6 @@ use super::V; use super::ConnId; -use super::packets::{self, Assertion, EndpointName, Captures}; +use super::packets::{self, Assertion, EndpointName}; use super::skeleton; use preserves::value::{self, Map, NestedValue}; @@ -121,9 +121,7 @@ impl Dataspace { }); } let old_assertions = self.index.assertion_count(); - schedule_events(&mut outbound_turns, - self.index.remove((&assertion).into()), - packets::Event::Del); + self.index.remove((&assertion).into(), &mut outbound_turns); self.churn.assertions_removed += old_assertions - self.index.assertion_count(); self.churn.endpoints_removed += 1; } @@ -155,9 +153,7 @@ impl Dataspace { }; let old_assertions = self.index.assertion_count(); - schedule_events(&mut outbound_turns, - self.index.insert(assertion.into()), - packets::Event::Add); + self.index.insert(assertion.into(), &mut outbound_turns); self.churn.assertions_added += self.index.assertion_count() - old_assertions; self.churn.endpoints_added += 1; @@ -180,10 +176,9 @@ impl Dataspace { } } packets::Action::Message(ref assertion) => { - schedule_events(&mut outbound_turns, - self.index.send(assertion.into(), - &mut self.churn.messages_delivered), - packets::Event::Msg); + self.index.send(assertion.into(), + &mut outbound_turns, + &mut self.churn.messages_delivered); self.churn.messages_injected += 1; } } @@ -212,16 +207,3 @@ impl Dataspace { self.index.endpoint_count() } } - -fn schedule_events(outbound_turns: &mut Map>, - events: skeleton::Events, - ctor: C) -where C: Fn(EndpointName, Captures) -> packets::Event -{ - for (eps, cs) in events { - for ep in eps { - outbound_turns.entry(ep.connection).or_insert_with(Vec::new) - .push(ctor(ep.name, cs.clone())); - } - } -} diff --git a/src/skeleton.rs b/src/skeleton.rs index 3579a54..7c15aac 100644 --- a/src/skeleton.rs +++ b/src/skeleton.rs @@ -14,7 +14,8 @@ type Bag = bag::BTreeBag; pub type Path = Vec; pub type Paths = Vec; -pub type Events = Vec<(Vec, Captures)>; +pub type Events = Vec; +pub type TurnMap = Map; #[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Clone)] pub struct Endpoint { @@ -48,8 +49,7 @@ impl Index { Index{ all_assertions: Bag::new(), root: Node::new(Continuation::new(Set::new())) } } - pub fn add_endpoint(&mut self, analysis_results: &AnalysisResults, endpoint: Endpoint) - -> Vec + pub fn add_endpoint(&mut self, analysis_results: &AnalysisResults, endpoint: Endpoint) -> Events { let continuation = self.root.extend(&analysis_results.skeleton); let continuation_cached_assertions = &continuation.cached_assertions; @@ -112,8 +112,7 @@ impl Index { } } - pub fn insert(&mut self, outer_value: CachedAssertion) -> Events { - let mut outputs = Vec::new(); + pub fn insert(&mut self, outer_value: CachedAssertion, outputs: &mut TurnMap) { let net = self.all_assertions.change(outer_value.clone(), 1); match net { bag::Net::AbsentToPresent => { @@ -124,7 +123,10 @@ impl Index { |l, v| { l.cached_assertions.insert(v.clone()); }, |es, cs| { if es.cached_captures.change(cs.clone(), 1) == bag::Net::AbsentToPresent { - outputs.push((es.endpoints.iter().cloned().collect(), cs)) + for ep in &es.endpoints { + outputs.entry(ep.connection).or_insert_with(Vec::new) + .push(Event::Add(ep.name.clone(), cs.clone())) + } } }) .perform(&mut self.root); @@ -132,11 +134,9 @@ impl Index { bag::Net::PresentToPresent => (), _ => unreachable!(), } - outputs } - pub fn remove(&mut self, outer_value: CachedAssertion) -> Events { - let mut outputs = Vec::new(); + pub fn remove(&mut self, outer_value: CachedAssertion, outputs: &mut TurnMap) { let net = self.all_assertions.change(outer_value.clone(), -1); match net { bag::Net::PresentToAbsent => { @@ -147,7 +147,10 @@ impl Index { |l, v| { l.cached_assertions.remove(v); }, |es, cs| { if es.cached_captures.change(cs.clone(), -1) == bag::Net::PresentToAbsent { - outputs.push((es.endpoints.iter().cloned().collect(), cs)) + for ep in &es.endpoints { + outputs.entry(ep.connection).or_insert_with(Vec::new) + .push(Event::Del(ep.name.clone(), cs.clone())) + } } }) .perform(&mut self.root); @@ -155,11 +158,13 @@ impl Index { bag::Net::PresentToPresent => (), _ => unreachable!(), } - outputs } - pub fn send(&mut self, outer_value: CachedAssertion, delivery_count: &mut usize) -> Events { - let mut outputs = Vec::new(); + pub fn send(&mut self, + outer_value: CachedAssertion, + outputs: &mut TurnMap, + delivery_count: &mut usize) + { Modification::new( false, &outer_value, @@ -167,9 +172,11 @@ impl Index { |_l, _v| (), |es, cs| { *delivery_count += es.endpoints.len(); - outputs.push((es.endpoints.iter().cloned().collect(), cs)) + for ep in &es.endpoints { + outputs.entry(ep.connection).or_insert_with(Vec::new) + .push(Event::Msg(ep.name.clone(), cs.clone())) + } }).perform(&mut self.root); - outputs } pub fn assertion_count(&self) -> usize {