-
Notifications
You must be signed in to change notification settings - Fork 152
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
rework accumulator to store values in memos
We used to store values in a central map, but now each memo has an `AccumulatorMap` that maps accumulated values (if any). The primary goals of this change are * forward compatible with speculative execution because it puts more data into tables; * step towards a refactoring to stop tracking outputs in the same array as inputs and thus to simplify how we do versioning. We will no longer need to walk the function's outputs and refresh their versions and so forth because they are stored in the function memo and so they get refreshed automatically when the memo is refreshed.
- Loading branch information
1 parent
2caa5cc
commit 3687e48
Showing
17 changed files
with
319 additions
and
109 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
use std::any::Any; | ||
use std::fmt::Debug; | ||
|
||
use super::Accumulator; | ||
|
||
#[derive(Clone, Debug)] | ||
pub(crate) struct Accumulated<A: Accumulator> { | ||
values: Vec<A>, | ||
} | ||
|
||
pub(crate) trait AnyAccumulated: Any + Debug + Send + Sync { | ||
fn as_dyn_any(&self) -> &dyn Any; | ||
fn as_dyn_any_mut(&mut self) -> &mut dyn Any; | ||
fn cloned(&self) -> Box<dyn AnyAccumulated>; | ||
} | ||
|
||
impl<A: Accumulator> Accumulated<A> { | ||
pub fn push(&mut self, value: A) { | ||
self.values.push(value); | ||
} | ||
|
||
pub fn extend_with_accumulated(&self, values: &mut Vec<A>) { | ||
values.extend_from_slice(&self.values); | ||
} | ||
} | ||
|
||
impl<A: Accumulator> Default for Accumulated<A> { | ||
fn default() -> Self { | ||
Self { | ||
values: Default::default(), | ||
} | ||
} | ||
} | ||
|
||
impl<A> AnyAccumulated for Accumulated<A> | ||
where | ||
A: Accumulator, | ||
{ | ||
fn as_dyn_any(&self) -> &dyn Any { | ||
self | ||
} | ||
|
||
fn as_dyn_any_mut(&mut self) -> &mut dyn Any { | ||
self | ||
} | ||
|
||
fn cloned(&self) -> Box<dyn AnyAccumulated> { | ||
let this: Self = self.clone(); | ||
Box::new(this) | ||
} | ||
} | ||
|
||
impl dyn AnyAccumulated { | ||
pub fn accumulate<A: Accumulator>(&mut self, value: A) { | ||
self.as_dyn_any_mut() | ||
.downcast_mut::<Accumulated<A>>() | ||
.unwrap() | ||
.push(value); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
use rustc_hash::FxHashMap; | ||
|
||
use crate::IngredientIndex; | ||
|
||
use super::{accumulated::Accumulated, Accumulator, AnyAccumulated}; | ||
|
||
#[derive(Default, Debug)] | ||
pub struct AccumulatedMap { | ||
map: FxHashMap<IngredientIndex, Box<dyn AnyAccumulated>>, | ||
} | ||
|
||
impl AccumulatedMap { | ||
pub fn accumulate<A: Accumulator>(&mut self, index: IngredientIndex, value: A) { | ||
self.map | ||
.entry(index) | ||
.or_insert_with(|| <Box<Accumulated<A>>>::default()) | ||
.accumulate(value); | ||
} | ||
|
||
pub fn extend_with_accumulated<A: Accumulator>( | ||
&self, | ||
index: IngredientIndex, | ||
output: &mut Vec<A>, | ||
) { | ||
let Some(a) = self.map.get(&index) else { | ||
return; | ||
}; | ||
|
||
a.as_dyn_any() | ||
.downcast_ref::<Accumulated<A>>() | ||
.unwrap() | ||
.extend_with_accumulated(output); | ||
} | ||
} | ||
|
||
impl Clone for AccumulatedMap { | ||
fn clone(&self) -> Self { | ||
Self { | ||
map: self | ||
.map | ||
.iter() | ||
.map(|(&key, value)| (key, value.cloned())) | ||
.collect(), | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.