Skip to content

Commit

Permalink
refactor(hydroflow_plus): use location.flow_state() to avoid clone (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
shadaj authored Nov 6, 2024
1 parent 8442d1b commit 9f74405
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 37 deletions.
18 changes: 7 additions & 11 deletions hydroflow_plus/src/optional.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use std::rc::Rc;
use stageleft::{q, IntoQuotedMut, Quoted};
use syn::parse_quote;

use crate::builder::{FlowState, FLOW_USED_MESSAGE};
use crate::builder::FLOW_USED_MESSAGE;
use crate::cycle::{CycleCollection, CycleComplete, DeferTick, ForwardRef, TickCycle};
use crate::ir::{HfPlusLeaf, HfPlusNode, HfPlusSource, TeeNode};
use crate::location::{check_matching_location, LocationId, NoTick};
Expand Down Expand Up @@ -35,10 +35,6 @@ impl<'a, T, W, N: Location<'a>> Optional<T, W, N> {
fn location_kind(&self) -> LocationId {
self.location.id()
}

fn flow_state(&self) -> &FlowState {
self.location.flow_state()
}
}

impl<'a, T, N: Location<'a>> DeferTick for Optional<T, Bounded, Tick<N>> {
Expand All @@ -64,8 +60,8 @@ impl<'a, T, N: Location<'a>> CycleCollection<'a, TickCycle> for Optional<T, Boun

impl<'a, T, N: Location<'a>> CycleComplete<'a, TickCycle> for Optional<T, Bounded, Tick<N>> {
fn complete(self, ident: syn::Ident) {
self.flow_state()
.clone()
self.location
.flow_state()
.borrow_mut()
.leaves
.as_mut()
Expand Down Expand Up @@ -95,8 +91,8 @@ impl<'a, T, N: Location<'a>> CycleCollection<'a, ForwardRef> for Optional<T, Bou

impl<'a, T, N: Location<'a>> CycleComplete<'a, ForwardRef> for Optional<T, Bounded, Tick<N>> {
fn complete(self, ident: syn::Ident) {
self.flow_state()
.clone()
self.location
.flow_state()
.borrow_mut()
.leaves
.as_mut()
Expand Down Expand Up @@ -126,8 +122,8 @@ impl<'a, T, W, N: Location<'a> + NoTick> CycleCollection<'a, ForwardRef> for Opt

impl<'a, T, W, N: Location<'a> + NoTick> CycleComplete<'a, ForwardRef> for Optional<T, W, N> {
fn complete(self, ident: syn::Ident) {
self.flow_state()
.clone()
self.location
.flow_state()
.borrow_mut()
.leaves
.as_mut()
Expand Down
14 changes: 5 additions & 9 deletions hydroflow_plus/src/singleton.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use std::rc::Rc;

use stageleft::{q, IntoQuotedMut, Quoted};

use crate::builder::{FlowState, FLOW_USED_MESSAGE};
use crate::builder::FLOW_USED_MESSAGE;
use crate::cycle::{
CycleCollection, CycleCollectionWithInitial, CycleComplete, DeferTick, ForwardRef, TickCycle,
};
Expand Down Expand Up @@ -33,10 +33,6 @@ impl<'a, T, W, N: Location<'a>> Singleton<T, W, N> {
fn location_kind(&self) -> LocationId {
self.location.id()
}

fn flow_state(&self) -> &FlowState {
self.location.flow_state()
}
}

impl<'a, T, N: Location<'a>> From<Singleton<T, Bounded, N>> for Singleton<T, Unbounded, N> {
Expand Down Expand Up @@ -73,8 +69,8 @@ impl<'a, T, N: Location<'a>> CycleCollectionWithInitial<'a, TickCycle>

impl<'a, T, N: Location<'a>> CycleComplete<'a, TickCycle> for Singleton<T, Bounded, Tick<N>> {
fn complete(self, ident: syn::Ident) {
self.flow_state()
.clone()
self.location
.flow_state()
.borrow_mut()
.leaves
.as_mut()
Expand Down Expand Up @@ -104,8 +100,8 @@ impl<'a, T, N: Location<'a>> CycleCollection<'a, ForwardRef> for Singleton<T, Bo

impl<'a, T, N: Location<'a>> CycleComplete<'a, ForwardRef> for Singleton<T, Bounded, Tick<N>> {
fn complete(self, ident: syn::Ident) {
self.flow_state()
.clone()
self.location
.flow_state()
.borrow_mut()
.leaves
.as_mut()
Expand Down
28 changes: 11 additions & 17 deletions hydroflow_plus/src/stream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use serde::Serialize;
use stageleft::{q, IntoQuotedMut, Quoted};
use syn::parse_quote;

use crate::builder::{FlowState, FLOW_USED_MESSAGE};
use crate::builder::FLOW_USED_MESSAGE;
use crate::cycle::{CycleCollection, CycleComplete, DeferTick, ForwardRef, TickCycle};
use crate::ir::{DebugInstantiate, HfPlusLeaf, HfPlusNode, TeeNode};
use crate::location::cluster::ClusterSelfId;
Expand Down Expand Up @@ -46,10 +46,6 @@ pub struct Stream<T, B, N> {
}

impl<'a, T, W, N: Location<'a>> Stream<T, W, N> {
fn flow_state(&self) -> &FlowState {
self.location.flow_state()
}

fn location_kind(&self) -> LocationId {
self.location.id()
}
Expand Down Expand Up @@ -78,8 +74,8 @@ impl<'a, T, N: Location<'a>> CycleCollection<'a, TickCycle> for Stream<T, Bounde

impl<'a, T, N: Location<'a>> CycleComplete<'a, TickCycle> for Stream<T, Bounded, Tick<N>> {
fn complete(self, ident: syn::Ident) {
self.flow_state()
.clone()
self.location
.flow_state()
.borrow_mut()
.leaves
.as_mut()
Expand Down Expand Up @@ -109,8 +105,8 @@ impl<'a, T, W, N: Location<'a> + NoTick> CycleCollection<'a, ForwardRef> for Str

impl<'a, T, W, N: Location<'a> + NoTick> CycleComplete<'a, ForwardRef> for Stream<T, W, N> {
fn complete(self, ident: syn::Ident) {
self.flow_state()
.clone()
self.location
.flow_state()
.borrow_mut()
.leaves
.as_mut()
Expand Down Expand Up @@ -494,8 +490,8 @@ impl<'a, T, W, N: Location<'a> + NoTick> Stream<T, W, N> {
}

pub fn for_each<F: Fn(T) + 'a>(self, f: impl IntoQuotedMut<'a, F>) {
self.flow_state()
.clone()
self.location
.flow_state()
.borrow_mut()
.leaves
.as_mut()
Expand All @@ -507,8 +503,8 @@ impl<'a, T, W, N: Location<'a> + NoTick> Stream<T, W, N> {
}

pub fn dest_sink<S: Unpin + Sink<T> + 'a>(self, sink: impl Quoted<'a, S>) {
self.flow_state()
.clone()
self.location
.flow_state()
.borrow_mut()
.leaves
.as_mut()
Expand Down Expand Up @@ -664,8 +660,7 @@ impl<'a, T, W, N: Location<'a> + NoTick> Stream<T, W, N> {
{
let serialize_pipeline = Some(serialize_bincode::<CoreType>(N::is_demux()));

let flow_state = self.flow_state().clone();
let mut flow_state_borrow = flow_state.borrow_mut();
let mut flow_state_borrow = self.location.flow_state().borrow_mut();

let external_key = flow_state_borrow.next_external_out;
flow_state_borrow.next_external_out += 1;
Expand Down Expand Up @@ -725,8 +720,7 @@ impl<'a, T, W, N: Location<'a> + NoTick> Stream<T, W, N> {
where
N: CanSend<'a, ExternalProcess<'a, N2>, In<Bytes> = T, Out<Bytes> = Bytes>,
{
let flow_state = self.flow_state().clone();
let mut flow_state_borrow = flow_state.borrow_mut();
let mut flow_state_borrow = self.location.flow_state().borrow_mut();
let external_key = flow_state_borrow.next_external_out;
flow_state_borrow.next_external_out += 1;

Expand Down

0 comments on commit 9f74405

Please sign in to comment.