Skip to content

Commit

Permalink
Add #[inline] to methods implementing XmlSource
Browse files Browse the repository at this point in the history
Related: tafia#678

All methods called only once or two and inlining them in most cases increases performance
of our benchmarks:

> critcmp master element-parser -t 5
group                                                              element-parser                         master
-----                                                              --------------                         ------
NsReader::read_resolved_event_into/trim_text = false               1.00    398.9±6.30µs        ? ?/sec    1.05    419.6±7.94µs        ? ?/sec
NsReader::read_resolved_event_into/trim_text = true                1.00    382.1±7.06µs        ? ?/sec    1.06    404.0±7.44µs        ? ?/sec
One event/CData                                                    1.00     56.3±0.97ns        ? ?/sec    1.21     68.1±1.35ns        ? ?/sec
One event/Comment                                                  1.00    141.2±2.52ns        ? ?/sec    1.14    161.4±2.79ns        ? ?/sec
decode_and_parse_document_with_namespaces/rpm_filelists.xml        1.00     95.1±1.45µs   115.5 MB/sec    1.07    102.2±1.65µs   107.5 MB/sec
escape_text/escaped_chars_long                                     1.42  1806.4±34.20ns        ? ?/sec    1.00  1275.0±23.98ns        ? ?/sec
escape_text/escaped_chars_short                                    1.00    491.5±8.35ns        ? ?/sec    1.07   526.6±10.80ns        ? ?/sec
escape_text/no_chars_to_escape_long                                2.06  1831.1±36.31ns        ? ?/sec    1.00   887.1±17.00ns        ? ?/sec
parse_document_nocopy_with_namespaces/libreoffice_document.fodt    1.00    507.2±8.56µs   107.6 MB/sec    1.08   546.2±10.20µs   100.0 MB/sec
parse_document_nocopy_with_namespaces/rpm_filelists.xml            1.00     87.2±1.64µs   126.0 MB/sec    1.14     99.2±1.74µs   110.7 MB/sec
parse_document_nocopy_with_namespaces/rpm_other.xml                1.00    139.6±2.83µs   158.5 MB/sec    1.07    148.7±2.71µs   148.9 MB/sec
parse_document_nocopy_with_namespaces/rpm_primary.xml              1.00    190.5±3.43µs   106.4 MB/sec    1.09    207.9±3.79µs    97.5 MB/sec
parse_document_nocopy_with_namespaces/rpm_primary2.xml             1.00     61.7±1.10µs   116.2 MB/sec    1.09     67.5±1.28µs   106.2 MB/sec
parse_document_nocopy_with_namespaces/sample_1.xml                 1.00     10.5±0.20µs   105.0 MB/sec    1.06     11.1±0.21µs    99.3 MB/sec
parse_document_nocopy_with_namespaces/sample_ns.xml                1.00      8.4±0.16µs    86.5 MB/sec    1.08      9.0±0.18µs    80.0 MB/sec
parse_document_nocopy_with_namespaces/sample_rss.xml               1.00   786.4±13.46µs   239.8 MB/sec    1.09   859.9±12.82µs   219.3 MB/sec
parse_document_nocopy_with_namespaces/test_writer_ident.xml        1.00     29.0±0.55µs   146.4 MB/sec    1.06     30.8±0.55µs   138.0 MB/sec
read_event/trim_text = false                                       1.00    199.3±3.59µs        ? ?/sec    1.10    218.5±3.98µs        ? ?/sec
read_event/trim_text = true                                        1.00    190.4±3.76µs        ? ?/sec    1.11    211.7±4.11µs        ? ?/sec
unescape_text/no_chars_to_unescape_short                           1.00     11.8±0.21ns        ? ?/sec    1.06     12.4±0.23ns        ? ?/sec
  • Loading branch information
