Skip to content

Commit

Permalink
Merge pull request #644 from epage/perf
Browse files Browse the repository at this point in the history
perf: Further tweak inlining
  • Loading branch information
epage authored Dec 31, 2024
2 parents 172bc9d + 1907d53 commit 7509bbd
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 1 deletion.
2 changes: 2 additions & 0 deletions src/combinator/branch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ pub trait Alt<I, O, E> {
/// # }
/// ```
#[doc(alias = "choice")]
#[inline(always)]
pub fn alt<Input: Stream, Output, Error, Alternatives>(
mut alternatives: Alternatives,
) -> impl Parser<Input, Output, Error>
Expand Down Expand Up @@ -118,6 +119,7 @@ pub trait Permutation<I, O, E> {
/// assert_eq!(parser("ab"), Err(ErrMode::Backtrack(InputError::new("b", ErrorKind::Tag))));
/// ```
///
#[inline(always)]
pub fn permutation<I: Stream, O, E: ParserError<I>, List: Permutation<I, O, E>>(
mut l: List,
) -> impl Parser<I, O, E> {
Expand Down
2 changes: 2 additions & 0 deletions src/combinator/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -542,6 +542,7 @@ enum State<E> {
/// ```
#[doc(alias = "value")]
#[doc(alias = "success")]
#[inline]
pub fn empty<Input, Error>(_input: &mut Input) -> PResult<(), Error>
where
Input: Stream,
Expand All @@ -566,6 +567,7 @@ where
/// assert_eq!(fail::<_, &str, _>.parse_peek(s), Err(ErrMode::Backtrack(InputError::new(s, ErrorKind::Fail))));
/// ```
#[doc(alias = "unexpected")]
#[inline]
pub fn fail<Input, Output, Error>(i: &mut Input) -> PResult<Output, Error>
where
Input: Stream,
Expand Down
1 change: 1 addition & 0 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,7 @@ pub trait ParserError<I: Stream>: Sized {

/// Process a parser assertion
#[cfg_attr(debug_assertions, track_caller)]
#[inline(always)]
fn assert(input: &I, _message: &'static str) -> Self
where
I: crate::lib::std::fmt::Debug,
Expand Down
1 change: 0 additions & 1 deletion src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1062,7 +1062,6 @@ where
E: ParserError<Recoverable<I, R>>,
E: crate::lib::std::fmt::Debug,
{
#[inline]
fn recoverable_parse(&mut self, input: I) -> (I, Option<O>, Vec<R>) {
debug_assert!(
!I::is_partial_supported(),
Expand Down
26 changes: 26 additions & 0 deletions src/stream/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ where
Self { initial, input }
}

#[inline]
fn location(&self) -> usize {
self.input.offset_from(&self.initial)
}
Expand All @@ -138,6 +139,7 @@ where
/// This is useful for formats that encode a graph with addresses relative to the start of the
/// input.
#[doc(alias = "fseek")]
#[inline]
pub fn reset_to_start(&mut self) {
let start = self.initial.checkpoint();
self.input.reset(&start);
Expand Down Expand Up @@ -195,6 +197,7 @@ impl<I, E> Default for Recoverable<I, E>
where
I: Default + Stream,
{
#[inline]
fn default() -> Self {
Self::new(I::default())
}
Expand All @@ -207,6 +210,7 @@ where
I: Stream,
{
/// Track recoverable errors with the stream
#[inline]
pub fn new(input: I) -> Self {
Self {
input,
Expand All @@ -216,6 +220,7 @@ where
}

/// Act as a normal stream
#[inline]
pub fn unrecoverable(input: I) -> Self {
Self {
input,
Expand All @@ -225,6 +230,7 @@ where
}

/// Access the current input and errors
#[inline]
pub fn into_parts(self) -> (I, Vec<E>) {
(self.input, self.errors)
}
Expand Down Expand Up @@ -446,6 +452,7 @@ where
I: StreamIsPartial,
{
/// Create a partial input
#[inline]
pub fn new(input: I) -> Self {
debug_assert!(
!I::is_partial_supported(),
Expand All @@ -466,6 +473,7 @@ impl<I> Default for Partial<I>
where
I: Default + StreamIsPartial,
{
#[inline]
fn default() -> Self {
Self::new(I::default())
}
Expand Down Expand Up @@ -1652,8 +1660,10 @@ pub trait StreamIsPartial: Sized {
impl<T> StreamIsPartial for &[T] {
type PartialState = ();

#[inline]
fn complete(&mut self) -> Self::PartialState {}

#[inline]
fn restore_partial(&mut self, _state: Self::PartialState) {}

#[inline(always)]
Expand All @@ -1665,10 +1675,12 @@ impl<T> StreamIsPartial for &[T] {
impl StreamIsPartial for &str {
type PartialState = ();

#[inline]
fn complete(&mut self) -> Self::PartialState {
// Already complete
}

#[inline]
fn restore_partial(&mut self, _state: Self::PartialState) {}

#[inline(always)]
Expand All @@ -1680,10 +1692,12 @@ impl StreamIsPartial for &str {
impl StreamIsPartial for &Bytes {
type PartialState = ();

#[inline]
fn complete(&mut self) -> Self::PartialState {
// Already complete
}

#[inline]
fn restore_partial(&mut self, _state: Self::PartialState) {}

#[inline(always)]
Expand All @@ -1695,10 +1709,12 @@ impl StreamIsPartial for &Bytes {
impl StreamIsPartial for &BStr {
type PartialState = ();

#[inline]
fn complete(&mut self) -> Self::PartialState {
// Already complete
}

#[inline]
fn restore_partial(&mut self, _state: Self::PartialState) {}

#[inline(always)]
Expand All @@ -1713,10 +1729,12 @@ where
{
type PartialState = I::PartialState;

#[inline]
fn complete(&mut self) -> Self::PartialState {
self.0.complete()
}

#[inline]
fn restore_partial(&mut self, state: Self::PartialState) {
self.0.restore_partial(state);
}
Expand All @@ -1738,10 +1756,12 @@ where
{
type PartialState = I::PartialState;

#[inline]
fn complete(&mut self) -> Self::PartialState {
self.input.complete()
}

#[inline]
fn restore_partial(&mut self, state: Self::PartialState) {
self.input.restore_partial(state);
}
Expand All @@ -1766,10 +1786,12 @@ where
{
type PartialState = I::PartialState;

#[inline]
fn complete(&mut self) -> Self::PartialState {
self.input.complete()
}

#[inline]
fn restore_partial(&mut self, state: Self::PartialState) {
self.input.restore_partial(state);
}
Expand All @@ -1791,10 +1813,12 @@ where
{
type PartialState = I::PartialState;

#[inline]
fn complete(&mut self) -> Self::PartialState {
self.input.complete()
}

#[inline]
fn restore_partial(&mut self, state: Self::PartialState) {
self.input.restore_partial(state);
}
Expand All @@ -1816,10 +1840,12 @@ where
{
type PartialState = bool;

#[inline]
fn complete(&mut self) -> Self::PartialState {
core::mem::replace(&mut self.partial, false)
}

#[inline]
fn restore_partial(&mut self, state: Self::PartialState) {
self.partial = state;
}
Expand Down

0 comments on commit 7509bbd

Please sign in to comment.