News for hackers. Stuff that matters.
Re:Hurl, the Exceptional Language by tylerhou on Sunday May 26, 2024 @11:20AM (#40482825)
Exceptions are less expensive in the unexceptional case. Consuming a Result value requires a branch to see whether it holds Error. That is not necessary if the function returns a value directly (and throws an exception on error).
Re:Hurl, the Exceptional Language by vacuity on Sunday May 26, 2024 @08:11PM (#40486468)
However, exceptions require a larger runtime and can make optimizations harder to reason about.
Re:Hurl, the Exceptional Language by zerodensity on Monday May 27, 2024 @05:41AM (#40489214)
I have heard this before but never really understood it.

Take this:

```

int bar(int arg); // May throw

int foo(int arg) {

    int b = bar(arg);

    return b * 5; 
}

```

VS:

```

Error<int> bar(int arg); // Uses error type

Error<int> foo(int arg) {

  auto b = bar(arg);

  if(b.ok()) { // Happy Path

    return b.unwrap() * 5;
  }

  return e; // Exceptional Path
}

```

In the happy case (no exception) how can the first version be harder to optimize than the second one? In the exceptional case exceptions will probably be slower. You trade fast common case for slow exceptional case so it makes sense to me. Is the slower one the one that is hard to optimize? Is this what we are talking about when optimizations are harder to reason about? Or are there some other things that become harder because of exception? Things like RAII, defer, goto stuff?

Re:Hurl, the Exceptional Language by vacuity on Monday May 27, 2024 @09:46AM (#40490656)
Admittedly, I'm not an expert. I believe it's due to the extra control flow points (assume all functions can throw, not just specially marked ones) and not having just a local value, which procludes analyzing the operations done on the error path.
Re:Hurl, the Exceptional Language by Kamq on Monday May 27, 2024 @04:48PM (#40494541)
> In the happy case (no exception) how can the first version be harder to optimize than the second one?

If your runtime has to do extra work to support stack-unwinding, it could easily be slower. You're doing work in the happy path in that case just in case the sad path happens. My guess is adding function metadata to some sort of datastructure (linked list?) so that it can figure out whats happening when it long jumps to the handler. I'd bet that allocation has at least one conditional in it.

The alternative would be putting the metadata on the stack, letting the function complete normally, and then check a flag to see which path you're on (same overhead).

To be clear, I have no idea how various runtimes implement this, just that the extra magic could easily have the same, or more, overhead than a conditional check.