A few months ago, after 1.5 years of OCaml I discovered Printexc.record_backtrace true
.
This is soooo useful to debug, so why isn’t it set to true by default ? I think it would help a lot of beginners, some of whose don’t even know the word backtrace
. The few languages I know have it by default (Python
, Java
). Is there some downside of backtrace that make this false by default ?
I find it’s easier to prefix my compile/run command with OCAMLRUNPARAM="b"
when I want to enable backtraces for my exceptions rather than explicitly call Printexec.record_backtrace
which may require recompiling the code. W.r.t beginners, it might just be sufficient to ask beginners to add that to their shell init files.
it might just be sufficient to ask beginners to add that to their shell init files
If we agree that for beginners it’s better to have backtrace enabled, then I think that it should be activated by default and be an option to remove it for advanced users.
TL;DR; Backtrace recording has some performance impact, especially on code that uses them heavily, which used to be the default style in OCaml.
When you’re using Base or Core, it is enabled by default. In vanilla OCaml, it is indeed disabled by default, because it will affect performance in the exception-heavy code, and the default style in OCaml (if such exists) is to use exceptions a lot. OCaml is moving away from it, for example, the Janestreet style prefers ADT to exceptions, that’s why the exceptions could be enabled in Base and Core without a noticeable impact on performance. As an anecdote, my first big project in OCaml (it was 3.11) that was written in the style of that epoch, did have a noticeable performance impact when backtrace recording was enabled, so I had to disable them manually after I switched to Core. My current project doesn’t raise exceptions much so there is no difference in performance.
Ok I didn’t know it affected performances. Maybe the compiler could add an hint after a program fail with exception like “you can try to use OCAMLPARAM=b to see the backtrace of this error”
This is a great suggestion. Perhaps you could file an issue on the repo?
Here it is :
I recently added alias dunetrace='OCAMLRUNPARAM="b" dune'
to my zshrc to make it a bit more convenient to reach for when I do want it. For some reason (there probably is one that I haven’t read enough to understand), dune exec --debug-backtraces
doesn’t do the trick for me.