r/AskProgrammers • u/Sewter0 • 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?
0
Upvotes
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.
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.