Skip to content

Commit

Permalink
refactor: avoid dynamic dispatch for non-folding operations (#770)
Browse files Browse the repository at this point in the history
  • Loading branch information
ss2165 committed Jan 3, 2024
1 parent 0e51723 commit ca7b605
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 9 deletions.
6 changes: 0 additions & 6 deletions src/extension/const_fold.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,6 @@ impl Debug for Box<dyn ConstFold> {
}
}

impl Default for Box<dyn ConstFold> {
fn default() -> Self {
Box::new(|&_: &_| None)
}
}

/// Blanket implementation for functions that only require the constants to
/// evaluate - type arguments are not relevant.
impl<T> ConstFold for T
Expand Down
6 changes: 3 additions & 3 deletions src/extension/op_def.rs
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,7 @@ pub struct OpDef {

/// Operations can optionally implement [`ConstFold`] to implement constant folding.
#[serde(skip)]
constant_folder: Box<dyn ConstFold>,
constant_folder: Option<Box<dyn ConstFold>>,
}

impl OpDef {
Expand Down Expand Up @@ -421,7 +421,7 @@ impl OpDef {
/// Set the constant folding function for this Op, which can evaluate it
/// given constant inputs.
pub fn set_constant_folder(&mut self, fold: impl ConstFold + 'static) {
self.constant_folder = Box::new(fold)
self.constant_folder = Some(Box::new(fold))
}

/// Evaluate an instance of this [`OpDef`] defined by the `type_args`, given
Expand All @@ -431,7 +431,7 @@ impl OpDef {
type_args: &[TypeArg],
consts: &[(crate::IncomingPort, crate::ops::Const)],
) -> ConstFoldResult {
self.constant_folder.fold(type_args, consts)
(self.constant_folder.as_ref())?.fold(type_args, consts)
}
}

Expand Down

0 comments on commit ca7b605

Please sign in to comment.