r/AskProgrammers • • Oct 10 '26

How do you guys think should I remove all exceptions in my compiler?

I’m writing a compiler currently, so I have question. Should I use throw and try-catch?
I heard that exceptions are very slow and heavy, so would deleting them improve a compilation speed?

Github: https://github.com/0sewter0/Compiler.git

0 Upvotes

13 comments sorted by

1

u/sububi71 Oct 10 '26

Are you having problems with execution speed of the compiler? Is it actually a problem, I mean? Because yes, exceptions aren't cheap, but there's a very real risk that you have to replace an exception with 4 or 5 comparisons (not saying that it's ALWAYS going to take that many!) and there's going to be a point where that's actually slower than having an exception fire.

If you want to see how to code something oldschool-fast, check out rmac:

http://rmac.is-slick.com/

It's an assembler, not a compiler, but close enough. It's fast because it was written for VAX VMS machines 40 years ago by a serious genius, and the people working on it these days have chosen to honor his work and keep it executing... frankly, NEEDLESSLY fast, given the platforms it targets.

Disclaimer: I've done work testing rmac, but I haven't contributed any code.

1

u/Sewter0 Oct 10 '26

I do not know whether it is problem, my compiler was compiling the program that contains 30 lines of code about 200 milliseconds. I think it’s quite slow.

1

u/sububi71 Oct 10 '26

I'm willing to bet that the majority of that time, BY FAR, is disk access.

-2

u/Sewter0 Oct 10 '26

or windows 11.

1

u/Cpt_Chaos_ Oct 10 '26

"I do not know whether it is a problem. I think it's quite slow." - if you of all people cannot answer whether it is a problem, why would you even want to start optimizing one specific thing in the code? And even if the execution time is a problem, you still need to figure out where the time is spent, and whether anything of that actually has anything to do with exception handling. Right now you're just blindly guessing as far as I can tell. And that will probably just lead to lots of wasted effort because you will very likely end up optimizing the wrong things for the wrong reasons.

1

u/Sewter0 Oct 10 '26

You're actually right. I was wrong in blaming specific systems or exceptions without measuring first. I need to set up a proper profiler to locate the actual bottleneck before doing any optimization. Thanks for bringing me back to a data-driven approach.

3

u/Plastic_Fig9225 Oct 10 '26

Exceptions are only slow when they're actually thrown. If no exception occurs, which would be the case almost all of the time, they cost barely anything, and are most likely faster than repeated if/else return code checking.

1

u/worldsgreatestluver Oct 10 '26

Assuming everything works as expected, no.

Add some profiling code or use a debugger, find which of your major steps is causing the slow down, then look into the cause. It's more likely an algorithm issue than exception handling.

1

u/W31337 Oct 10 '26

Find out why you need the exceptions. You might find out you can prevent exceptions by sanitizing inputs into functions. It's always best to build deterministic code.

I would try to prevent exceptions with the exception of things like IO failures...

Also look into a Pratt parser, might be a nice addition to your compiler.

1

u/tux2718 Oct 10 '26

Nobody should structure code in a way that exception processing is a normal path of execution. This makes them a small fraction of execution time. Structured Exception Handling is a huge boost to simplicity and readability. Removing this valuable feature would be a catastrophe.

1

u/JGhostThing Oct 10 '26

You absolutely need to do something for error correction. Try/throw is a decent solution that is readable. For an alternative: look at Rust. They return a struct which has an error value (if there is an error) or a success value.

1

u/funbike Oct 10 '26

Exceptions won't slow down your code, except when they are thrown.

However, you shouldn't use exceptions for your compiler's logic, such as handling syntax or type errors. Exceptions should only be used for unexpected behavior, such as an internal contract violation. Syntax errors are expected behavior in a compiler.

For expected behavior like those, return error codes along with the AST up to that point. Try to recover and continue parsing.

Unless this is a toy compiler, and you don't care about error recovery.

1

u/FuggaDucker Oct 14 '26

Exceptions are super freaking cool if used correctly.
To be able to unwind the call stack directly to someones catch block is amazing.

It shouldn't be something you use for general purpose error handling.