Skip to content

Commit

Permalink
Merge pull request #142 from rakaly/itoa
Browse files Browse the repository at this point in the history
Introduce faster_writer feature (based on itoa)
  • Loading branch information
nickbabcock authored Dec 16, 2023
2 parents bc8dc27 + 70b6b8f commit 1d6dfc9
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 5 deletions.
4 changes: 3 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,13 @@ members = ["jomini_derive"]
serde = { version = "1", optional = true }
serde_json = { version = "1", optional = true }
jomini_derive = { path = "jomini_derive", version = "^0.2.3", optional = true }
itoa = { version = "1.0", optional = true }

[features]
default = ["derive"]
default = ["derive", "faster_writer"]
derive = ["serde/derive", "jomini_derive"]
json = ["serde", "serde_json"]
faster_writer = ["dep:itoa"]

[dev-dependencies]
attohttpc = { version = "0.24", features = ["tls-vendored"] }
Expand Down
42 changes: 38 additions & 4 deletions src/text/writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,16 +75,28 @@ const WRITE_STATE_NEXT: [WriteState; 7] = [
WriteState::KeyValueSeparator,
];

#[cfg(feature = "faster_writer")]
macro_rules! write_num {
($self:ident, $data:expr) => { $self.write_unquoted(itoa::Buffer::new().format($data).as_bytes()) }
}

#[cfg(not(feature = "faster_writer"))]
macro_rules! write_num {
($self:ident, $data:expr) => { write!($self, "{}", $data) }
}

impl<W> TextWriter<W>
where
W: Write,
{
/// Get inner writer, keeping ownership
#[inline]
pub fn inner(&mut self) -> &mut W {
&mut self.writer
}

/// Consumes this Writer, returning the underlying writer
#[inline]
pub fn into_inner(self) -> W {
self.writer
}
Expand All @@ -102,6 +114,7 @@ where
}

/// Write out the start of an object
#[inline]
pub fn write_object_start(&mut self) -> Result<(), Error> {
self.write_preamble()?;
self.writer.write_all(b"{")?;
Expand All @@ -113,6 +126,7 @@ where
}

/// Write out the start of an array
#[inline]
pub fn write_array_start(&mut self) -> Result<(), Error> {
self.write_preamble()?;
self.writer.write_all(b"{")?;
Expand All @@ -124,6 +138,7 @@ where
}

