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

#![allow(unused_imports)]
use crate::combinator::alt;
use crate::combinator::dispatch;
use crate::stream::BStr;