2

In C++ you have compile flags to enable "Optimize debugging experience" using "-Og" or "/Og" (and possibly other flags on other compilers).

This flag enables very basic optimisations that don't interfere with the debugging experience (as far as I understand it). But it does mean that trivial or "free" optimisations made by the compiler are enabled for the program, which you then don't have to worry about.

From GCC optimise options, "Optimize debugging experience" is :

Optimize debugging experience. -Og should be the optimization level of choice for the standard edit-compile-debug cycle, offering a reasonable level of optimization while maintaining fast compilation and a good debugging experience. It is a better choice than -O0 for producing debuggable code because some compiler passes that collect debug information are disabled at -O0.

I was wondering if there was an option somewhere to enable the same kind of benefits, or if any such options are planned. Ideally which can be enabled through cargo and in as cross-platform a manner as possible.

Note that I'm not asking about "opt-levels" which are the equivalent of "-O1, -02, etc".

ShadowMitia
  • 2,411
  • 1
  • 19
  • 24
  • What would that do that isn't already done by default? – mcarton Jun 20 '20 at 22:52
  • @mcarton I believe by default the compiler does no optimisation by default in the dev profile. That flag in C++ allows so very basic optimisations to be enabled without interfering the debugging. – ShadowMitia Jun 21 '20 at 08:32

1 Answers1

4

In the Rust Cargo book on Profiles, you will see that the default compilation profile, named dev, is specified like this:

[profile.dev]
opt-level = 0
debug = true
debug-assertions = true
overflow-checks = true
lto = false
panic = 'unwind'
incremental = true
codegen-units = 256
rpath = false

As debug = true means that the full debug information is stored, this means that the compiled objects of the project will already be prepared for debugging, albeit with no optimisations. At the moment there is no flag in Cargo nor rustc to "optimize the debugging experience". While we can be sure that debugging symbols are retained, unlike what is stated by GCC where "debugging information in some passes is lost with -O0", applying optimisations and still have a good debugging experience is a bit of a trade-off game: in particular, LLVM provides some guarantees, but the abilities to navigate and play around with properties while in debug mode may become compromised (relevant LLVM documentation page).

If we take the true meaning of "improving the debugging experience", this is something that can be done on a case by case basis, by tweaking the compilation profile. For example, it is a common requirement in real-time program development, such as videogame development, to apply a few code optimisations so that the run-time performance is bearable. See the Rustc book on code-gen options to see the things that can be done on this end. Each opt-level would contribute to that experience in its own way.

See also:

E_net4
  • 27,810
  • 13
  • 101
  • 139
  • Thanks for your answer, but it doesn't answer my question. I know debug information stored by default, but I'm asking if there's an optimisation flag to get a better experience while in the dev profile. – ShadowMitia Jun 21 '20 at 08:28
  • @ShadowMitia I have updated the answer. The short answer is "not really", although there are parameters one can try and experiment with. – E_net4 Jun 21 '20 at 09:36