Skip to content

Commit 633552a

Browse files
perf: serialize 128-bit integers via itoa
This optimizes the `serialize_i128` and `serialize_u128` implementations by having them use `itoa`, as all of the other integer implementations do. This avoids going through the `Display` implementation of `u128`/`i128` and an intermediate allocation, which is necessary because of how `collect_str` is currently implemented. The current implementation seems to be a remnant of the pre-1.0 `itoa` days, which didn't support `u128` and `i128`. PR #401
1 parent e9f06f4 commit 633552a

File tree

1 file changed

+4
-2
lines changed

1 file changed

+4
-2
lines changed

src/serializer.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,8 @@ impl<'a, 'w, W: io::Write> Serializer for &'a mut SeRecord<'w, W> {
6868

6969
serde_if_integer128! {
7070
fn serialize_i128(self, v: i128) -> Result<Self::Ok, Self::Error> {
71-
self.collect_str(&v)
71+
let mut buffer = itoa::Buffer::new();
72+
self.wtr.write_field(buffer.format(v))
7273
}
7374
}
7475

@@ -94,7 +95,8 @@ impl<'a, 'w, W: io::Write> Serializer for &'a mut SeRecord<'w, W> {
9495

9596
serde_if_integer128! {
9697
fn serialize_u128(self, v: u128) -> Result<Self::Ok, Self::Error> {
97-
self.collect_str(&v)
98+
let mut buffer = itoa::Buffer::new();
99+
self.wtr.write_field(buffer.format(v))
98100
}
99101
}
100102

0 commit comments

Comments
 (0)