martin-g commented on code in PR #20116: URL: https://github.com/apache/datafusion/pull/20116#discussion_r2759299680
########## datafusion/spark/benches/sha2.rs: ########## @@ -0,0 +1,100 @@ +// 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. + +extern crate criterion; + +use arrow::array::*; +use arrow::datatypes::*; +use criterion::{Criterion, criterion_group, criterion_main}; +use datafusion_common::ScalarValue; +use datafusion_common::config::ConfigOptions; +use datafusion_expr::{ColumnarValue, ScalarFunctionArgs, ScalarUDFImpl}; +use datafusion_spark::function::hash::sha2::SparkSha2; +use rand::rngs::StdRng; +use rand::{Rng, SeedableRng}; +use std::hint::black_box; +use std::sync::Arc; + +fn seedable_rng() -> StdRng { + StdRng::seed_from_u64(42) +} + +fn generate_binary_data(size: usize, null_density: f32) -> BinaryArray { + let mut rng = seedable_rng(); + let mut builder = BinaryBuilder::new(); + for _ in 0..size { + if rng.random::<f32>() < null_density { + builder.append_null(); + } else { + let len = rng.random_range::<usize, _>(1..=100); + let bytes: Vec<u8> = (0..len).map(|_| rng.random()).collect(); + builder.append_value(&bytes); + } + } + builder.finish() +} + +fn run_benchmark(c: &mut Criterion, name: &str, size: usize, args: &[ColumnarValue]) { + let sha2_func = SparkSha2::new(); + let arg_fields: Vec<_> = args + .iter() + .enumerate() + .map(|(idx, arg)| Field::new(format!("arg_{idx}"), arg.data_type(), true).into()) + .collect(); + let config_options = Arc::new(ConfigOptions::default()); + + c.bench_function(&format!("{name}/size={size}"), |b| { + b.iter(|| { + black_box( + sha2_func + .invoke_with_args(ScalarFunctionArgs { + args: args.to_vec(), + arg_fields: arg_fields.clone(), + number_rows: size, + return_field: Arc::new(Field::new("f", DataType::Utf8, true)), + config_options: Arc::clone(&config_options), + }) + .unwrap(), + ) + }) + }); +} + +fn criterion_benchmark(c: &mut Criterion) { + // Scalar benchmark (avoid array expansion) + let scalar_args = vec![ + ColumnarValue::Scalar(ScalarValue::Binary(Some(b"Spark".to_vec()))), + ColumnarValue::Scalar(ScalarValue::Int32(Some(256))), + ]; + run_benchmark(c, "sha2/scalar", 1, &scalar_args); + + let sizes = vec![1024, 4096, 8192]; + let null_density = 0.1; + + for &size in &sizes { + let values = generate_binary_data(size, null_density); + let bit_lengths = Int32Array::from(vec![256; size]); + let array_args = vec![ + ColumnarValue::Array(Arc::new(values)), + ColumnarValue::Array(Arc::new(bit_lengths)), + ]; + run_benchmark(c, "sha2/array_binary_256", size, &array_args); Review Comment: Also bench array+scalar combination: ```suggestion run_benchmark(c, "sha2/array_binary_256", size, &array_args); let array_scalar_args = vec![ ColumnarValue::Array(Arc::new(values)), ColumnarValue::Scalar(ScalarValue::Int32(Some(256))), ]; run_benchmark(c, "sha2/array_scalar_binary_256", size, &array_scalar_args); ``` ########## datafusion/spark/src/function/hash/sha2.rs: ########## @@ -87,7 +88,98 @@ impl ScalarUDFImpl for SparkSha2 { } fn invoke_with_args(&self, args: ScalarFunctionArgs) -> Result<ColumnarValue> { - make_scalar_function(sha2_impl, vec![])(&args.args) + let values = &args.args[0]; + let bit_lengths = &args.args[1]; Review Comment: Maybe use `take_function_args()` for consistency with other functions and better error handling ? ########## datafusion/spark/src/function/hash/sha2.rs: ########## @@ -144,11 +257,18 @@ where Arc::new(array) } +const HEX_CHARS: [u8; 16] = *b"0123456789abcdef"; + +#[inline] fn hex_encode<T: AsRef<[u8]>>(data: T) -> String { - let mut s = String::with_capacity(data.as_ref().len() * 2); - for b in data.as_ref() { - // Writing to a string never errors, so we can unwrap here. - write!(&mut s, "{b:02x}").unwrap(); + let bytes = data.as_ref(); + let mut out = Vec::with_capacity(bytes.len() * 2); + for &b in bytes { + let hi = b >> 4; + let lo = b & 0x0F; + out.push(HEX_CHARS[hi as usize]); + out.push(HEX_CHARS[lo as usize]); Review Comment: The `hex` crate is used in other datafusion-** crates as an optional dependency. It also uses SIMD to be even faster for bigger input. Consider using it here too. -- 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]
