Show file tree
Hide file tree
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improve ParseError::from_error_kind() performance
According to the flamegraph produced by the following command: cat large-yarn.nix | cargo flamegraph -p nix-parser --example viewer The most siginificant amount of time and resources being spent parsing this very large Nix expression is actually in the error handling code, specifically in `<Errors as ParseError>::from_error_kind()` and `<Errors as ParseError>::append()`, mostly in the conversion of the input fragment into a heap-allocated string for `Error::Nom`. The specific call dominating the flamegraph was `core::fmt::write::hbe8b9e0d6296c582`. Since we are already preserving the span of the input fragment, there technically is no need to preserve the source text itself since we can retrieve it later in `Error::to_diagnostic()`. Removing the `String` payload from the `Error::Nom` variant and removing all occurrences of `.into()` and `.to_string()` where `Error::Nom` occurs nets a massive performance win on my MacBook Pro, shown by the Criterion benchmark below: ``` parse example.nix time: [722.02 us 724.88 us 729.24 us] change: [-53.820% -51.759% -48.149%] (p = 0.00 < 0.05) Performance has improved. Found 10 outliers among 100 measurements (10.00%) 3 (3.00%) high mild 7 (7.00%) high severe ``` This is a reduction from an average time of 1.5558 ms on `cargo bench`. Additionally, the time spent parsing the `large-yarn.nix` file above was reduced drastically as well: ``` $ cat large-yarn.nix | time cargo run -p nix-parser --example viewer # Old 1667.33 real 1262.42 user 10.53 sys $ cat large-yarn.nix | time cargo run -p nix-parser --example viewer # New 0.87 real 0.52 user 0.05 sys ```
- Loading branch information
Showing 2 changed files with 6 additions and 19 deletions.