/// Write the end of an array or object
#[inline]
pub fn write_end(&mut self) -> Result<(), Error> {
let old_state = self.state;
if let Some(mode) = self.depth.pop() {
Expand Down Expand Up @@ -164,6 +179,7 @@ where
/// # Ok(())
/// # }
/// ```
#[inline]
pub fn write_bool(&mut self, data: bool) -> Result<(), Error> {
match data {
true => write!(self, "yes"),
Expand All @@ -189,6 +205,7 @@ where
/// # Ok(())
/// # }
/// ```
#[inline]
pub fn write_operator(&mut self, data: Operator) -> Result<(), Error> {
if self.mixed_mode == MixedMode::Disabled {
if data == Operator::Equal {
Expand Down Expand Up @@ -223,6 +240,7 @@ where
/// # Ok(())
/// # }
/// ```
#[inline]
pub fn write_unquoted(&mut self, data: &[u8]) -> Result<(), Error> {
self.write_preamble()?;
self.writer.write_all(data)?;
Expand All @@ -249,6 +267,7 @@ where
/// # Ok(())
/// # }
/// ```
#[inline]
pub fn write_quoted(&mut self, data: &[u8]) -> Result<(), Error> {
self.write_preamble()?;
let esc_buf = self.scratch.split_off(0);
Expand All @@ -274,8 +293,9 @@ where
/// # Ok(())
/// # }
/// ```
#[inline]
pub fn write_i32(&mut self, data: i32) -> Result<(), Error> {
write!(self, "{}", data)
write_num!(self, data)
}

/// Write an unsigned 32bit integer.
Expand All @@ -291,8 +311,9 @@ where
/// # Ok(())
/// # }
/// ```
#[inline]
pub fn write_u32(&mut self, data: u32) -> Result<(), Error> {
write!(self, "{}", data)
write_num!(self, data)
}

/// Write an unsigned 64bit integer.
Expand All @@ -308,8 +329,9 @@ where
/// # Ok(())
/// # }
/// ```
#[inline]
pub fn write_u64(&mut self, data: u64) -> Result<(), Error> {
write!(self, "{}", data)
write_num!(self, data)
}

/// Write a signed 64bit integer.
Expand All @@ -325,8 +347,9 @@ where
/// # Ok(())
/// # }
/// ```
#[inline]
pub fn write_i64(&mut self, data: i64) -> Result<(), Error> {
write!(self, "{}", data)
write_num!(self, data)
}

/// Write a 32 bit floating point at full precision
Expand All @@ -342,6 +365,7 @@ where
/// # Ok(())
/// # }
/// ```
#[inline]
pub fn write_f32(&mut self, data: f32) -> Result<(), Error> {
write!(self, "{}", data)
}
Expand All @@ -359,6 +383,7 @@ where
/// # Ok(())
/// # }
/// ```
#[inline]
pub fn write_f32_precision(&mut self, data: f32, precision: usize) -> Result<(), Error> {
write!(self, "{0:.1$}", data, precision)
}
Expand All @@ -376,6 +401,7 @@ where
/// # Ok(())
/// # }
/// ```
#[inline]
pub fn write_f64(&mut self, data: f64) -> Result<(), Error> {
write!(self, "{}", data)
}
Expand All @@ -393,6 +419,7 @@ where
/// # Ok(())
/// # }
/// ```
#[inline]
pub fn write_f64_precision(&mut self, data: f64, precision: usize) -> Result<(), Error> {
write!(self, "{0:.1$}", data, precision)
}
Expand All @@ -419,6 +446,7 @@ where
/// # Ok(())
/// # }
/// ```
#[inline]
pub fn write_header(&mut self, header: &[u8]) -> Result<(), Error> {
self.write_preamble()?;
self.writer.write_all(header)?;
Expand All @@ -441,6 +469,7 @@ where
/// # Ok(())
/// # }
/// ```
#[inline]
pub fn write_date(&mut self, data: PdsDateFormatter) -> Result<(), Error> {
write!(self, "{}", data)
}
Expand Down Expand Up @@ -490,13 +519,15 @@ where
/// # Ok(())
/// # }
/// ```
#[inline]
pub fn write_fmt(&mut self, fmt: Arguments) -> Result<(), Error> {
self.write_preamble()?;
self.writer.write_fmt(fmt)?;
self.write_epilogue()?;
Ok(())
}

#[inline]
fn write_line_terminator(&mut self) -> Result<(), Error> {
if self.needs_line_terminator {
self.writer.write_all(b"\n")?;
Expand All @@ -506,6 +537,7 @@ where
Ok(())
}

#[inline]
fn write_preamble(&mut self) -> Result<(), Error> {
let just_wrote_line_terminator = self.needs_line_terminator;
self.write_line_terminator()?;
Expand Down Expand Up @@ -542,6 +574,7 @@ where
}

/// Write the indent characters
#[inline]
fn write_indent(&mut self) -> Result<(), Error> {
for _ in 0..self.depth.len() * usize::from(self.indent_factor) {
self.writer.write_all(&[self.indent_char])?;
Expand All @@ -551,6 +584,7 @@ where
}

/// Enter mixed mode for writing a container that is a list and an object
#[inline]
pub fn start_mixed_mode(&mut self) {
self.mode = DepthMode::Array;
self.mixed_mode = MixedMode::Started;
Expand Down

0 comments on commit 1d6dfc9

Please sign in to comment.