jayzhan211 commented on code in PR #24800:
URL: https://github.com/apache/datafusion/pull/24800#discussion_r3914329364


##########
datafusion/physical-expr/benches/equivalence_properties.rs:
##########
@@ -0,0 +1,150 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+//! Benchmarks for the ordering satisfaction checks on 
[`EquivalenceProperties`].
+//!
+//! These are called repeatedly during physical optimization (sort removal,
+//! `EnforceSorting`, `EnforceDistribution`, and the requirement checks for
+//! windows, joins and aggregates), so their cost shows up directly in planning
+//! time.
+//!
+//! The benchmarks are parameterized by the number of equivalence classes, 
since
+//! that -- not the schema width, which is behind an `Arc` -- is what these
+//! checks carry around.
+
+use std::sync::Arc;
+
+use arrow::compute::SortOptions;
+use arrow::datatypes::{DataType, Field, Schema, SchemaRef};
+use criterion::{BenchmarkId, Criterion, criterion_group, criterion_main};
+use datafusion_physical_expr::expressions::Column;
+use datafusion_physical_expr::{
+    EquivalenceProperties, LexOrdering, PhysicalExpr, PhysicalSortExpr,
+    PhysicalSortRequirement,
+};
+
+fn schema(n_cols: usize) -> SchemaRef {
+    Arc::new(Schema::new(
+        (0..n_cols)
+            .map(|i| Field::new(format!("c{i}"), DataType::Int32, true))
+            .collect::<Vec<_>>(),
+    ))
+}
+
+fn col(i: usize) -> Arc<dyn PhysicalExpr> {
+    Arc::new(Column::new(&format!("c{i}"), i))
+}
+
+fn asc(i: usize) -> PhysicalSortExpr {
+    PhysicalSortExpr::new(col(i), SortOptions::default())
+}
+
+/// Properties with three equivalent orderings and `n_classes` equivalence
+/// classes, i.e. roughly what a scan feeding a join and a window function 
looks
+/// like. Columns `c0..c7` carry the orderings; the equivalence classes are 
built
+/// from the columns above them.
+fn properties(n_classes: usize) -> EquivalenceProperties {
+    let schema = schema(8 + 2 * n_classes);
+    let mut props = EquivalenceProperties::new(schema);
+    props.add_orderings([
+        vec![asc(0), asc(1), asc(2), asc(3)],
+        vec![asc(4), asc(5)],
+        vec![asc(6)],
+    ]);
+    for i in 0..n_classes {
+        props
+            .add_equal_conditions(col(8 + 2 * i), col(9 + 2 * i))
+            .unwrap();
+    }
+    props
+}
+
+fn bench_ordering_satisfaction(c: &mut Criterion) {
+    let mut group = c.benchmark_group("equivalence_properties");
+
+    for n_classes in [2, 8, 32] {
+        let props = properties(n_classes);
+
+        // A single sort key: the most common shape by far.
+        group.bench_with_input(

Review Comment:
   Good point — the setup was measurable, not incidental. `asc(i)` does a 
`format!` plus an `Arc` allocation per key per iteration, which was a real 
fraction of a ~400 ns measurement.
   
   Switched to `iter_batched`: the sort exprs, requirements and the 
`LexOrdering` are now built once up front, and since each check takes its input 
by value, the untimed setup step hands each iteration a fresh clone. Only the 
call is timed. Also swapped `bench_with_input` for `bench_function`, as the 
parameter was only being used as a label.
   
   Cloning done *inside* the check stays inside the timed iteration — that is 
the thing this PR is about, so it has to be measured.
   
   It moved the numbers in the direction you'd expect. At 8 equivalence 
classes, 1 key:
   
   | | before | after | change |
   |---|---:|---:|---|
   | `ordering_satisfy` | 2.73 µs | 0.36 µs | −86.6% (was −84.9%) |
   | `ordering_satisfy_requirement` | 2.90 µs | 0.30 µs | −89.2% (was −86.3%) |
   
   The old numbers were understating the improvement, since the constant setup 
cost sat in both columns.
   
   I also documented the scope in the module header rather than leaving it 
implicit:
   
   ```rust
   //! # Scope
   //!
   //! These measure the satisfaction check itself, not the cost of assembling 
its
   //! arguments. The sort expressions, requirements and orderings are built 
once,
   //! up front. Because the checks take their input by value, each iteration 
gets a
   //! fresh copy from the untimed setup step of `iter_batched`; only the call 
is
   //! timed. Any copying the check does internally is part of what is measured.
   ```
   
   Metrics in the PR description updated to match.
   



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to