Mingun committed Jun 9, 2024
1 parent 31c9359 commit 86bc3cd
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/reader/buffered_reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ use crate::reader::{is_whitespace, BangType, Parser, Reader, Span, XmlSource};
macro_rules! impl_buffered_source {
($($lf:lifetime, $reader:tt, $async:ident, $await:ident)?) => {
#[cfg(not(feature = "encoding"))]
#[inline]
$($async)? fn remove_utf8_bom(&mut self) -> Result<()> {
use crate::encoding::UTF8_BOM;

Expand All @@ -31,6 +32,7 @@ macro_rules! impl_buffered_source {
}

#[cfg(feature = "encoding")]
#[inline]
$($async)? fn detect_encoding(&mut self) -> Result<Option<&'static encoding_rs::Encoding>> {
loop {
break match self $(.$reader)? .fill_buf() $(.$await)? {
Expand Down Expand Up @@ -91,6 +93,7 @@ macro_rules! impl_buffered_source {
Ok((&buf[start..], done))
}

#[inline]
$($async)? fn read_with<$($lf,)? P: Parser>(
&mut self,
mut parser: P,
Expand Down Expand Up @@ -133,6 +136,7 @@ macro_rules! impl_buffered_source {
Err(Error::Syntax(P::eof_error()))
}

#[inline]
$($async)? fn read_bang_element $(<$lf>)? (
&mut self,
buf: &'b mut Vec<u8>,
Expand Down Expand Up @@ -183,6 +187,7 @@ macro_rules! impl_buffered_source {
Err(bang_type.to_err())
}

#[inline]
$($async)? fn skip_whitespace(&mut self, position: &mut usize) -> Result<()> {
loop {
break match self $(.$reader)? .fill_buf() $(.$await)? {
Expand All @@ -202,6 +207,7 @@ macro_rules! impl_buffered_source {
}
}

#[inline]
$($async)? fn skip_one(&mut self, byte: u8) -> Result<bool> {
// search byte must be within the ascii range
debug_assert!(byte.is_ascii());
Expand All @@ -215,6 +221,7 @@ macro_rules! impl_buffered_source {
}
}

#[inline]
$($async)? fn peek_one(&mut self) -> Result<Option<u8>> {
loop {
break match self $(.$reader)? .fill_buf() $(.$await)? {
Expand Down
8 changes: 8 additions & 0 deletions src/reader/slice_reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,7 @@ impl<'a> Reader<&'a [u8]> {
/// that will be borrowed by events. This implementation provides a zero-copy deserialization
impl<'a> XmlSource<'a, ()> for &'a [u8] {
#[cfg(not(feature = "encoding"))]
#[inline]
fn remove_utf8_bom(&mut self) -> Result<()> {
if self.starts_with(crate::encoding::UTF8_BOM) {
*self = &self[crate::encoding::UTF8_BOM.len()..];
Expand All @@ -245,6 +246,7 @@ impl<'a> XmlSource<'a, ()> for &'a [u8] {
}

#[cfg(feature = "encoding")]
#[inline]
fn detect_encoding(&mut self) -> Result<Option<&'static Encoding>> {
if let Some((enc, bom_len)) = crate::encoding::detect_encoding(self) {
*self = &self[bom_len..];
Expand All @@ -253,6 +255,7 @@ impl<'a> XmlSource<'a, ()> for &'a [u8] {
Ok(None)
}

#[inline]
fn read_bytes_until(
&mut self,
byte: u8,
Expand All @@ -275,6 +278,7 @@ impl<'a> XmlSource<'a, ()> for &'a [u8] {
}
}

#[inline]
fn read_with<P>(&mut self, mut parser: P, _buf: (), position: &mut usize) -> Result<&'a [u8]>
where
P: Parser,
Expand All @@ -291,6 +295,7 @@ impl<'a> XmlSource<'a, ()> for &'a [u8] {
Err(Error::Syntax(P::eof_error()))
}

#[inline]
fn read_bang_element(
&mut self,
_buf: (),
Expand All @@ -312,6 +317,7 @@ impl<'a> XmlSource<'a, ()> for &'a [u8] {
Err(bang_type.to_err())
}

#[inline]
fn skip_whitespace(&mut self, position: &mut usize) -> Result<()> {
let whitespaces = self
.iter()
Expand All @@ -322,6 +328,7 @@ impl<'a> XmlSource<'a, ()> for &'a [u8] {
Ok(())
}

#[inline]
fn skip_one(&mut self, byte: u8) -> Result<bool> {
// search byte must be within the ascii range
debug_assert!(byte.is_ascii());
Expand All @@ -333,6 +340,7 @@ impl<'a> XmlSource<'a, ()> for &'a [u8] {
}
}

#[inline]
fn peek_one(&mut self) -> Result<Option<u8>> {
Ok(self.first().copied())
}
Expand Down

0 comments on commit 86bc3cd

Please sign in to comment.