# Idea: \`#\[opt\_level(level)\]\` for performance-sensitive functions

**URL:** <https://internals.rust-lang.org/t/idea-opt-level-level-for-performance-sensitive-functions/20113>\
**Category:** Uncategorized\
**Created:** [January 1, 2024, 1:50am UTC](https://internals.rust-lang.org/t/idea-opt-level-level-for-performance-sensitive-functions/20113 "2024-01-01T01:50:25Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![stackinspector](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/stackinspector/32/9487_2.png) [@stackinspector](https://internals.rust-lang.org/u/stackinspector)\
**Post date:** [January 1, 2024, 1:50am UTC](https://internals.rust-lang.org/t/idea-opt-level-level-for-performance-sensitive-functions/20113/1 "2024-01-01T01:50:25Z")

</div>

For some algorithm impl core functions (e.g. permutation functions for various crypto algorithms), I sometimes want them to be optimized in debug mode as well, because whether they are optimized or not can make a huge difference in performance and size\*, and in most cases debug mode is not for debugging these functions, which requires that they are still optimized while the rest of the program is complied in debug mode. First of all, I guess this may require creating some kind of "boundaries", is this possible with our current compiler? Then, the way to mark such functions could be an attribute like `#[optimize(condition)]`, indicating that optimization is required in the case specified by `condition`. In addition to the debug mode, other conditions (such as arch, etc.) may also be useful.

\*The [`keccak::f1600`](https://docs.rs/keccak/0.1.4/keccak/fn.f1600.html) function under `x86_64-pc-windows-msvc` measured a performance difference of 89.4x between debug and release (measured by subtracting `SystemTime` before and after executing a million times on an empty state), and a size difference of 24.2x (measured by `cargo-bloat`).

---

<div class="post-metadata">

**Author:** ![pitaj](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/pitaj/32/11262_2.png) [@pitaj](https://internals.rust-lang.org/u/pitaj)\
**Post date:** [January 1, 2024, 1:58am UTC](https://internals.rust-lang.org/t/idea-opt-level-level-for-performance-sensitive-functions/20113/2 "2024-01-01T01:58:32Z")

</div>

I think having this would be useful, but I just want to mention that you can optimize your dependencies while building your own code in debug mode.

---

<div class="post-metadata">

**Author:** ![Mokuz](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/mokuz/32/7225_2.png) [@Mokuz](https://internals.rust-lang.org/u/Mokuz)\
**Post date:** [January 1, 2024, 2:16am UTC](https://internals.rust-lang.org/t/idea-opt-level-level-for-performance-sensitive-functions/20113/3 "2024-01-01T02:16:14Z")

</div>

I think this is only possible on linux. I've tried it on windows it just silently broke everything (I tried finding out why but couldn't come up with anything).

---

<div class="post-metadata">

**Author:** ![toc](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/toc/32/6692_2.png) [@toc](https://internals.rust-lang.org/u/toc)\
**Post date:** [January 1, 2024, 8:01am UTC](https://internals.rust-lang.org/t/idea-opt-level-level-for-performance-sensitive-functions/20113/4 "2024-01-01T08:01:05Z")

</div>

> [@pitaj](#):
>
> I think having this would be useful, but I just want to mention that you can optimize your dependencies while building your own code in debug mode.

[https://doc.rust-lang.org/cargo/reference/profiles.html#overrides](https://doc.rust-lang.org/cargo/reference/profiles.html#overrides)

---

<div class="post-metadata">

**Author:** ![mathstuf](https://avatars.discourse-cdn.com/v4/letter/m/958977/32.png) [@mathstuf](https://internals.rust-lang.org/u/mathstuf)\
**Post date:** [January 1, 2024, 1:48pm UTC](https://internals.rust-lang.org/t/idea-opt-level-level-for-performance-sensitive-functions/20113/5 "2024-01-01T13:48:41Z")

</div>

The build type tends to get conflated with the selection of which runtime to use. On Windows, there are two incompatible runtimes: a release runtime and a debug runtime. You can compile your code however you want and independently select which runtime to use, but profiles will tend to choose based on how your crate is compiled. The runtime selection must be uniform across a program \[1\]. I doubt you're debugging the runtime, so using the release runtime and compiling your code with debug symbols seems the most reasonable thing to do (FWIW, CMake calls this "RelWithDebInfo").

* * *

1. there are ways around this, but probably not relevant here as the stdlib is really the deciding factor for Rust programs AFAIK.

---

<div class="post-metadata">

**Author:** ![Nemo157](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/nemo157/32/11585_2.png) [@Nemo157](https://internals.rust-lang.org/u/Nemo157)\
**Post date:** [January 1, 2024, 1:56pm UTC](https://internals.rust-lang.org/t/idea-opt-level-level-for-performance-sensitive-functions/20113/6 "2024-01-01T13:56:58Z")

</div>

I don't really have opinions on whether `#[optimize]` would be wanted/possible (or what semantics it would have, does it force transitive optimization too somehow?). But for cfg-based conditionality we already have `cfg_attr`, so the attribute doesn't need to implement that internally, it can just be used like `#[cfg_attr(some_conditon, optimize)]`.

---

<div class="post-metadata">

**Author:** ![dyslexicsteak](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/dyslexicsteak/32/11627_2.png) [@dyslexicsteak](https://internals.rust-lang.org/u/dyslexicsteak)\
**Post date:** [January 1, 2024, 3:28pm UTC](https://internals.rust-lang.org/t/idea-opt-level-level-for-performance-sensitive-functions/20113/7 "2024-01-01T15:28:37Z")

</div>

An `optimize` attribute already exists, also adds `optimize(size)` and `optimize(speed)` but is blocked on some attribute propagation stuff mentioned in the RFC rendered [here](https://github.com/rust-lang/rfcs/blob/master/text/2412-optimize-attr.md). It's tracked at [#54882](https://github.com/rust-lang/rust/issues/54882).

---

<div class="post-metadata">

**Author:** ![stackinspector](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/stackinspector/32/9487_2.png) [@stackinspector](https://internals.rust-lang.org/u/stackinspector)\
**Post date:** [January 1, 2024, 9:48pm UTC](https://internals.rust-lang.org/t/idea-opt-level-level-for-performance-sensitive-functions/20113/8 "2024-01-01T21:48:58Z")

</div>

> [@toc](#):
>
> [https://doc.rust-lang.org/cargo/reference/profiles.html#overrides](https://doc.rust-lang.org/cargo/reference/profiles.html#overrides)

It seems to only work for the current workspace. `tiny-keccak` [has this in its own Cargo.toml](https://github.com/debris/tiny-keccak/blob/f0fa146/Cargo.toml#L17-L23), but using it as a dependency for the same test, just replacing `keccak::f1600` with `tiny_keccak::keccakf`, the performance and sizing behaves the same. Perhaps it would be better if this was up to the crate author and affected all dependents. (Should there be a way for dependents to be able to turn it off?) And it would also be better to refine the granularity to functions rather than crates; crypto algorithms have peripheral abstractions in addition to the performance-sensitive core functions, and optimizing only the core functions would help with debugging the peripheral abstractions.

> [@dyslexicsteak](#):
>
> An `optimize` attribute already exists

It was the name of the option `opt-level` that reminded me. The `#[optimize(condition)]` was just a random thought, I didn't even consider `cfg_attr`. I would prefer the form like `#[cfg_attr(debug, opt_level(3))]` now. I will change the title.

---

<div class="post-metadata">

**Author:** ![dyslexicsteak](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/dyslexicsteak/32/11627_2.png) [@dyslexicsteak](https://internals.rust-lang.org/u/dyslexicsteak)\
**Post date:** [January 2, 2024, 4:40am UTC](https://internals.rust-lang.org/t/idea-opt-level-level-for-performance-sensitive-functions/20113/9 "2024-01-02T04:40:39Z")

</div>

The `optimize` attribute's discussion had missed some things in the RFC, like specifying the optimisation level. You can add these missing features; see [this comment](https://github.com/rust-lang/rust/issues/54882#issuecomment-427652235) about that.

---

<div class="post-metadata">

**Author:** ![system](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/system/32/14092_2.png) [@system](https://internals.rust-lang.org/u/system)\
**Post date:** [April 1, 2024, 4:41am UTC](https://internals.rust-lang.org/t/idea-opt-level-level-for-performance-sensitive-functions/20113/10 "2024-04-01T04:41:01Z")

</div>

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.
