@@ -44,7 +44,7 @@ impl ArrayOptimizer {
4444 /// Optimize the given array by applying registered rewrite rules.
4545 pub fn optimize_array ( & self , array : & ArrayRef ) -> VortexResult < ArrayRef > {
4646 // To handle large and bushy plan trees, we implement iterative optimizer passes.
47- // We need to know how to do one step of optimizer here .
47+ // We maintain a stack of jobs, which are array tree nodes that we will optimize .
4848
4949 let mut job_id = 0 ;
5050 let mut make_job = |array : ArrayRef | {
@@ -71,7 +71,18 @@ impl ArrayOptimizer {
7171 dtype : DType ,
7272 }
7373
74- // mapping of results
74+ impl OptimizerJob {
75+ // Replace the target array of this job with the new array.
76+ // This also causes all the children to be dropped.
77+ fn replace ( mut self , array : ArrayRef ) -> Self {
78+ self . child_tasks . clear ( ) ;
79+ self . unoptimized_children = array. children ( ) ;
80+ self . array = array;
81+ self
82+ }
83+ }
84+
85+ // optimized array nodes
7586 let mut results: HashMap < usize , ArrayRef > = HashMap :: new ( ) ;
7687 let mut optimize_stack = VecDeque :: new ( ) ;
7788
@@ -83,8 +94,8 @@ impl ArrayOptimizer {
8394 tracing:: debug!( "Starting array optimization\n {}" , array. display_tree( ) ) ;
8495
8596 while !optimize_stack. is_empty ( ) {
86- // Pop off another job. This is an array which may have several children that need
87- // to be optimized before it can itself be optimized.
97+ // Visit the next job. Jobs are array tree nodes, which may have several children that
98+ // need optimization before it itself be optimized.
8899 let mut job = optimize_stack. pop_front ( ) . unwrap ( ) ;
89100
90101 if let Some ( child) = job. unoptimized_children . pop ( ) {
@@ -116,9 +127,7 @@ impl ArrayOptimizer {
116127
117128 if let Some ( new_array) = self . apply_reduce_rules ( & array) ? {
118129 // Update the job with the same job ID, but new children and new array
119- job. unoptimized_children = new_array. children ( ) ;
120- job. child_tasks . clear ( ) ;
121- job. array = new_array;
130+ let job = job. replace ( new_array) ;
122131 optimize_stack. push_front ( job) ;
123132 continue ;
124133 }
0 commit comments