-
Notifications
You must be signed in to change notification settings - Fork 839
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Faster Serde Integration (~80% faster) #4861
Changes from 3 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
// 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, Schema}; | ||
use criterion::*; | ||
use rand::{thread_rng, Rng}; | ||
use serde::Serialize; | ||
use std::sync::Arc; | ||
|
||
#[allow(deprecated)] | ||
fn do_bench<R: Serialize>(c: &mut Criterion, name: &str, rows: &[R], schema: &Schema) { | ||
let schema = Arc::new(schema.clone()); | ||
c.bench_function(name, |b| { | ||
b.iter(|| { | ||
let builder = ReaderBuilder::new(schema.clone()).with_batch_size(64); | ||
let mut decoder = builder.build_decoder().unwrap(); | ||
decoder.serialize(&rows) | ||
}) | ||
}); | ||
} | ||
|
||
fn criterion_benchmark(c: &mut Criterion) { | ||
let mut rng = thread_rng(); | ||
let schema = Schema::new(vec![Field::new("i32", DataType::Int32, false)]); | ||
let v: Vec<i32> = (0..2048).map(|_| rng.gen_range(0..10000)).collect(); | ||
|
||
do_bench(c, "small_i32", &v, &schema); | ||
let v: Vec<i32> = (0..2048).map(|_| rng.gen()).collect(); | ||
do_bench(c, "large_i32", &v, &schema); | ||
|
||
let schema = Schema::new(vec![Field::new("i64", DataType::Int64, false)]); | ||
let v: Vec<i64> = (0..2048).map(|_| rng.gen_range(0..10000)).collect(); | ||
do_bench(c, "small_i64", &v, &schema); | ||
let v: Vec<i64> = (0..2048).map(|_| rng.gen_range(0..i32::MAX as _)).collect(); | ||
do_bench(c, "medium_i64", &v, &schema); | ||
let v: Vec<i64> = (0..2048).map(|_| rng.gen()).collect(); | ||
do_bench(c, "large_i64", &v, &schema); | ||
|
||
let schema = Schema::new(vec![Field::new("f32", DataType::Float32, false)]); | ||
let v: Vec<f32> = (0..2048).map(|_| rng.gen_range(0.0..10000.)).collect(); | ||
do_bench(c, "small_f32", &v, &schema); | ||
let v: Vec<f32> = (0..2048).map(|_| rng.gen_range(0.0..f32::MAX)).collect(); | ||
do_bench(c, "large_f32", &v, &schema); | ||
} | ||
|
||
criterion_group!(benches, criterion_benchmark); | ||
criterion_main!(benches); |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -77,22 +77,6 @@ impl<'a> TapeSerializer<'a> { | |
} | ||
} | ||
|
||
/// The tape stores all values as strings, and so must serialize numeric types | ||
/// | ||
/// Formatting to a string only to parse it back again is rather wasteful, | ||
/// it may be possible to tweak the tape representation to avoid this | ||
/// | ||
/// Need to use macro as const generic expressions are unstable | ||
/// <https://github.com/rust-lang/rust/issues/76560> | ||
macro_rules! serialize_numeric { | ||
($s:ident, $t:ty, $v:ident) => {{ | ||
let mut buffer = [0_u8; <$t>::FORMATTED_SIZE]; | ||
let s = lexical_core::write($v, &mut buffer); | ||
$s.serialize_number(s); | ||
Ok(()) | ||
}}; | ||
} | ||
|
||
impl<'a, 'b> Serializer for &'a mut TapeSerializer<'b> { | ||
type Ok = (); | ||
|
||
|
@@ -115,43 +99,63 @@ impl<'a, 'b> Serializer for &'a mut TapeSerializer<'b> { | |
} | ||
|
||
fn serialize_i8(self, v: i8) -> Result<(), SerializerError> { | ||
serialize_numeric!(self, i8, v) | ||
self.serialize_i32(v as _) | ||
} | ||
|
||
fn serialize_i16(self, v: i16) -> Result<(), SerializerError> { | ||
serialize_numeric!(self, i16, v) | ||
self.serialize_i32(v as _) | ||
} | ||
|
||
fn serialize_i32(self, v: i32) -> Result<(), SerializerError> { | ||
serialize_numeric!(self, i32, v) | ||
self.elements.push(TapeElement::I32(v)); | ||
Ok(()) | ||
} | ||
|
||
fn serialize_i64(self, v: i64) -> Result<(), SerializerError> { | ||
serialize_numeric!(self, i64, v) | ||
let low = v as i32; | ||
let high = (v >> 32) as i32; | ||
self.elements.push(TapeElement::I64(high)); | ||
self.elements.push(TapeElement::I32(low)); | ||
Ok(()) | ||
} | ||
|
||
fn serialize_u8(self, v: u8) -> Result<(), SerializerError> { | ||
serialize_numeric!(self, u8, v) | ||
self.serialize_i32(v as _) | ||
} | ||
|
||
fn serialize_u16(self, v: u16) -> Result<(), SerializerError> { | ||
serialize_numeric!(self, u16, v) | ||
self.serialize_i32(v as _) | ||
} | ||
|
||
fn serialize_u32(self, v: u32) -> Result<(), SerializerError> { | ||
serialize_numeric!(self, u32, v) | ||
match i32::try_from(v) { | ||
Ok(v) => self.serialize_i32(v), | ||
Err(_) => self.serialize_i64(v as _), | ||
} | ||
} | ||
|
||
fn serialize_u64(self, v: u64) -> Result<(), SerializerError> { | ||
serialize_numeric!(self, u64, v) | ||
match i64::try_from(v) { | ||
Ok(v) => self.serialize_i64(v), | ||
Err(_) => { | ||
let mut buffer = [0_u8; u64::FORMATTED_SIZE]; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The additional complexity to support rountripping values There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Would you imagine doing it via There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Possibly, or as a u64 variant. Given JSON only reliably roundtrips f64, I don't think this is a very common use-case worth optimising for |
||
let s = lexical_core::write(v, &mut buffer); | ||
self.serialize_number(s); | ||
Ok(()) | ||
} | ||
} | ||
} | ||
|
||
fn serialize_f32(self, v: f32) -> Result<(), SerializerError> { | ||
serialize_numeric!(self, f32, v) | ||
self.elements.push(TapeElement::F32(v.to_bits())); | ||
Ok(()) | ||
} | ||
|
||
fn serialize_f64(self, v: f64) -> Result<(), SerializerError> { | ||
serialize_numeric!(self, f64, v) | ||
let bits = v.to_bits(); | ||
self.elements.push(TapeElement::F64((bits >> 32) as u32)); | ||
self.elements.push(TapeElement::F32(bits as u32)); | ||
Ok(()) | ||
} | ||
|
||
fn serialize_char(self, v: char) -> Result<(), SerializerError> { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,6 +18,7 @@ | |
use crate::reader::serializer::TapeSerializer; | ||
use arrow_schema::ArrowError; | ||
use serde::Serialize; | ||
use std::fmt::Write; | ||
|
||
/// We decode JSON to a flattened tape representation, | ||
/// allowing for efficient traversal of the JSON data | ||
|
@@ -54,6 +55,25 @@ pub enum TapeElement { | |
/// | ||
/// Contains the offset into the [`Tape`] string data | ||
Number(u32), | ||
|
||
/// The high bits of a i64 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 |
||
/// | ||
/// Followed by [`Self::I32`] containing the low bits | ||
I64(i32), | ||
|
||
/// A 32-bit signed integer | ||
/// | ||
/// May be preceded by [`Self::I64`] containing high bits | ||
I32(i32), | ||
|
||
/// The high bits of a 64-bit float | ||
/// | ||
/// Followed by [`Self::F32`] containing the low bits | ||
F64(u32), | ||
|
||
/// A 32-bit float or the low-bits of a 64-bit float if preceded by [`Self::F64`] | ||
F32(u32), | ||
|
||
/// A true literal | ||
True, | ||
/// A false literal | ||
|
@@ -104,10 +124,15 @@ impl<'a> Tape<'a> { | |
| TapeElement::Number(_) | ||
| TapeElement::True | ||
| TapeElement::False | ||
| TapeElement::Null => Ok(cur_idx + 1), | ||
| TapeElement::Null | ||
| TapeElement::I32(_) | ||
| TapeElement::F32(_) => Ok(cur_idx + 1), | ||
TapeElement::I64(_) | TapeElement::F64(_) => Ok(cur_idx + 2), | ||
TapeElement::StartList(end_idx) => Ok(end_idx + 1), | ||
TapeElement::StartObject(end_idx) => Ok(end_idx + 1), | ||
_ => Err(self.error(cur_idx, expected)), | ||
TapeElement::EndObject(_) | TapeElement::EndList(_) => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. +1 for removing the catch all |
||
Err(self.error(cur_idx, expected)) | ||
} | ||
} | ||
} | ||
|
||
|
@@ -153,6 +178,28 @@ impl<'a> Tape<'a> { | |
TapeElement::True => out.push_str("true"), | ||
TapeElement::False => out.push_str("false"), | ||
TapeElement::Null => out.push_str("null"), | ||
TapeElement::I64(high) => match self.get(idx + 1) { | ||
TapeElement::I32(low) => { | ||
let val = (high as i64) << 32 | low as i64; | ||
let _ = write!(out, "{val}"); | ||
return idx + 2; | ||
} | ||
_ => unreachable!(), | ||
}, | ||
TapeElement::I32(val) => { | ||
let _ = write!(out, "{val}"); | ||
} | ||
TapeElement::F64(high) => match self.get(idx + 1) { | ||
TapeElement::F32(low) => { | ||
let val = f64::from_bits((high as u64) << 32 | low as u64); | ||
let _ = write!(out, "{val}"); | ||
return idx + 2; | ||
} | ||
_ => unreachable!(), | ||
}, | ||
TapeElement::F32(val) => { | ||
let _ = write!(out, "{}", f32::from_bits(val)); | ||
} | ||
} | ||
idx + 1 | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🎉