Skip to content

Commit 3cdafaf

Browse files
abacefalamb
andauthored
[json] Optimize primitive numbers to string (50%-250% faster) (#7819)
# 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 #7273 . # 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. # 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. Added ryu and itoa to convert primitive numbers to strings # 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)? I assume since I am not adding any functionality there are already tests covering this # 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. There should not be Co-authored-by: Andrew Lamb <andrew@nerdnetworks.org>
1 parent 234cc15 commit 3cdafaf

File tree

2 files changed

+12
-4
lines changed

2 files changed

+12
-4
lines changed

arrow-json/Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@ chrono = { workspace = true }
5050
lexical-core = { version = "1.0", default-features = false}
5151
memchr = "2.7.4"
5252
simdutf8 = { workspace = true }
53+
ryu = "1.0"
54+
itoa = "1.0"
5355

5456
[dev-dependencies]
5557
flate2 = { version = "1", default-features = false, features = ["rust_backend"] }

arrow-json/src/reader/string_array.rs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@ use std::marker::PhantomData;
2424
use crate::reader::ArrayDecoder;
2525
use crate::reader::tape::{Tape, TapeElement};
2626

27+
use itoa;
28+
use ryu;
29+
2730
const TRUE: &str = "true";
2831
const FALSE: &str = "false";
2932

@@ -85,6 +88,9 @@ impl<O: OffsetSizeTrait> ArrayDecoder for StringArrayDecoder<O> {
8588

8689
let mut builder = GenericStringBuilder::<O>::with_capacity(pos.len(), data_capacity);
8790

91+
let mut float_formatter = ryu::Buffer::new();
92+
let mut int_formatter = itoa::Buffer::new();
93+
8894
for p in pos {
8995
match tape.get(*p) {
9096
TapeElement::String(idx) => {
@@ -103,20 +109,20 @@ impl<O: OffsetSizeTrait> ArrayDecoder for StringArrayDecoder<O> {
103109
TapeElement::I64(high) if coerce_primitive => match tape.get(p + 1) {
104110
TapeElement::I32(low) => {
105111
let val = ((high as i64) << 32) | (low as u32) as i64;
106-
builder.append_value(val.to_string());
112+
builder.append_value(int_formatter.format(val));
107113
}
108114
_ => unreachable!(),
109115
},
110116
TapeElement::I32(n) if coerce_primitive => {
111-
builder.append_value(n.to_string());
117+
builder.append_value(int_formatter.format(n));
112118
}
113119
TapeElement::F32(n) if coerce_primitive => {
114-
builder.append_value(n.to_string());
120+
builder.append_value(int_formatter.format(n));
115121
}
116122
TapeElement::F64(high) if coerce_primitive => match tape.get(p + 1) {
117123
TapeElement::F32(low) => {
118124
let val = f64::from_bits(((high as u64) << 32) | low as u64);
119-
builder.append_value(val.to_string());
125+
builder.append_value(float_formatter.format_finite(val));
120126
}
121127
_ => unreachable!(),
122128
},

0 commit comments

Comments
 (0)