alamb commented on code in PR #8583: URL: https://github.com/apache/arrow-rs/pull/8583#discussion_r2422792278
########## arrow-array/Cargo.toml: ########## @@ -80,3 +80,7 @@ harness = false [[bench]] name = "union_array" harness = false + +[[bench]] +name = "record_batch" Review Comment: Could you please move this benchmark into its own PR (mostly to make it easier to run the benchmark against unmodified code)? ########## arrow-array/benches/record_batch.rs: ########## @@ -0,0 +1,92 @@ +// 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. + +use arrow_array::{ArrayRef, Int64Array, RecordBatch, RecordBatchOptions}; +use arrow_schema::{DataType, Field, Schema, SchemaRef}; +use criterion::*; +use num_integer::Integer; +use std::sync::Arc; + +fn make_record_batch(column_count: usize, row_count: usize) -> RecordBatch { + let fields = (0..column_count) + .map(|i| Field::new(format!("col_{}", i), DataType::Int64, i.is_even())) + .collect::<Vec<_>>(); + + let columns = fields + .iter() + .map(|_| { + let array_ref: ArrayRef = Arc::new(Int64Array::from_value(0, row_count)); + array_ref + }) + .collect::<Vec<_>>(); + + let schema = Schema::new(fields); + + let mut options = RecordBatchOptions::new(); + options.row_count = Some(row_count); + + RecordBatch::try_new_with_options(SchemaRef::new(schema), columns, &options).unwrap() +} + +fn project_benchmark( + c: &mut Criterion, + column_count: usize, + row_count: usize, + projection_size: usize, +) { + let input = make_input(column_count, row_count, projection_size); + + c.bench_with_input( + BenchmarkId::new( + "project", + format!( + "{:?}x{:?} -> {:?}x{:?}", + input.0.num_columns(), + input.0.num_rows(), + input.1.len(), + input.0.num_rows() + ), + ), + &input, + |b, (rb, projection)| { + b.iter(|| black_box(rb.project(projection).unwrap())); + }, + ); +} + +fn make_input( + column_count: usize, + row_count: usize, + projection_size: usize, +) -> (RecordBatch, Vec<usize>) { + let rb = make_record_batch(column_count, row_count); + let projection = (0..projection_size).collect::<Vec<_>>(); + (rb, projection) +} + +fn criterion_benchmark(c: &mut Criterion) { + project_benchmark(c, 100, 100, 1); + project_benchmark(c, 100, 100, 50); + project_benchmark(c, 100, 100, 99); Review Comment: I think a row count of 8192 is more realistic Also, adding a benchmark with 1000 columns would also be useful (and maybe show off your improvement more) ########## arrow-array/src/record_batch.rs: ########## @@ -445,14 +445,16 @@ impl RecordBatch { }) .collect::<Result<Vec<_>, _>>()?; - RecordBatch::try_new_with_options( - SchemaRef::new(projected_schema), - batch_fields, - &RecordBatchOptions { - match_field_names: true, - row_count: Some(self.row_count), - }, - ) + unsafe { Review Comment: I double checked the checks that happen as part of creating a `RecordBatch` and I agree they are entirely redundant when projecting an already valid RecordBatch https://github.com/apache/arrow-rs/blob/e5e4db9820bdf0b4cd3c2e8a62cd68b9318e81e4/arrow-array/src/record_batch.rs#L307-L365 -- 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]
