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

Avoid costly errors in JS witness generation #830

Merged
merged 6 commits into from
Oct 3, 2023

Conversation

mitschabaude
Copy link
Contributor

@mitschabaude mitschabaude commented Aug 24, 2023

Instead of throwing JS errors, which are costly because they collect a full stack trace, this changes some core parts of snarky to throw strings instead. Throwing strings is very fast, and this change leads to a huge speedup during witrness generation, see o1-labs/o1js#1092

Comment on lines 73 to 77
let of_basic = function T x -> x | _ -> failwith "different constructor"
let of_basic = function
| T x ->
x
| _ ->
raise (Failure "different constructor")
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'm just replacing failwith s with raise (Failure s). This makes a difference because in JS, we use a custom override of the failwith function, to have it throw JS errors instead of strings. This is very important in a couple of circumstances where the error implies an actual failure condition. Here, the error is just used for control flow and always caught a level earlier, so we don't need a pretty error.

Copy link
Member

Choose a reason for hiding this comment

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

If you don't want to pay the cost of the exception here, can you change the code above it to use an val of_basic : ('v, 'f) basic -> ('v, 'f) t option instead.

Comment on lines 73 to 77
let of_basic = function T x -> x | _ -> failwith "different constructor"
let of_basic = function
| T x ->
x
| _ ->
raise (Failure "different constructor")
Copy link
Member

Choose a reason for hiding this comment

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

If you don't want to pay the cost of the exception here, can you change the code above it to use an val of_basic : ('v, 'f) basic -> ('v, 'f) t option instead.

failwith
( "Unhandled request: "
^ Core_kernel.String.concat ~sep:"\n" label_stack )
raise
Copy link
Member

Choose a reason for hiding this comment

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

As above, this should probably return an option, and the caller should handle the error by constructing an appropriate error message instead. This also has the benefit of never constructing this string if it isn't needed.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

good point! will change

As_prover.Provider.run p (Run_state.stack s) (get_value s)
(Run_state.handler s)
As_prover.Provider.run p (get_value s) (Run_state.handler s)
|> Option.value_exn
Copy link
Member

Choose a reason for hiding this comment

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

If you do this in a match statement, then we'll only compute the error message when there's actually an error.

@mitschabaude mitschabaude merged commit 94b2df8 into master Oct 3, 2023
5 of 6 checks passed
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