Skip to main content

vortex_array/optimizer/
mod.rs

1// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: Copyright the Vortex contributors
3
4//! The optimizer applies metadata-only rewrite rules (`reduce` and `reduce_parent`) in a
5//! fixpoint loop until no more transformations are possible.
6//!
7//! Optimization runs between execution steps, which is what enables cross-step optimizations:
8//! after a child is decoded, new `reduce_parent` rules may match that were previously blocked.
9//!
10//! There are three public entry points on [`ArrayOptimizer`]:
11//!
12//! - [`ArrayOptimizer::optimize`] uses only static rules registered on encoding vtables.
13//! - [`ArrayOptimizer::optimize_ctx`] also consults session-scoped [`ArrayKernels`] before
14//!   static parent-reduce rules, so this is the entry point used by execution.
15//! - [`ArrayOptimizer::optimize_recursive`] applies the session-aware optimizer to the root and
16//!   every descendant.
17
18use vortex_error::VortexResult;
19use vortex_error::vortex_bail;
20use vortex_session::SessionExt;
21use vortex_session::VortexSession;
22
23use crate::ArrayRef;
24use crate::optimizer::kernels::ArrayKernels;
25
26pub mod kernels;
27pub mod rules;
28
29/// Extension trait for optimizing array trees using reduce/reduce_parent rules.
30pub trait ArrayOptimizer {
31    /// Optimize the root array node by running reduce and reduce_parent rules to fixpoint.
32    ///
33    /// This uses only static rules registered on encoding vtables. Use [`Self::optimize_ctx`]
34    /// when session-registered [`ArrayKernels`] should participate.
35    fn optimize(&self) -> VortexResult<ArrayRef>;
36
37    /// Optimize the root array node using static rules and any [`ArrayKernels`] on `session`.
38    ///
39    /// Session kernels are checked for each `(parent_encoding_id, child_encoding_id)` pair before
40    /// the child's static `PARENT_RULES`. If `session` does not contain [`ArrayKernels`], this
41    /// behaves like [`Self::optimize`].
42    fn optimize_ctx(&self, session: &VortexSession) -> VortexResult<ArrayRef>;
43
44    /// Optimize the entire array tree recursively (root and all descendants).
45    ///
46    /// This uses the same session-aware rule ordering as [`Self::optimize_ctx`] for every node in
47    /// the tree.
48    fn optimize_recursive(&self, session: &VortexSession) -> VortexResult<ArrayRef>;
49}
50
51impl ArrayOptimizer for ArrayRef {
52    fn optimize(&self) -> VortexResult<ArrayRef> {
53        Ok(try_optimize(self, None)?.unwrap_or_else(|| self.clone()))
54    }
55
56    fn optimize_ctx(&self, session: &VortexSession) -> VortexResult<ArrayRef> {
57        Ok(try_optimize(self, Some(session))?.unwrap_or_else(|| self.clone()))
58    }
59
60    fn optimize_recursive(&self, session: &VortexSession) -> VortexResult<ArrayRef> {
61        Ok(try_optimize_recursive(self, session)?.unwrap_or_else(|| self.clone()))
62    }
63}
64
65fn try_optimize(
66    array: &ArrayRef,
67    session: Option<&VortexSession>,
68) -> VortexResult<Option<ArrayRef>> {
69    let mut current_array = array.clone();
70    let mut any_optimizations = false;
71    let array_ref = session.and_then(|s| s.get_opt::<ArrayKernels>());
72
73    // Apply reduction rules to the current array until no more rules apply.
74    let mut loop_counter = 0;
75    'outer: loop {
76        if loop_counter > 100 {
77            vortex_bail!("Exceeded maximum optimization iterations (possible infinite loop)");
78        }
79        loop_counter += 1;
80
81        if let Some(new_array) = current_array.reduce()? {
82            current_array = new_array;
83            any_optimizations = true;
84            continue;
85        }
86
87        // Apply parent reduction rules to each slot in the context of the current array.
88        // Its important to take all slots here, as `current_array` can change inside the loop.
89        for (slot_idx, slot) in current_array.slots().iter().enumerate() {
90            let Some(child) = slot else { continue };
91
92            // Session kernels take precedence over the child encoding's static PARENT_RULES.
93            if let Some(array_ref) = &array_ref
94                && let Some(plugins) =
95                    array_ref.find_reduce_parent(current_array.encoding_id(), child.encoding_id())
96            {
97                for plugin in plugins.as_ref() {
98                    if let Some(new_array) = plugin(child, &current_array, slot_idx)? {
99                        current_array = new_array;
100                        any_optimizations = true;
101                        continue 'outer;
102                    }
103                }
104            }
105
106            if let Some(new_array) = child.reduce_parent(&current_array, slot_idx)? {
107                // If the parent was replaced, then we attempt to reduce it again.
108                current_array = new_array;
109                any_optimizations = true;
110
111                // Continue to the start of the outer loop
112                continue 'outer;
113            }
114        }
115
116        // No more optimizations can be applied
117        break;
118    }
119
120    if any_optimizations {
121        Ok(Some(current_array))
122    } else {
123        Ok(None)
124    }
125}
126
127fn try_optimize_recursive(
128    array: &ArrayRef,
129    session: &VortexSession,
130) -> VortexResult<Option<ArrayRef>> {
131    let mut current_array = array.clone();
132    let mut any_optimizations = false;
133
134    if let Some(new_array) = try_optimize(&current_array, Some(session))? {
135        current_array = new_array;
136        any_optimizations = true;
137    }
138
139    let mut new_slots = Vec::with_capacity(current_array.slots().len());
140    let mut any_slot_optimized = false;
141    for slot in current_array.slots() {
142        match slot {
143            Some(child) => {
144                if let Some(new_child) = try_optimize_recursive(child, session)? {
145                    new_slots.push(Some(new_child));
146                    any_slot_optimized = true;
147                } else {
148                    new_slots.push(Some(child.clone()));
149                }
150            }
151            None => new_slots.push(None),
152        }
153    }
154
155    if any_slot_optimized {
156        current_array = current_array.with_slots(new_slots)?;
157        any_optimizations = true;
158    }
159
160    if any_optimizations {
161        Ok(Some(current_array))
162    } else {
163        Ok(None)
164    }
165}