From 7b3624cc1d514c604ad38a9798169c069c84f2c4 Mon Sep 17 00:00:00 2001 From: David Himmelstrup Date: Wed, 14 Sep 2022 16:15:13 +0200 Subject: [PATCH] Improve performance when writing a single digit. --- integer/src/fmt/digit_writer.rs | 9 +++++++++ integer/src/fmt/power_two.rs | 3 ++- 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/integer/src/fmt/digit_writer.rs b/integer/src/fmt/digit_writer.rs index 94ef495..d3a403c 100644 --- a/integer/src/fmt/digit_writer.rs +++ b/integer/src/fmt/digit_writer.rs @@ -41,6 +41,15 @@ impl<'a> DigitWriter<'a> { Ok(()) } + pub fn write_digit(&mut self, digit: u8) -> fmt::Result { + self.buffer[self.buffer_len] = digit; + self.buffer_len += 1; + if self.buffer_len == BUFFER_LEN { + self.flush()?; + } + Ok(()) + } + /// Must call flush to make sure all the data is written. pub fn flush(&mut self) -> fmt::Result { let buffer_len_rounded = math::round_up(self.buffer_len, arch::digits::DIGIT_CHUNK_LEN); diff --git a/integer/src/fmt/power_two.rs b/integer/src/fmt/power_two.rs index cd40f78..e89172f 100644 --- a/integer/src/fmt/power_two.rs +++ b/integer/src/fmt/power_two.rs @@ -168,7 +168,8 @@ impl PreparedForFormatting for PreparedLarge<'_> { bits -= self.log_radix; digit = ((word >> bits) & mask) as u8; } - digit_writer.write(&[digit])?; + // digit_writer.write(&[digit])?; + digit_writer.write_digit(digit)?; } debug_assert_eq!(bits, 0); Ok(())