kosiew commented on code in PR #21891: URL: https://github.com/apache/datafusion/pull/21891#discussion_r3199791039
########## datafusion/functions-nested/benches/array_range.rs: ########## @@ -0,0 +1,172 @@ +// 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 range and generate_series functions. + +use arrow::array::{ArrayRef, Int64Array}; +use arrow::datatypes::{DataType, Field}; +use criterion::{BenchmarkId, Criterion, criterion_group, criterion_main}; +use datafusion_common::{ScalarValue, config::ConfigOptions}; +use datafusion_expr::{ColumnarValue, ScalarFunctionArgs, ScalarUDFImpl}; +use datafusion_functions_nested::range::{Range, gen_series_udf}; +use rand::rngs::StdRng; +use rand::{Rng, SeedableRng}; +use std::hint::black_box; +use std::sync::Arc; + +const NUM_ROWS: &[usize] = &[100, 1000, 10000]; +const INT_STEPS: &[i64] = &[1, 5, 50]; Review Comment: Small benchmark suggestion: this currently covers positive steps with `start < stop`, but negative-step ranges are supported and the new code has a separate branch for them. It would be helpful to add a decreasing input set with negative steps so the perf coverage includes both hot paths and can catch under-reserving or branch-specific regressions. ########## datafusion/functions-nested/src/range.rs: ########## @@ -542,32 +538,57 @@ fn retrieve_range_args( Some((start, stop, step)) } -/// Returns an iterator of i64 values from start to stop -fn gen_range_iter( +/// Generate integer range values directly into the provided buffer. +#[inline] +fn generate_range_values( start: i64, stop: i64, - decreasing: bool, + step: i64, include_upper: bool, -) -> Box<dyn Iterator<Item = i64>> { - match (decreasing, include_upper) { - // Decreasing range, stop is inclusive - (true, true) => Box::new((stop..=start).rev()), - // Decreasing range, stop is exclusive - (true, false) => { - if stop == i64::MAX { - // start is never greater than stop, and stop is exclusive, - // so the decreasing range must be empty. - Box::new(std::iter::empty()) - } else { - // Increase the stop value by one to exclude it. - // Since stop is not i64::MAX, `stop + 1` will not overflow. - Box::new((stop + 1..=start).rev()) + values: &mut Vec<i64>, +) { + if !include_upper && start == stop { + return; + } + + if step > 0 { + let limit = if include_upper { + stop + } else { + stop.saturating_sub(1) + }; + if start > limit { + return; + } + let count = ((limit - start) as u64 / step as u64 + 1) as usize; Review Comment: I think this reserve calculation needs to avoid signed subtraction. The new direct Int64 loop still needs to preserve the previous `range` and `generate_series` behavior for all valid `i64` inputs, including small-output ranges that span `i64::MIN..=i64::MAX`. For example, `range(-9223372036854775808, 9223372036854775807, 9223372036854775807)` and `generate_series(-9223372036854775808, 9223372036854775807, 9223372036854775807)` can overflow at `limit - start`. In debug CI this can panic, and in release it can wrap and try to reserve a huge capacity. The negative-step branch around line 582 appears to have the same pattern. Could you compute the distance with overflow-safe unsigned arithmetic instead, for example `start.abs_diff(limit) / step.unsigned_abs() + 1`, and add regression coverage for full-span, large-step positive and negative cases? -- 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]
