Pre-RFC: branch optimization for panicking paths



Provide optimizer hints as “unlikely” for panicking paths.


Unlike exception (in other languages), panics are used for critical (logic) errors and is unlikely to be called. We can attempt to improve the performance of unwrap() with this.

I consider this RFC to be simple, or this maybe doesn’t need a RFC at all. Comments on improving or expanding this are welcome, as well as discussing potential issues of this RFC.


Would be nice with an RFC to at least discuss alternatives?


This doesn’t seem like RFC territory, since it’s a relatively small internal change that has no observable behavior aside from code quality. Furthermore, there’s nothing to build consensus about, it’s a matter of data: someone needs to implement this and show that it performs better.


I was under the impression that panics use landing pads similar to C++ exceptions. If that’s the case, then they should already be completely outside of the normal control flow.

edit: Or do you mean adding “unlikely” to branches that start a panic? That seems sensible.


Isn’t this already the case?


What the #[cold] that sfackler mentioned turns into in LLVM:

This attribute indicates that this function is rarely called. When computing edge weights, basic blocks post-dominated by a cold function call are also considered to be cold; and, thus, given low weight.


I have briefly tested on godbolt and seems you’re correct. Thanks for correcting me.