Skip to content
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

Merged
merged 4 commits into from
Sep 26, 2023

Conversation

tustvold
Copy link
Contributor

@tustvold tustvold commented Sep 26, 2023

Which issue does this PR close?

Closes #.

Rationale for this change

Encoding numerics directly in the tape drastically improves the performance of the serde integration.

small_i32               time:   [5.3992 µs 5.4006 µs 5.4020 µs]
                        change: [-70.553% -70.532% -70.511%] (p = 0.00 < 0.05)
                        Performance has improved.

large_i32               time:   [5.2606 µs 5.2618 µs 5.2631 µs]
                        change: [-76.768% -76.747% -76.727%] (p = 0.00 < 0.05)
                        Performance has improved.
Found 2 outliers among 100 measurements (2.00%)
  2 (2.00%) high mild

small_i64               time:   [5.2937 µs 5.2960 µs 5.2986 µs]
                        change: [-73.032% -73.002% -72.974%] (p = 0.00 < 0.05)
                        Performance has improved.

medium_i64              time:   [5.3314 µs 5.3372 µs 5.3417 µs]
                        change: [-77.574% -77.553% -77.533%] (p = 0.00 < 0.05)
                        Performance has improved.
Found 2 outliers among 100 measurements (2.00%)
  1 (1.00%) low severe
  1 (1.00%) low mild

large_i64               time:   [5.6473 µs 5.6503 µs 5.6532 µs]
                        change: [-81.152% -81.103% -81.056%] (p = 0.00 < 0.05)
                        Performance has improved.
Found 22 outliers among 100 measurements (22.00%)
  22 (22.00%) low severe

small_f32               time:   [3.6082 µs 3.6101 µs 3.6121 µs]
                        change: [-93.595% -93.591% -93.588%] (p = 0.00 < 0.05)
                        Performance has improved.

large_f32               time:   [3.5233 µs 3.5245 µs 3.5256 µs]
                        change: [-94.058% -94.055% -94.053%] (p = 0.00 < 0.05)
                        Performance has improved.

It additionally opens the door to eager parsing in the future, which may yield performance improvements for regular JSON decoding.

I have confirmed this does not regress the performance of the JSON decoder

What changes are included in this PR?

Are there any user-facing changes?

@github-actions github-actions bot added the arrow Changes to the arrow crate label Sep 26, 2023
match i64::try_from(v) {
Ok(v) => self.serialize_i64(v),
Err(_) => {
let mut buffer = [0_u8; u64::FORMATTED_SIZE];
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The additional complexity to support rountripping values u64::MAX > v > i64::MAX seemed not worth it, so we just fallback to serializing to a string

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would you imagine doing it via i128 or something?

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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

Copy link
Contributor

@alamb alamb left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me -- thank you @tustvold

/// 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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🎉

match i64::try_from(v) {
Ok(v) => self.serialize_i64(v),
Err(_) => {
let mut buffer = [0_u8; u64::FORMATTED_SIZE];
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would you imagine doing it via i128 or something?

@@ -54,6 +55,25 @@ pub enum TapeElement {
///
/// Contains the offset into the [`Tape`] string data
Number(u32),

/// The high bits of a i64
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

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(_) => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1 for removing the catch all

@tustvold tustvold merged commit fbd9008 into apache:master Sep 26, 2023
23 checks passed
ryanaston pushed a commit to segmentio/arrow-rs that referenced this pull request Nov 6, 2023
* Store decoded numerics in JSON tape

* Add arrow-json serde benchmarks

* Fix timestamp serialize

* Clippy
@tustvold tustvold mentioned this pull request Feb 1, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
arrow Changes to the arrow crate
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants