This is an automated email from the ASF dual-hosted git repository.
Jefffrey pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/arrow-rs.git
The following commit(s) were added to refs/heads/main by this push:
new f88697844f Cache encoded field name in FieldEncoder (#10296)
f88697844f is described below
commit f88697844fe38ccecee297f505894289b46e090f
Author: Michal Piatkowski <[email protected]>
AuthorDate: Fri Jul 17 08:39:33 2026 +0200
Cache encoded field name in FieldEncoder (#10296)
# Which issue does this PR close?
<!--
We generally require a GitHub issue to be filed for all bug fixes and
enhancements and this helps us generate change logs for our releases.
You can link an issue to this PR using the GitHub syntax.
-->
- Closes #10295.
# Rationale for this change
<!--
Why are you proposing this change? If this is already explained clearly
in the issue then this section is not needed.
Explaining clearly why changes are proposed helps reviewers understand
your changes and offer better suggestions for fixes.
-->
See issue.
# What changes are included in this PR?
<!--
There is no need to duplicate the description in the issue here but it
is sometimes worth providing a summary of the individual changes in this
PR.
-->
See issue.
# Are these changes tested?
<!--
We typically require tests for all PRs in order to:
1. Prevent the code from being accidentally broken by subsequent changes
2. Serve as another way to document the expected behavior of the code
If tests are not included in your PR, please explain why (for example,
are they covered by existing tests)?
If this PR claims a performance improvement, please include evidence
such as benchmark results.
-->
Should be covered by existing tests.
# Are there any user-facing changes?
<!--
If there are user-facing changes then we may require documentation to be
updated before approving the PR.
If there are any breaking changes to public APIs, please call them out.
-->
No.
---
arrow-json/src/writer/encoder.rs | 15 ++++++++++-----
arrow/benches/json_writer.rs | 39 +++++++++++++++++++++++++++++++++------
2 files changed, 43 insertions(+), 11 deletions(-)
diff --git a/arrow-json/src/writer/encoder.rs b/arrow-json/src/writer/encoder.rs
index 45055c5a36..ae0db947e4 100644
--- a/arrow-json/src/writer/encoder.rs
+++ b/arrow-json/src/writer/encoder.rs
@@ -412,8 +412,14 @@ pub fn make_encoder<'a>(
let array = array.as_struct();
let encoders = fields.iter().zip(array.columns()).map(|(field,
array)| {
let encoder = make_encoder(field, array, options)?;
- Ok(FieldEncoder{
- field: field.clone(),
+
+ // For typical ASCII names, this will be the exact length
(includes 2x quotes, 1x colon).
+ let mut field_name = Vec::with_capacity(field.name().len() +
3);
+ encode_string(field.name(), &mut field_name);
+ field_name.push(b':');
+
+ Ok(FieldEncoder {
+ field_name,
encoder,
})
}).collect::<Result<Vec<_>, ArrowError>>()?;
@@ -471,7 +477,7 @@ fn encode_binary(bytes: &[u8], out: &mut Vec<u8>) {
}
struct FieldEncoder<'a> {
- field: FieldRef,
+ field_name: Vec<u8>,
encoder: NullableEncoder<'a>,
}
@@ -509,8 +515,7 @@ impl Encoder for StructArrayEncoder<'_> {
is_first = false;
if self.struct_mode == StructMode::ObjectOnly {
- encode_string(field_encoder.field.name(), out);
- out.push(b':');
+ out.extend_from_slice(&field_encoder.field_name);
}
if is_null {
diff --git a/arrow/benches/json_writer.rs b/arrow/benches/json_writer.rs
index c636c076ec..adcee16488 100644
--- a/arrow/benches/json_writer.rs
+++ b/arrow/benches/json_writer.rs
@@ -31,6 +31,7 @@ use serde::Serialize;
use std::sync::Arc;
const NUM_ROWS: usize = 65536;
+const STR_LEN: usize = 20;
fn do_bench(c: &mut Criterion, name: &str, batch: &RecordBatch) {
c.bench_function(name, |b| {
@@ -42,12 +43,12 @@ fn do_bench(c: &mut Criterion, name: &str, batch:
&RecordBatch) {
});
}
-fn create_mixed(len: usize) -> RecordBatch {
+fn create_mixed(len: usize, str_len: usize) -> RecordBatch {
let c1 = Arc::new(create_string_array::<i32>(len, 0.));
let c2 = Arc::new(create_primitive_array::<Int32Type>(len, 0.));
let c3 = Arc::new(create_primitive_array::<UInt32Type>(len, 0.));
- let c4 = Arc::new(create_string_array_with_len::<i32>(len, 0.2, 10));
- let c5 = Arc::new(create_string_array_with_len::<i32>(len, 0.2, 20));
+ let c4 = Arc::new(create_string_array_with_len::<i32>(len, 0.2, str_len /
2));
+ let c5 = Arc::new(create_string_array_with_len::<i32>(len, 0.2, str_len));
let c6 = Arc::new(create_primitive_array::<Float32Type>(len, 0.2));
RecordBatch::try_from_iter([
("c1", c1 as _),
@@ -82,7 +83,7 @@ fn create_offsets(len: usize) -> (usize, OffsetBuffer<i32>) {
}
fn create_nullable_struct(len: usize) -> StructArray {
- let c2 = StructArray::from(create_mixed(len));
+ let c2 = StructArray::from(create_mixed(len, STR_LEN));
StructArray::new(
c2.fields().clone(),
c2.columns().to_vec(),
@@ -111,10 +112,34 @@ fn bench_integer(c: &mut Criterion) {
}
fn bench_mixed(c: &mut Criterion) {
- let batch = create_mixed(NUM_ROWS);
+ let batch = create_mixed(NUM_ROWS, STR_LEN);
do_bench(c, "bench_mixed", &batch)
}
+fn create_mixed_longname(len: usize, str_len: usize) -> RecordBatch {
+ let batch = create_mixed(len, str_len);
+ // Skip the first 400-len string column.
+ let columns = &batch.columns()[1..];
+
+ let names: Vec<String> = columns
+ .iter()
+ .enumerate()
+ .map(|(idx, c)| format!("{:?}_column_{}", c.data_type(), idx))
+ .collect();
+
RecordBatch::try_from_iter(names.iter().zip(columns.iter().map(Arc::clone))).unwrap()
+}
+
+fn bench_mixed_longname(c: &mut Criterion) {
+ let batch = create_mixed_longname(NUM_ROWS, STR_LEN);
+ do_bench(c, "bench_mixed_longname", &batch);
+}
+
+fn bench_shortmixed_longname(c: &mut Criterion) {
+ // Reduce the influence of slow-to-encode string columns.
+ let batch = create_mixed_longname(NUM_ROWS, STR_LEN / 5);
+ do_bench(c, "bench_shortmixed_longname", &batch);
+}
+
fn bench_dict_array(c: &mut Criterion) {
let c1 = Arc::new(create_string_dict_array::<Int32Type>(NUM_ROWS, 0., 30));
let c2 = Arc::new(create_string_dict_array::<Int32Type>(NUM_ROWS, 0., 20));
@@ -139,7 +164,7 @@ fn bench_string(c: &mut Criterion) {
fn bench_struct(c: &mut Criterion) {
let c1 = Arc::new(create_string_array::<i32>(NUM_ROWS, 0.));
- let c2 = Arc::new(StructArray::from(create_mixed(NUM_ROWS)));
+ let c2 = Arc::new(StructArray::from(create_mixed(NUM_ROWS, STR_LEN)));
let batch = RecordBatch::try_from_iter([("c1", c1 as _), ("c2", c2 as
_)]).unwrap();
do_bench(c, "bench_struct", &batch)
@@ -319,6 +344,8 @@ fn criterion_benchmark(c: &mut Criterion) {
bench_float(c);
bench_string(c);
bench_mixed(c);
+ bench_mixed_longname(c);
+ bench_shortmixed_longname(c);
bench_dict_array(c);
bench_struct(c);
bench_nullable_struct(c);