alamb commented on code in PR #9091:
URL: https://github.com/apache/arrow-rs/pull/9091#discussion_r2669372780


##########
arrow-json/src/reader/binary_array.rs:
##########
@@ -15,30 +15,94 @@
 // specific language governing permissions and limitations
 // under the License.
 
-use arrow_array::builder::{
-    BinaryViewBuilder, FixedSizeBinaryBuilder, GenericBinaryBuilder, 
GenericStringBuilder,
-};
+use arrow_array::builder::{BinaryViewBuilder, FixedSizeBinaryBuilder, 
GenericBinaryBuilder};
 use arrow_array::{Array, GenericStringArray, OffsetSizeTrait};
 use arrow_data::ArrayData;
 use arrow_schema::ArrowError;
+use std::io::Write;
 use std::marker::PhantomData;
 
 use crate::reader::ArrayDecoder;
 use crate::reader::tape::{Tape, TapeElement};
 
-/// Decode a hex-encoded string into bytes
-fn decode_hex_string(hex_string: &str) -> Result<Vec<u8>, ArrowError> {
-    let mut decoded = Vec::with_capacity(hex_string.len() / 2);
-    for substr in hex_string.as_bytes().chunks(2) {
-        let str = std::str::from_utf8(substr).map_err(|e| {
-            ArrowError::JsonError(format!("invalid utf8 in hex encoded binary 
data: {e}"))
-        })?;
-        let byte = u8::from_str_radix(str, 16).map_err(|e| {
-            ArrowError::JsonError(format!("invalid hex encoding in binary 
data: {e}"))
-        })?;
-        decoded.push(byte);
-    }
-    Ok(decoded)
+#[inline]
+fn decode_hex_digit(byte: u8) -> Option<u8> {
+    match byte {
+        b'0'..=b'9' => Some(byte - b'0'),
+        b'a'..=b'f' => Some(byte - b'a' + 10),
+        b'A'..=b'F' => Some(byte - b'A' + 10),
+        _ => None,
+    }
+}
+
+fn invalid_hex_error_at(index: usize, byte: u8) -> ArrowError {
+    ArrowError::JsonError(format!(
+        "invalid hex encoding in binary data: invalid digit 0x{byte:02x} at 
position {index}"
+    ))
+}
+
+fn decode_hex_to_vec(hex_string: &str, out: &mut Vec<u8>) -> Result<(), 
ArrowError> {

Review Comment:
   since Vec implements writer, you shouldn't need this copy -- 
decode_hex_to_writer should be also work for Vec



##########
arrow-json/src/reader/binary_array.rs:
##########
@@ -91,13 +155,15 @@ impl FixedSizeBinaryArrayDecoder {
 impl ArrayDecoder for FixedSizeBinaryArrayDecoder {
     fn decode(&mut self, tape: &Tape<'_>, pos: &[u32]) -> Result<ArrayData, 
ArrowError> {
         let mut builder = FixedSizeBinaryBuilder::with_capacity(pos.len(), 
self.len);
+        let mut scratch = Vec::with_capacity(self.len as usize);

Review Comment:
   why self.len? Isn't that the length of the input? Also the scratch used 
below is different (no initial capacity)



##########
arrow-json/benches/reader.rs:
##########
@@ -0,0 +1,107 @@
+// 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_json::ReaderBuilder;
+use arrow_schema::{DataType, Field};
+use criterion::{Criterion, criterion_group, criterion_main};
+use std::hint::black_box;
+use std::sync::Arc;
+
+const BINARY_ROWS: usize = 1 << 15;
+const BINARY_BYTES: usize = 64;
+
+fn bench_decode(c: &mut Criterion, name: &str, data: &[u8], field: Arc<Field>, 
rows: usize) {
+    c.bench_function(name, |b| {
+        b.iter(|| {
+            let mut decoder = ReaderBuilder::new_with_field(field.clone())
+                .with_batch_size(rows)
+                .build_decoder()
+                .unwrap();
+
+            let mut offset = 0;
+            while offset < data.len() {
+                let read = decoder.decode(black_box(&data[offset..])).unwrap();
+                if read == 0 {
+                    break;
+                }
+                offset += read;
+            }
+
+            let batch = decoder.flush().unwrap();
+            black_box(batch);
+        })
+    });
+}
+
+#[inline]

Review Comment:
   this is good -- can you please make a separate PR with just the benchmarks 
(ideally with some additional comments that explain what it is measuring)? That 
way it will be easier for me to compare the performance



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