|
| 1 | +- Feature Name: expect_intrinsic |
| 2 | +- Start Date: 2015-05-20 |
| 3 | +- RFC PR: (leave this empty) |
| 4 | +- Rust Issue: (leave this empty) |
| 5 | + |
| 6 | +# Summary |
| 7 | + |
| 8 | +Provide an intrinsic function for hinting the likelyhood of branches being taken. |
| 9 | + |
| 10 | +# Motivation |
| 11 | + |
| 12 | +Branch prediction can have significant effects on the running time of some code. Especially tight |
| 13 | +inner loops which may be run millions of times. While in general programmers aren't able to |
| 14 | +effectively provide hints to the compiler, there are cases where the likelyhood of some branch |
| 15 | +being taken can be known. |
| 16 | + |
| 17 | +For example: in arbitrary-precision arithmetic, operations are often performed in a base that is |
| 18 | +equal to `2^word_size`. The most basic division algorithm, "Schoolbook Division", has a step that |
| 19 | +will be taken in `2/B` cases (where `B` is the base the numbers are in), given random input. On a |
| 20 | +32-bit processor that is approximately one in two billion cases, for 64-bit it's one in 18 |
| 21 | +quintillion cases. |
| 22 | + |
| 23 | +# Detailed design |
| 24 | + |
| 25 | +Implement an `expect` intrinsic with the signature: `fn(bool, bool) -> bool`. The first argument is |
| 26 | +the condition being tested, the second argument is the expected result. The return value is the |
| 27 | +same as the first argument, meaning that `if foo == bar { .. }` can be simply replaced with |
| 28 | +`if expect(foo == bar, false) { .. }`. |
| 29 | + |
| 30 | +The expected value is required to be a constant value. |
| 31 | + |
| 32 | +# Drawbacks |
| 33 | + |
| 34 | +The second argument is required to be a constant value, which can't be easily expressed. |
| 35 | + |
| 36 | +# Alternatives |
| 37 | + |
| 38 | +Provide a pair of intrinsics `likely` and `unlikely`, these are the same as `expect` just with |
| 39 | +`true` and `false` substituted in for the expected value, respectively. |
| 40 | + |
| 41 | +# Unresolved questions |
| 42 | + |
| 43 | +None. |
0 commit comments