Skip to content

Commit

Permalink
perf: don't use dynamic dispatch in visitors (#15607)
Browse files Browse the repository at this point in the history
  • Loading branch information
ritchie46 committed Apr 12, 2024
1 parent 0a594af commit 5e74924
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 20 deletions.
16 changes: 8 additions & 8 deletions crates/polars-plan/src/logical_plan/visitor/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ use crate::prelude::*;
impl TreeWalker for Expr {
type Arena = ();

fn apply_children(
fn apply_children<F: FnMut(&Self, &Self::Arena) -> PolarsResult<VisitRecursion>>(
&self,
op: &mut dyn FnMut(&Self, &Self::Arena) -> PolarsResult<VisitRecursion>,
op: &mut F,
arena: &Self::Arena,
) -> PolarsResult<VisitRecursion> {
let mut scratch = unitvec![];
Expand All @@ -29,9 +29,9 @@ impl TreeWalker for Expr {
Ok(VisitRecursion::Continue)
}

fn map_children(
fn map_children<F: FnMut(Self, &mut Self::Arena) -> PolarsResult<Self>>(
self,
f: &mut dyn FnMut(Self, &mut Self::Arena) -> PolarsResult<Self>,
f: &mut F,
_arena: &mut Self::Arena,
) -> PolarsResult<Self> {
use polars_utils::functions::try_arc_map as am;
Expand Down Expand Up @@ -256,9 +256,9 @@ impl PartialEq for AExprArena<'_> {

impl TreeWalker for AexprNode {
type Arena = Arena<AExpr>;
fn apply_children(
fn apply_children<F: FnMut(&Self, &Self::Arena) -> PolarsResult<VisitRecursion>>(
&self,
op: &mut dyn FnMut(&Self, &Self::Arena) -> PolarsResult<VisitRecursion>,
op: &mut F,
arena: &Self::Arena,
) -> PolarsResult<VisitRecursion> {
let mut scratch = unitvec![];
Expand All @@ -276,9 +276,9 @@ impl TreeWalker for AexprNode {
Ok(VisitRecursion::Continue)
}

fn map_children(
fn map_children<F: FnMut(Self, &mut Self::Arena) -> PolarsResult<Self>>(
mut self,
op: &mut dyn FnMut(Self, &mut Self::Arena) -> PolarsResult<Self>,
op: &mut F,
arena: &mut Self::Arena,
) -> PolarsResult<Self> {
let mut scratch = unitvec![];
Expand Down
8 changes: 4 additions & 4 deletions crates/polars-plan/src/logical_plan/visitor/lp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,9 @@ pub type IRNodeArena = (Arena<IR>, Arena<AExpr>);
impl TreeWalker for IRNode {
type Arena = IRNodeArena;

fn apply_children(
fn apply_children<F: FnMut(&Self, &Self::Arena) -> PolarsResult<VisitRecursion>>(
&self,
op: &mut dyn FnMut(&Self, &Self::Arena) -> PolarsResult<VisitRecursion>,
op: &mut F,
arena: &Self::Arena,
) -> PolarsResult<VisitRecursion> {
let mut scratch = unitvec![];
Expand All @@ -66,9 +66,9 @@ impl TreeWalker for IRNode {
Ok(VisitRecursion::Continue)
}

fn map_children(
fn map_children<F: FnMut(Self, &mut Self::Arena) -> PolarsResult<Self>>(
self,
op: &mut dyn FnMut(Self, &mut Self::Arena) -> PolarsResult<Self>,
op: &mut F,
arena: &mut Self::Arena,
) -> PolarsResult<Self> {
let mut inputs = vec![];
Expand Down
16 changes: 8 additions & 8 deletions crates/polars-plan/src/logical_plan/visitor/visitors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,23 @@ use super::*;
/// Implemented for [`crate::dsl::Expr`] and [`AexprNode`].
pub trait TreeWalker: Sized {
type Arena;
fn apply_children(
fn apply_children<F: FnMut(&Self, &Self::Arena) -> PolarsResult<VisitRecursion>>(
&self,
op: &mut dyn FnMut(&Self, &Self::Arena) -> PolarsResult<VisitRecursion>,
op: &mut F,
arena: &Self::Arena,
) -> PolarsResult<VisitRecursion>;

fn map_children(
fn map_children<F: FnMut(Self, &mut Self::Arena) -> PolarsResult<Self>>(
self,
op: &mut dyn FnMut(Self, &mut Self::Arena) -> PolarsResult<Self>,
op: &mut F,
arena: &mut Self::Arena,
) -> PolarsResult<Self>;

/// Walks all nodes in depth-first-order.
#[recursive]
fn visit(
fn visit<V: Visitor<Node = Self, Arena = Self::Arena>>(
&self,
visitor: &mut dyn Visitor<Node = Self, Arena = Self::Arena>,
visitor: &mut V,
arena: &Self::Arena,
) -> PolarsResult<VisitRecursion> {
match visitor.pre_visit(self, arena)? {
Expand All @@ -44,9 +44,9 @@ pub trait TreeWalker: Sized {
}

#[recursive]
fn rewrite(
fn rewrite<R: RewritingVisitor<Node = Self, Arena = Self::Arena>>(
self,
rewriter: &mut dyn RewritingVisitor<Node = Self, Arena = Self::Arena>,
rewriter: &mut R,
arena: &mut Self::Arena,
) -> PolarsResult<Self> {
let mutate_this_node = match rewriter.pre_visit(&self, arena)? {
Expand Down

0 comments on commit 5e74924

Please sign in to comment.