Skip to content

Commit b1edc7d

Browse files
committed
Optimize position search in error path
Translating index into a line/column pair takes considerable time. Notably, the JSON benchmark modified to run on malformed data spends around 50% of the CPU time generating the error object. While it is generally assumed that the cold path is quite slow, such a drastic pessimization may be unexpected, especially when a faster implementation exists. Using vectorized routines provided by the memchr crate increases performance of the failure path by 2x on average. Old implementation: DOM STRUCT data/canada.json 122 MB/s 168 MB/s data/citm_catalog.json 135 MB/s 195 MB/s data/twitter.json 142 MB/s 226 MB/s New implementation: DOM STRUCT data/canada.json 216 MB/s 376 MB/s data/citm_catalog.json 238 MB/s 736 MB/s data/twitter.json 210 MB/s 492 MB/s In comparison, the performance of the happy path is: DOM STRUCT data/canada.json 283 MB/s 416 MB/s data/citm_catalog.json 429 MB/s 864 MB/s data/twitter.json 275 MB/s 541 MB/s While this introduces a new dependency, memchr is much faster to compile than serde, so compile time does not increase significantly. Additionally, memchr provides a more efficient SWAR-based implementation of both the memchr and count routines even without std, providing benefits for embedded uses as well.
1 parent 40dd7f5 commit b1edc7d

File tree

2 files changed

+9
-13
lines changed

2 files changed

+9
-13
lines changed

Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ rust-version = "1.56"
1414
[dependencies]
1515
indexmap = { version = "2.2.3", optional = true }
1616
itoa = "1.0"
17+
memchr = { version = "2", default-features = false }
1718
ryu = "1.0"
1819
serde = { version = "1.0.194", default-features = false }
1920

@@ -45,7 +46,7 @@ features = ["raw_value"]
4546
[features]
4647
default = ["std"]
4748

48-
std = ["serde/std"]
49+
std = ["memchr/std", "serde/std"]
4950

5051
# Provide integration for heap-allocated collections without depending on the
5152
# rest of the Rust standard library.

src/read.rs

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -415,19 +415,14 @@ impl<'a> SliceRead<'a> {
415415
}
416416

417417
fn position_of_index(&self, i: usize) -> Position {
418-
let mut position = Position { line: 1, column: 0 };
419-
for ch in &self.slice[..i] {
420-
match *ch {
421-
b'\n' => {
422-
position.line += 1;
423-
position.column = 0;
424-
}
425-
_ => {
426-
position.column += 1;
427-
}
428-
}
418+
let start_of_line = match memchr::memrchr(b'\n', &self.slice[..i]) {
419+
Some(position) => position + 1,
420+
None => 0,
421+
};
422+
Position {
423+
line: 1 + memchr::memchr_iter(b'\n', &self.slice[..start_of_line]).count(),
424+
column: i - start_of_line,
429425
}
430-
position
431426
}
432427

433428
/// The big optimization here over IoRead is that if the string contains no

0 commit comments

Comments
 (0)