Skip to content

Commit f7bb49a

Browse files
committed
perf: Improve parse json performance
1 parent 98670cb commit f7bb49a

10 files changed

Lines changed: 1235 additions & 308 deletions

File tree

Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ rust-version = "1.80"
2727

2828
[dependencies]
2929
byteorder = "1.5.0"
30+
compact_str = "0.9.0"
31+
enum-as-inner = "0.6.1"
3032
ethnum = "1.5.1"
3133
fast-float2 = "0.2.3"
3234
itoa = "1.0"

src/core/databend/ser.rs

Lines changed: 227 additions & 72 deletions
Large diffs are not rendered by default.

src/core/databend/util.rs

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,48 @@ impl OwnedJsonb {
246246
}
247247

248248
impl Number {
249+
#[inline]
250+
pub(crate) fn memory_size(&self) -> usize {
251+
match self {
252+
Self::Int64(v) => {
253+
if *v == 0 {
254+
1
255+
} else if *v >= i8::MIN.into() && *v <= i8::MAX.into() {
256+
2
257+
} else if *v >= i16::MIN.into() && *v <= i16::MAX.into() {
258+
3
259+
} else if *v >= i32::MIN.into() && *v <= i32::MAX.into() {
260+
5
261+
} else {
262+
9
263+
}
264+
}
265+
Self::UInt64(v) => {
266+
if *v == 0 {
267+
1
268+
} else if *v <= u8::MAX.into() {
269+
2
270+
} else if *v <= u16::MAX.into() {
271+
3
272+
} else if *v <= u32::MAX.into() {
273+
5
274+
} else {
275+
9
276+
}
277+
}
278+
Self::Float64(v) => {
279+
if v.is_nan() || v.is_infinite() {
280+
1
281+
} else {
282+
9
283+
}
284+
}
285+
Self::Decimal64(_) => 10,
286+
Self::Decimal128(_) => 18,
287+
Self::Decimal256(_) => 34,
288+
}
289+
}
290+
249291
#[inline]
250292
pub(crate) fn compact_encode<W: Write>(&self, mut writer: W) -> Result<usize> {
251293
match self {

src/error.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,14 @@ pub enum ParseErrorCode {
3535
InvalidLoneLeadingSurrogateInHexEscape(u16),
3636
InvalidSurrogateInHexEscape(u16),
3737
UnexpectedEndOfHexEscape,
38+
ObjectDuplicateKey(String),
3839
}
3940

4041
pub type Result<T> = std::result::Result<T, Error>;
4142

4243
impl Display for ParseErrorCode {
4344
fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
44-
match *self {
45+
match self {
4546
ParseErrorCode::InvalidEOF => f.write_str("EOF while parsing a value"),
4647
ParseErrorCode::InvalidNumberValue => f.write_str("invalid number"),
4748
ParseErrorCode::InvalidStringValue => f.write_str("invalid string"),
@@ -68,6 +69,9 @@ impl Display for ParseErrorCode {
6869
write!(f, "invalid surrogate in hex escape '{:X}'", n)
6970
}
7071
ParseErrorCode::UnexpectedEndOfHexEscape => f.write_str("unexpected end of hex escape"),
72+
ParseErrorCode::ObjectDuplicateKey(key) => {
73+
write!(f, "duplicate object attribute \"{}\"", key)
74+
}
7175
}
7276
}
7377
}

src/lib.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,12 @@ pub use number::Number;
8989
pub use owned::to_owned_jsonb;
9090
pub use owned::OwnedJsonb;
9191
pub use parser::from_slice;
92+
pub use parser::parse_owned_jsonb;
93+
pub use parser::parse_owned_jsonb_standard_mode;
94+
pub use parser::parse_owned_jsonb_standard_mode_with_buf;
95+
pub use parser::parse_owned_jsonb_with_buf;
9296
pub use parser::parse_value;
97+
pub use parser::parse_value_standard_mode;
9398
pub use raw::from_raw_jsonb;
9499
pub use raw::RawJsonb;
95100
pub use value::*;

0 commit comments

Comments
 (0)