Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(parser): Parser error optimisation #1292

Merged
merged 8 commits into from
May 9, 2023
Merged

Conversation

joss-aztec
Copy link
Contributor

@joss-aztec joss-aztec commented May 4, 2023

Related issue(s)

Resolves #

Description

Summary of changes

tl;dr
~40% speed up in release mode
~10% speed up in debug mode

Chumsky produces and aggregates large numbers of errors in normal operation regardless of whether it's on the happy path. This is because as it tries each grammar rule in turn, it produces an error until the matching rule is encountered. Currently our error construction and aggregation is rather expensive, largely in due to the construction and destruction of BTreeSets that may hold zero or very few items.

This PR uses SmallOrdSet to avoid heap allocations as long as possible.

Other changes:

  • labels switched to an enum to avoid string allocations

Samples taken using time nargo print-acir

debug release
master 4.1s 0.47s
this branch 3.7s 0.34s

Dependency additions / changes

Test additions / changes

Checklist

  • I have tested the changes locally.
  • I have formatted the changes with Prettier and/or cargo fmt with default settings.
  • I have linked this PR to the issue(s) that it resolves.
  • I have reviewed the changes on GitHub, line by line.
  • I have ensured all changes are covered in the description.

Documentation needs

  • This PR requires documentation updates when merged.

Additional context

Copy link
Contributor

@jfecher jfecher left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks pretty good, I appreciate the timing information as well. Have you tested against existing small set crates? There aren't many but I wonder it one of those fits our usecase any better.

crates/noirc_frontend/src/parser/errors/late_alloc_set.rs Outdated Show resolved Hide resolved
@joss-aztec
Copy link
Contributor Author

Just noticed these changes throw up the following clippy warning:

warning: the `Err`-variant returned from this function is very large
   --> crates/noirc_frontend/src/ast/mod.rs:154:10
    |
154 |     ) -> Result<UnresolvedTypeExpression, ParserError> {
    |          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the `Err`-variant is at least 344 bytes
    |
    = help: try reducing the size of `parser::errors::ParserError`, for example by boxing large elements or replacing it with `Box<parser::errors::ParserError>`
    = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#result_large_err
    = note: `#[warn(clippy::result_large_err)]` on by default

Guess I better benchmark Resolver::resolve_expression to check I haven't inadvertently hurt it. If not, then I guess it's justifiable to mute the warning.

I'm aware that I've spent more time on this PR than it's really worth - so shout if you think I should abandon.

@jfecher
Copy link
Contributor

jfecher commented May 5, 2023

I think this PR is still a worthwhile pursuit, its nice to have the timing information so that we know for later what any performance characteristics are like. I'm interested in what the performance of tinyset would be as well? Is it an easy slot-in replacement for small-ord-set?

@joss-aztec
Copy link
Contributor Author

I had a go at tinyset, but it's pretty cumbersome to integrate with our current enums. You need to implement the following trait on your element type:

pub trait Fits64: Copy {
    /// Convert back *from* a u64.  This is unsafe, since it is only
    /// infallible (and lossless) if the `u64` originally came from
    /// type `Self`.
    unsafe fn from_u64(x: u64) -> Self;
    /// Convert to a `u64`.  This should be infallible.
    fn to_u64(self) -> u64;
}

Probably the biggest hurdle is you'd need to map to some alternative representation of a token that's able to support the Copy trait. (Maybe an argument to split data from ids as we have done for ssa?) I also had trouble with unsafe related errors, but that could be my rust inexperience rather than a real blocker.

jfecher
jfecher previously approved these changes May 9, 2023
Copy link
Contributor

@jfecher jfecher left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good. I have one final question but I don't want to hold this PR any longer.

@joss-aztec joss-aztec enabled auto-merge May 9, 2023 17:20
@joss-aztec joss-aztec added this pull request to the merge queue May 9, 2023
Merged via the queue into master with commit e123aa7 May 9, 2023
@joss-aztec joss-aztec deleted the joss/optim-parser-err branch May 9, 2023 18:11
TomAFrench added a commit that referenced this pull request May 12, 2023
* phated/acvm-0.12.0: (45 commits)
  chore!: Update to ACVM 0.12.0
  official release of backend
  feat: use dummy constructor for bb call
  chore: add missing `?`
  chore: use `try_vecmap` in old `vecmap` locations
  chore: update `acvm-backend-barretenberg` to 0.1.0 commit
  latest master
  fix: improve variable resolution
  test: re enabled sort test
  chore: update cargo tomls
  feat: adapted to heterogeneous bb calls
  remove unneeded import
  fix grep problems
  chore: replace long `Backend` type parameters with `B`
  update to latest commit
  chore: Make CliError generic over a Backend
  chore: Update nargo core to return backend errors
  chore!: Update to acvm 0.11.0
  chore(parser): Parser error optimisation (#1292)
  chore(ssa refactor): Implement function inlining (#1293)
  ...
TomAFrench added a commit that referenced this pull request May 17, 2023
* master: (66 commits)
  feat(nargo)!: retire print-acir in favour of flag (#1328)
  chore(ssa): enable cse for assert (#1350)
  chore(ssa refactor): Add basic instruction simplification (#1329)
  chore(noir): Release 0.6.0 (#1279)
  feat: enable to_radix for any field element (#1343)
  chore(ssa refactor): Simplify inlining pass and fix inlining failure (#1337)
  chore!: Update to acvm 0.11.0 (#1322)
  feat: Add ECDSA secp256k1 builtin test (#1294)
  chore: add support for encoding/decoding inputs from JSON (#1325)
  feat: Issue an error when attempting to use a `return` expression (#1330)
  chore(ssa refactor): Fix inlining bug (#1335)
  fix: to-bits and to-radix for > 128 bits (#1312)
  chore(parser): Parser error optimisation (#1292)
  chore(ssa refactor): Implement function inlining (#1293)
  chore: fix installation link in readme (#1326)
  chore: fix installation link in readme (#1326)
  feat(stdlib): Add keccak (#1249)
  fix: Parsing nested generics (#1319)
  chore(ssa refactor): Document some SSA-gen functions (#1321)
  fix: Assigning to tuple fields (#1318)
  ...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants