jecsand838 commented on code in PR #8025:
URL: https://github.com/apache/arrow-rs/pull/8025#discussion_r2257647972


##########
arrow-avro/benches/decoder.rs:
##########
@@ -0,0 +1,436 @@
+// 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 `arrow‑avro` **Decoder**
+//!
+
+extern crate apache_avro;
+extern crate arrow_avro;
+extern crate criterion;
+extern crate num_bigint;
+extern crate once_cell;
+extern crate uuid;
+
+use apache_avro::types::Value;
+use apache_avro::{to_avro_datum, Decimal, Schema as ApacheSchema};
+use arrow_avro::{reader::ReaderBuilder, schema::Schema as AvroSchema};
+use criterion::{criterion_group, criterion_main, BatchSize, BenchmarkId, 
Criterion, Throughput};
+use once_cell::sync::Lazy;
+use std::{hint::black_box, io, time::Duration};
+use uuid::Uuid;
+
+fn encode_records(schema: &ApacheSchema, rows: impl Iterator<Item = Value>) -> 
Vec<u8> {
+    let mut out = Vec::new();
+    for v in rows {
+        out.extend_from_slice(&to_avro_datum(schema, v).expect("encode datum 
failed"));
+    }
+    out
+}
+
+fn gen_int(sc: &ApacheSchema, n: usize) -> Vec<u8> {
+    encode_records(
+        sc,
+        (0..n).map(|i| Value::Record(vec![("field1".into(), Value::Int(i as 
i32))])),
+    )
+}
+
+fn gen_long(sc: &ApacheSchema, n: usize) -> Vec<u8> {
+    encode_records(
+        sc,
+        (0..n).map(|i| Value::Record(vec![("field1".into(), Value::Long(i as 
i64))])),
+    )
+}
+
+fn gen_float(sc: &ApacheSchema, n: usize) -> Vec<u8> {
+    encode_records(
+        sc,
+        (0..n).map(|i| Value::Record(vec![("field1".into(), Value::Float(i as 
f32 + 0.5678))])),
+    )
+}
+
+fn gen_bool(sc: &ApacheSchema, n: usize) -> Vec<u8> {
+    encode_records(
+        sc,
+        (0..n).map(|i| Value::Record(vec![("field1".into(), Value::Boolean(i % 
2 == 0))])),
+    )
+}
+
+fn gen_double(sc: &ApacheSchema, n: usize) -> Vec<u8> {
+    encode_records(
+        sc,
+        (0..n).map(|i| Value::Record(vec![("field1".into(), Value::Double(i as 
f64 + 0.1234))])),
+    )
+}
+
+fn gen_bytes(sc: &ApacheSchema, n: usize) -> Vec<u8> {
+    encode_records(
+        sc,
+        (0..n).map(|i| {
+            let payload = vec![(i & 0xFF) as u8; 16];
+            Value::Record(vec![("field1".into(), Value::Bytes(payload))])
+        }),
+    )
+}
+
+fn gen_string(sc: &ApacheSchema, n: usize) -> Vec<u8> {
+    encode_records(
+        sc,
+        (0..n).map(|i| {
+            let s = if i % 3 == 0 {
+                format!("value-{i}")
+            } else {
+                "abcdefghij".into()
+            };
+            Value::Record(vec![("field1".into(), Value::String(s))])
+        }),
+    )
+}
+
+fn gen_date(sc: &ApacheSchema, n: usize) -> Vec<u8> {
+    encode_records(
+        sc,
+        (0..n).map(|i| Value::Record(vec![("field1".into(), Value::Int(i as 
i32))])),
+    )
+}
+
+fn gen_timemillis(sc: &ApacheSchema, n: usize) -> Vec<u8> {
+    encode_records(
+        sc,
+        (0..n).map(|i| Value::Record(vec![("field1".into(), Value::Int((i * 
37) as i32))])),
+    )
+}
+
+fn gen_timemicros(sc: &ApacheSchema, n: usize) -> Vec<u8> {
+    encode_records(
+        sc,
+        (0..n).map(|i| Value::Record(vec![("field1".into(), Value::Long((i * 
1_001) as i64))])),
+    )
+}
+
+fn gen_ts_millis(sc: &ApacheSchema, n: usize) -> Vec<u8> {
+    encode_records(
+        sc,
+        (0..n).map(|i| {
+            Value::Record(vec![(
+                "field1".into(),
+                Value::Long(1_600_000_000_000 + i as i64),
+            )])
+        }),
+    )
+}
+
+fn gen_ts_micros(sc: &ApacheSchema, n: usize) -> Vec<u8> {
+    encode_records(
+        sc,
+        (0..n).map(|i| {
+            Value::Record(vec![(
+                "field1".into(),
+                Value::Long(1_600_000_000_000_000 + i as i64),
+            )])
+        }),
+    )
+}
+
+fn gen_map(sc: &ApacheSchema, n: usize) -> Vec<u8> {
+    use std::collections::HashMap;
+    encode_records(
+        sc,
+        (0..n).map(|i| {
+            let mut m = HashMap::new();
+            let int_val = |v: i32| Value::Union(0, Box::new(Value::Int(v)));
+            m.insert("key1".into(), int_val(i as i32));
+            let key2_val = if i % 5 == 0 {
+                Value::Union(1, Box::new(Value::Null))
+            } else {
+                int_val(i as i32 + 1)
+            };
+            m.insert("key2".into(), key2_val);
+            m.insert("key3".into(), int_val(42));
+            Value::Record(vec![("field1".into(), Value::Map(m))])
+        }),
+    )
+}
+
+fn gen_array(sc: &ApacheSchema, n: usize) -> Vec<u8> {
+    encode_records(
+        sc,
+        (0..n).map(|i| {
+            let items = (0..5).map(|j| Value::Int(i as i32 + j)).collect();
+            Value::Record(vec![("field1".into(), Value::Array(items))])
+        }),
+    )
+}
+
+fn trim_i128_be(v: i128) -> Vec<u8> {
+    let full = v.to_be_bytes();
+    let first = full
+        .iter()
+        .enumerate()
+        .take_while(|(i, b)| {
+            *i < 15
+                && ((**b == 0x00 && full[i + 1] & 0x80 == 0)
+                    || (**b == 0xFF && full[i + 1] & 0x80 != 0))
+        })
+        .count();
+    full[first..].to_vec()
+}
+
+fn gen_decimal(sc: &ApacheSchema, n: usize) -> Vec<u8> {
+    encode_records(
+        sc,
+        (0..n).map(|i| {
+            let unscaled = if i % 2 == 0 { i as i128 } else { -(i as i128) };
+            Value::Record(vec![(
+                "field1".into(),
+                Value::Decimal(Decimal::from(trim_i128_be(unscaled))),
+            )])
+        }),
+    )
+}
+
+fn gen_uuid(sc: &ApacheSchema, n: usize) -> Vec<u8> {
+    encode_records(
+        sc,
+        (0..n).map(|i| {
+            let mut raw = (i as u128).to_be_bytes();
+            raw[6] = (raw[6] & 0x0F) | 0x40;
+            raw[8] = (raw[8] & 0x3F) | 0x80;
+            Value::Record(vec![("field1".into(), 
Value::Uuid(Uuid::from_bytes(raw)))])
+        }),
+    )
+}
+
+fn gen_fixed(sc: &ApacheSchema, n: usize) -> Vec<u8> {
+    encode_records(
+        sc,
+        (0..n).map(|i| {
+            let mut buf = vec![0u8; 16];
+            buf[..8].copy_from_slice(&(i as u64).to_be_bytes());
+            Value::Record(vec![("field1".into(), Value::Fixed(16, buf))])
+        }),
+    )
+}
+
+fn gen_interval(sc: &ApacheSchema, n: usize) -> Vec<u8> {
+    encode_records(
+        sc,
+        (0..n).map(|i| {
+            let months = (i % 24) as u32;
+            let days = (i % 32) as u32;
+            let millis = (i * 10) as u32;
+            let mut buf = Vec::with_capacity(12);
+            buf.extend_from_slice(&months.to_le_bytes());
+            buf.extend_from_slice(&days.to_le_bytes());
+            buf.extend_from_slice(&millis.to_le_bytes());
+            Value::Record(vec![("field1".into(), Value::Fixed(12, buf))])
+        }),
+    )
+}
+
+fn gen_enum(sc: &ApacheSchema, n: usize) -> Vec<u8> {
+    const SYMBOLS: [&str; 3] = ["A", "B", "C"];
+    encode_records(
+        sc,
+        (0..n).map(|i| {
+            let idx = i % 3;
+            Value::Record(vec![(
+                "field1".into(),
+                Value::Enum(idx as u32, SYMBOLS[idx].into()),
+            )])
+        }),
+    )
+}
+
+fn gen_mixed(sc: &ApacheSchema, n: usize) -> Vec<u8> {
+    encode_records(
+        sc,
+        (0..n).map(|i| {
+            Value::Record(vec![
+                ("f1".into(), Value::Int(i as i32)),
+                ("f2".into(), Value::Long(i as i64)),
+                ("f3".into(), Value::String(format!("name-{i}"))),
+                ("f4".into(), Value::Double(i as f64 * 1.5)),
+            ])
+        }),
+    )
+}
+
+fn gen_nested(sc: &ApacheSchema, n: usize) -> Vec<u8> {
+    encode_records(
+        sc,
+        (0..n).map(|i| {
+            let sub = Value::Record(vec![
+                ("x".into(), Value::Int(i as i32)),
+                ("y".into(), Value::String("constant".into())),
+            ]);
+            Value::Record(vec![("sub".into(), sub)])
+        }),
+    )
+}
+
+const DEFAULT_BATCH: usize = 65_536;
+
+fn new_decoder(
+    schema_json: &'static str,
+    batch_size: usize,
+    utf8view: bool,
+) -> arrow_avro::reader::Decoder {
+    let schema: AvroSchema<'static> = 
serde_json::from_str(schema_json).unwrap();
+    ReaderBuilder::new()
+        .with_schema(schema)
+        .with_batch_size(batch_size)
+        .with_utf8_view(utf8view)
+        .build_decoder(io::empty())

Review Comment:
   That's about to be removed as `.build_decoder` doesn't need a `Reader` once 
https://github.com/apache/arrow-rs/pull/8006 is finalized and merged in.



-- 
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]

Reply via email to