Skip to content

Commit

Permalink
Speed up integer decoding
Browse files Browse the repository at this point in the history
Checking for `is_safe_integer` guarantees that the value is already a number, and then using `unchecked_into_f64` allows to avoid extra memory access cost of `Option<f64>`.

This speeds up CitmCatalog parsing benchmark by 15%.
  • Loading branch information
RReverser committed Dec 9, 2023
1 parent 0e38f22 commit 3dfe727
Showing 1 changed file with 2 additions and 4 deletions.
6 changes: 2 additions & 4 deletions src/de.rs
Original file line number Diff line number Diff line change
Expand Up @@ -245,10 +245,8 @@ impl Deserializer {
}

fn as_safe_integer(&self) -> Option<i64> {
if let Some(v) = self.value.as_f64() {
if Number::is_safe_integer(&self.value) {
return Some(v as i64);
}
if Number::is_safe_integer(&self.value) {
return Some(self.value.unchecked_into_f64() as i64);
}
None
}
Expand Down

0 comments on commit 3dfe727

Please sign in to comment.