This is an automated email from the ASF dual-hosted git repository. github-merge-queue[bot] pushed a commit to branch gh-readonly-queue/main/pr-12079-ea3c266746a5108c4df23a34bd51c5e38ba5a4a7 in repository https://gitbox.apache.org/repos/asf/arrow-rs.git
commit d34dc3e21ae09c656d6ea05908c2b76e36078101 Author: Kosta Tarasov <[email protected]> AuthorDate: Thu Nov 5 04:25:34 2026 +0000 [Variant] Add unshred_variant benchmarks (#12079) # Which issue does this PR close? - Part of #11101. # Rationale for this change No benchmark calls `unshred_variant`, directly or through `variant_get`, so changes to this kernel can't be compared on the existing suite. #11106 is a current example: it changes how nested fallback values are written, and nothing measures that path. # What changes are included in this PR? Adds seven `unshred_variant` benchmarks to `variant_kernels`, each processing 8,192 rows. The cases are chosen so that a change to one path should not move the others. | Benchmark | Rows | What it isolates | ns/row | |---|---|---|---:| | `unshred_variant_primitive` | Int64, fully typed | Row loop and metadata validation | 41 | | `unshred_variant_object` | `{first, last, age}`, fully typed | Object construction with a small dictionary | 406 | | `unshred_variant_partial_object` | 5 of 15 fields typed, 10 leftover, one sorted 300-name dictionary shared by all rows | Merging typed and leftover fields, binary search name lookups | 3,354 | | `unshred_variant_partial_object_unsorted_metadata` | Same documents, unsorted dictionary | Linear scan name lookups | 18,020 | | `unshred_variant_nested_fallback` | Field shredded as Int64 holding an object 5 levels deep with 2 fields per object | Rebuilding a nested fallback value | 10,223 | | `unshred_variant_list` | `List<Float64>`, 0 to 100 elements per row | Per-element list cost | 775 | | `unshred_variant_list_of_objects` | 8 typed objects per row | Objects in lists, repeated name lookups within a row | 1,589 | Times are medians of 3 Criterion runs on one workstation, to show scale only. Left out on purpose: the unshredded fast path, because it is a constant-time clone, and other primitive types and metadata encodings, which stayed within about 12 ns and about 5% of these cases in ad hoc runs. The partial object schema and the JSON-to-variant setup move into small helpers shared with existing benchmarks, and the object fixture can now take its dictionary order. The existing benchmarks keep their names and workloads. # Are these changes tested? This is benchmark-only. As a check that the suite catches what it is for, applying the source changes from #11106 moves `unshred_variant_nested_fallback` from 10,223 to 18,917 ns per row (1.85x), while the other six cases stay between 0.99x and 1.03x. # Are there any significant user-facing changes? No, only benchmark code changes. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-authored-by: Claude Opus 5.5 <[email protected]> --- parquet-variant-compute/benches/variant_kernels.rs | 142 ++++++++++++++++++--- 1 file changed, 122 insertions(+), 20 deletions(-) diff --git a/parquet-variant-compute/benches/variant_kernels.rs b/parquet-variant-compute/benches/variant_kernels.rs index e36c85ddf1..39a900f016 100644 --- a/parquet-variant-compute/benches/variant_kernels.rs +++ b/parquet-variant-compute/benches/variant_kernels.rs @@ -29,8 +29,8 @@ use parquet_variant::{ VariantPath, VariantPathElement, }; use parquet_variant_compute::{ - GetOptions, VariantArray, VariantArrayBuilder, json_to_variant, shred_variant, variant_get, - variant_to_json, + GetOptions, VariantArray, VariantArrayBuilder, json_to_variant, shred_variant, unshred_variant, + variant_get, variant_to_json, }; use parquet_variant_json::append_json; use rand::RngExt; @@ -426,21 +426,7 @@ pub fn variant_get_object_path_metadata_bench(c: &mut Criterion) { /// lookup is visible. pub fn shred_variant_partial_object_bench(c: &mut Criterion) { let variant_array = create_unshredded_object_variant_array(VARIANT_BATCH_ROWS); - - // The source objects have 15 fields (`attr.000`, `attr.020`, ... `attr.280`). Shred the first - // 5 of them, leaving the other 10 to be written to the leftover `value` column. - let shredded_fields = (0..300) - .step_by(20) - .take(5) - .map(|index| { - Arc::new(Field::new( - format!("attr.{index:03}"), - DataType::Int32, - true, - )) - }) - .collect::<Vec<FieldRef>>(); - let as_type = DataType::Struct(Fields::from(shredded_fields)); + let as_type = partial_object_shredding_type(); c.bench_function("shred_variant_partial_object_8k_rows", |b| { b.iter(|| std::hint::black_box(shred_variant(&variant_array, &as_type).unwrap())) @@ -468,6 +454,84 @@ pub fn shred_variant_unmatched_object_bench(c: &mut Criterion) { }); } +pub fn unshred_variant_bench(c: &mut Criterion) { + // Little work per row makes the row loop and metadata validation visible. + bench_unshred_variant( + c, + "unshred_variant_primitive", + create_primitive_variant_array(VARIANT_BATCH_ROWS), + DataType::Int64, + ); + + // Every field comes from a typed column and is looked up in a small dictionary. + bench_unshred_variant( + c, + "unshred_variant_object", + create_variant_array_from_json(json_repeated_struct(VARIANT_BATCH_ROWS)), + DataType::Struct(Fields::from(vec![ + Field::new("first", DataType::Utf8, true), + Field::new("last", DataType::Utf8, true), + Field::new("age", DataType::Int64, true), + ])), + ); + + // Typed fields merge with leftover fields, and all rows share a 300-name dictionary. + bench_unshred_variant( + c, + "unshred_variant_partial_object", + create_unshredded_object_variant_array(VARIANT_BATCH_ROWS), + partial_object_shredding_type(), + ); + + // Same documents, but an unsorted dictionary makes every field name lookup a linear scan. + bench_unshred_variant( + c, + "unshred_variant_partial_object_unsorted_metadata", + create_unshredded_object_variant_array_with_names(VARIANT_BATCH_ROWS, (0..300).rev()), + partial_object_shredding_type(), + ); + + // `a` holds an object instead of its shredded Int64, and rebuilding it visits every level. + let json = format!(r#"{{"a":{}}}"#, json_nested_object(5)); + bench_unshred_variant( + c, + "unshred_variant_nested_fallback", + create_variant_array_from_json(std::iter::repeat_n(json, VARIANT_BATCH_ROWS)), + DataType::Struct(Fields::from(vec![Field::new("a", DataType::Int64, true)])), + ); + + // Lists recurse through elements without metadata field-name lookups. + bench_unshred_variant( + c, + "unshred_variant_list", + create_variant_array_from_json(json_repeated_list(VARIANT_BATCH_ROWS)), + DataType::List(Arc::new(Field::new("item", DataType::Float64, true))), + ); + + // Field name lookups repeat for each of the 8 objects in a row. + let json = format!("[{}]", [r#"{"id":1,"name":"n"}"#; 8].join(",")); + let element = DataType::Struct(Fields::from(vec![ + Field::new("id", DataType::Int64, true), + Field::new("name", DataType::Utf8, true), + ])); + bench_unshred_variant( + c, + "unshred_variant_list_of_objects", + create_variant_array_from_json(std::iter::repeat_n(json, VARIANT_BATCH_ROWS)), + DataType::List(Arc::new(Field::new("item", element, true))), + ); +} + +fn bench_unshred_variant( + c: &mut Criterion, + name: &str, + variant_array: VariantArray, + as_type: DataType, +) { + let input = shred_variant(&variant_array, &as_type).unwrap(); + c.bench_function(name, |b| b.iter(|| unshred_variant(&input).unwrap())); +} + pub fn variant_get_utf8_from_int_bench(c: &mut Criterion) { bench_variant_get_utf8(c, "variant_get_utf8_from_int", |rng, array_size| { let mut vab = VariantArrayBuilder::new(array_size); @@ -874,6 +938,7 @@ criterion_group!( variant_get_object_path_metadata_bench, shred_variant_partial_object_bench, shred_variant_unmatched_object_bench, + unshred_variant_bench, variant_array_builder_build_bench, benchmark_batch_json_string_to_variant, variant_to_json_bench, @@ -922,8 +987,7 @@ fn create_object_variant_array(size: usize) -> VariantArray { json.replace("\"age\":", "\"years\":") } }); - let input: ArrayRef = Arc::new(StringArray::from_iter_values(json)); - json_to_variant(&input).unwrap() + create_variant_array_from_json(json) } /// Encodes the two metadata dictionaries from `create_object_variant_array` while @@ -1008,7 +1072,14 @@ fn create_per_row_metadata_variant_array(json_templates: &[Value], size: usize) } fn create_unshredded_object_variant_array(size: usize) -> VariantArray { - let field_names = (0..300) + create_unshredded_object_variant_array_with_names(size, 0..300) +} + +fn create_unshredded_object_variant_array_with_names( + size: usize, + names: impl Iterator<Item = usize>, +) -> VariantArray { + let field_names = names .map(|index| format!("attr.{index:03}")) .collect::<Vec<_>>(); let mut builder = @@ -1041,6 +1112,37 @@ fn create_unshredded_object_variant_array(size: usize) -> VariantArray { .expect("created struct should be a valid unshredded variant") } +fn partial_object_shredding_type() -> DataType { + // The source objects have 15 fields (`attr.000`, `attr.020`, ... `attr.280`). Shred the first + // 5 of them, leaving the other 10 to be written to the leftover `value` column. + let shredded_fields = (0..300) + .step_by(20) + .take(5) + .map(|index| { + Arc::new(Field::new( + format!("attr.{index:03}"), + DataType::Int32, + true, + )) + }) + .collect::<Vec<FieldRef>>(); + DataType::Struct(Fields::from(shredded_fields)) +} + +fn create_variant_array_from_json(json: impl IntoIterator<Item = String>) -> VariantArray { + let input: ArrayRef = Arc::new(StringArray::from_iter_values(json)); + json_to_variant(&input).unwrap() +} + +/// Return a JSON object nested `depth` levels deep with two fields per object. +fn json_nested_object(depth: usize) -> String { + if depth == 0 { + return "1".to_string(); + } + let child = json_nested_object(depth - 1); + format!(r#"{{"l{depth}":{child},"r{depth}":{child}}}"#) +} + /// Return an iterator off JSON strings, each representing a person /// with random first name, last name, and age. ///
