Using exceptions to return from deeply nested recursions?
App Development2 posts211 views2 likesLast activity Jun 2021
LE
Leonid_RozenbergOP
Jun 2021Now that exceptions are in the language, is there any performance benefit to using them to escape from a long call stack?
In other languages this is a technique that is used to unwind all of the accumulated stack frames in one operation.
CO
cocreature
Jun 2021I assume you’re comparing it against an Either/EitherT style short-circuiting?
I do expect exception throwing to outperform that. However, I think it’s important to clarify a few points/caveats:
- The two are only equivalent if you do not have any writes to the ledger (clearly a given if we talk about code outside of
Update. Otherwise the exception will roll them back whereasEitherwill not roll them back. - You can only catch in
Update. I’d think twice about moving things intoUpdatejust to get a performance boost. - You make it potentially harder to understand your code by making the errors not visible in the type.
- How much it matters really depends on your code. If you have a tight loop which is basically doing nothing more than
>>=onEither, you can probably gain something. However, most code does something interesting between binds and then the performance advantage can easily become insignificant.