Using exceptions to return from deeply nested recursions?

Now 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.

1 Like

I 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:

  1. 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 whereas Either will not roll them back.
  2. You can only catch in Update. I’d think twice about moving things into Update just to get a performance boost.
  3. You make it potentially harder to understand your code by making the errors not visible in the type.
  4. How much it matters really depends on your code. If you have a tight loop which is basically doing nothing more than >>= on Either, you can probably gain something. However, most code does something interesting between binds and then the performance advantage can easily become insignificant.
1 Like