| 1 |
//! # Performance |
| 2 |
//! |
| 3 |
//! ## Runtime Performance |
| 4 |
//! |
| 5 |
//! See also the general Rust [Performance Book](https://nnethercote.github.io/perf-book/) |
| 6 |
//! |
| 7 |
//! Tips |
| 8 |
//! - Try `cargo add winnow -F simd`. For some it offers significant performance improvements |
| 9 |
//! - When enough cases of an [`alt`] have unique prefixes, prefer [`dispatch`] |
| 10 |
//! - When parsing text, try to parse as bytes (`u8`) rather than `char`s ([`BStr`] can make |
| 11 |
//! debugging easier) |
| 12 |
//! - Find simplified subsets of the grammar to parse, falling back to the full grammar when it |
| 13 |
//! doesn't work. For example, when parsing json strings, parse them without support for escapes, |
| 14 |
//! falling back to escape support if it fails. |
| 15 |
//! - Watch for large return types. A surprising place these can show up is when chaining parsers |
| 16 |
//! with a tuple. |
| 17 |
//! |
| 18 |
//! ## Build-time Performance |
| 19 |
//! |
| 20 |
//! Returning complex types as `impl Trait` can negatively impact build times. This can hit in |
| 21 |
//! surprising cases like: |
| 22 |
//! ```rust |
| 23 |
//! # use winnow::prelude::*; |
| 24 |
//! fn foo<I, O, E>() -> impl Parser<I, O, E> |
| 25 |
//! # where |
| 26 |
//! # I: winnow::stream::Stream<Token=O>, |
| 27 |
//! # I: winnow::stream::StreamIsPartial, |
| 28 |
//! # E: winnow::error::ParserError<I>, |
| 29 |
//! { |
| 30 |
//! // ...some chained combinators... |
| 31 |
//! # winnow::token::any |
| 32 |
//! } |
| 33 |
//! ``` |
| 34 |
//! |
| 35 |
//! Instead, wrap the combinators in a closure to simplify the type: |
| 36 |
//! ```rust |
| 37 |
//! # use winnow::prelude::*; |
| 38 |
//! fn foo<I, O, E>() -> impl Parser<I, O, E> |
| 39 |
//! # where |
| 40 |
//! # I: winnow::stream::Stream<Token=O>, |
| 41 |
//! # I: winnow::stream::StreamIsPartial, |
| 42 |
//! # E: winnow::error::ParserError<I>, |
| 43 |
//! { |
| 44 |
//! move |input: &mut I| { |
| 45 |
//! // ...some chained combinators... |
| 46 |
//! # winnow::token::any |
| 47 |
//! .parse_next(input) |
| 48 |
//! } |
| 49 |
//! } |
| 50 |
//! ``` |
| 51 |
|
| 52 |
#![allow(unused_imports)] |
| 53 |
use crate::combinator::alt; |
| 54 |
use crate::combinator::dispatch; |
| 55 |
use crate::stream::BStr; |