@@ -116,17 +116,26 @@ More information on the new streaming engine: https://github.com/pola-rs/polars/
116116 #[ cfg( debug_assertions) ]
117117 let prev_schema = lp_arena. get ( lp_top) . schema ( lp_arena) . into_owned ( ) ;
118118
119- // Collect members for optimizations that need it.
120- let mut members = MemberCollector :: new ( ) ;
121- if !opt_flags. eager ( ) && ( comm_subexpr_elim || opt_flags. projection_pushdown ( ) ) {
122- members. collect ( lp_top, lp_arena, expr_arena)
119+ let mut _opt_members = & mut None ;
120+
121+ macro_rules! get_or_init_members {
122+ ( ) => {
123+ _get_or_init_members( _opt_members, lp_top, lp_arena, expr_arena)
124+ } ;
125+ }
126+
127+ macro_rules! get_members_opt {
128+ ( ) => {
129+ _opt_members. as_mut( )
130+ } ;
123131 }
124132
125133 // Run before slice pushdown
126- if opt_flags. contains ( OptFlags :: CHECK_ORDER_OBSERVE )
127- && members. has_group_by | members. has_sort | members. has_distinct
128- {
129- set_order_flags ( lp_top, lp_arena, expr_arena, scratch) ;
134+ if opt_flags. contains ( OptFlags :: CHECK_ORDER_OBSERVE ) {
135+ let members = get_or_init_members ! ( ) ;
136+ if members. has_group_by | members. has_sort | members. has_distinct {
137+ set_order_flags ( lp_top, lp_arena, expr_arena, scratch) ;
138+ }
130139 }
131140
132141 if opt_flags. simplify_expr ( ) {
@@ -135,21 +144,24 @@ More information on the new streaming engine: https://github.com/pola-rs/polars/
135144 }
136145
137146 #[ cfg( feature = "cse" ) ]
138- let _cse_plan_changed = if comm_subplan_elim
139- && members. has_joins_or_unions
140- && members. has_duplicate_scans ( )
141- && !members. has_cache
142- {
143- if verbose {
144- eprintln ! ( "found multiple sources; run comm_subplan_elim" )
145- }
146- let ( lp, changed, cid2c) = cse:: elim_cmn_subplans ( lp_top, lp_arena, expr_arena) ;
147+ let _cse_plan_changed = if comm_subplan_elim {
148+ let members = get_or_init_members ! ( ) ;
149+
150+ if members. has_joins_or_unions && members. has_duplicate_scans ( ) && !members. has_cache {
151+ if verbose {
152+ eprintln ! ( "found multiple sources; run comm_subplan_elim" )
153+ }
147154
148- prune_unused_caches ( lp_arena , cid2c) ;
155+ let ( lp , changed , cid2c) = cse :: elim_cmn_subplans ( lp_top , lp_arena , expr_arena ) ;
149156
150- lp_top = lp;
151- members. has_cache |= changed;
152- changed
157+ prune_unused_caches ( lp_arena, cid2c) ;
158+
159+ lp_top = lp;
160+ members. has_cache |= changed;
161+ changed
162+ } else {
163+ false
164+ }
153165 } else {
154166 false
155167 } ;
@@ -181,7 +193,7 @@ More information on the new streaming engine: https://github.com/pola-rs/polars/
181193 }
182194
183195 // Make sure it is after predicate pushdown
184- if opt_flags. collapse_joins ( ) && members . has_filter_with_join_input {
196+ if opt_flags. collapse_joins ( ) && get_or_init_members ! ( ) . has_filter_with_join_input {
185197 collapse_joins:: optimize ( lp_top, lp_arena, expr_arena) ;
186198 }
187199
@@ -219,7 +231,10 @@ More information on the new streaming engine: https://github.com/pola-rs/polars/
219231
220232 lp_top = opt. optimize_loop ( & mut rules, expr_arena, lp_arena, lp_top) ?;
221233
222- if members. has_joins_or_unions && members. has_cache && _cse_plan_changed {
234+ if _cse_plan_changed
235+ && get_members_opt ! ( )
236+ . is_some_and ( |members| members. has_joins_or_unions && members. has_cache )
237+ {
223238 // We only want to run this on cse inserted caches
224239 cache_states:: set_cache_states (
225240 lp_top,
@@ -234,7 +249,7 @@ More information on the new streaming engine: https://github.com/pola-rs/polars/
234249
235250 // This one should run (nearly) last as this modifies the projections
236251 #[ cfg( feature = "cse" ) ]
237- if comm_subexpr_elim && !members . has_ext_context {
252+ if comm_subexpr_elim && !get_or_init_members ! ( ) . has_ext_context {
238253 let mut optimizer = CommonSubExprOptimizer :: new ( ) ;
239254 let alp_node = IRNode :: new_mutate ( lp_top) ;
240255
@@ -260,3 +275,17 @@ More information on the new streaming engine: https://github.com/pola-rs/polars/
260275
261276 Ok ( lp_top)
262277}
278+
279+ fn _get_or_init_members < ' a > (
280+ opt_members : & ' a mut Option < MemberCollector > ,
281+ lp_top : Node ,
282+ lp_arena : & mut Arena < IR > ,
283+ expr_arena : & mut Arena < AExpr > ,
284+ ) -> & ' a mut MemberCollector {
285+ opt_members. get_or_insert_with ( || {
286+ let mut members = MemberCollector :: new ( ) ;
287+ members. collect ( lp_top, lp_arena, expr_arena) ;
288+
289+ members
290+ } )
291+ }
0 commit comments