@@ -111,36 +111,91 @@ pub const unsafe fn unreachable_unchecked() -> ! {
111111
112112/// Makes a *soundness* promise to the compiler that `cond` holds.
113113///
114- /// This may allow the optimizer to simplify things,
115- /// but it might also make the generated code slower.
116- /// Either way, calling it will most likely make compilation take longer.
114+ /// This may allow the optimizer to simplify things, but it might also make the generated code
115+ /// slower. Either way, calling it will most likely make compilation take longer.
117116///
118- /// This is a situational tool for micro-optimization, and is allowed to do nothing.
119- /// Any use should come with a repeatable benchmark to show the value
120- /// and allow removing it later should the optimizer get smarter and no longer need it .
117+ /// You may know this from other places as
118+ /// [`llvm.assume`](https://llvm.org/docs/LangRef.html#llvm-assume-intrinsic) or, in C,
119+ /// [`__builtin_assume`](https://clang.llvm.org/docs/LanguageExtensions.html#builtin-assume) .
121120///
122- /// The more complicated the condition the less likely this is to be fruitful.
123- /// For example, `assert_unchecked(foo.is_sorted())` is a complex enough value
124- /// that the compiler is unlikely to be able to take advantage of it.
121+ /// This promotes a correctness requirement to a soundness requirement. Don't do that without
122+ /// very good reason.
125123///
126- /// There's also no need to `assert_unchecked` basic properties of things. For
127- /// example, the compiler already knows the range of `count_ones`, so there's no
128- /// benefit to `let n = u32::count_ones(x); assert_unchecked(n <= u32::BITS);`.
124+ /// # Usage
129125///
130- /// If ever you're tempted to write `assert_unchecked(false)`, then you're
131- /// actually looking for [`unreachable_unchecked()`].
126+ /// This is a situational tool for micro-optimization, and is allowed to do nothing. Any use
127+ /// should come with a repeatable benchmark to show the value, with the expectation to drop it
128+ /// later should the optimizer get smarter and no longer need it.
132129///
133- /// You may know this from other places
134- /// as [`llvm.assume`](https://llvm.org/docs/LangRef.html#llvm-assume-intrinsic)
135- /// or [`__builtin_assume`](https://clang.llvm.org/docs/LanguageExtensions.html#builtin-assume) .
130+ /// The more complicated the condition, the less likely this is to be fruitful. For example,
131+ /// `assert_unchecked(foo.is_sorted())` is a complex enough value that the compiler is unlikely
132+ /// to be able to take advantage of it .
136133///
137- /// This promotes a correctness requirement to a soundness requirement.
138- /// Don't do that without very good reason.
134+ /// There's also no need to `assert_unchecked` basic properties of things. For example, the
135+ /// compiler already knows the range of `count_ones`, so there is no benefit to
136+ /// `let n = u32::count_ones(x); assert_unchecked(n <= u32::BITS);`.
137+ ///
138+ /// In release mode, the argument will most likely not actually be evaluated.
139+ ///
140+ /// If ever you are tempted to write `assert_unchecked(false)`, then you are actually looking for
141+ /// [`unreachable_unchecked()`].
139142///
140143/// # Safety
141144///
142- /// `cond` must be `true`. It's immediate UB to call this with `false`.
145+ /// `cond` must be `true`. It is immediate UB to call this with `false`.
146+ ///
147+ /// # Example
148+ ///
149+ /// ```
150+ /// #![feature(hint_assert_unchecked)]
151+ ///
152+ /// use core::hint;
153+ ///
154+ /// /// # Safety
155+ /// ///
156+ /// /// `p` must be nonnull and valid
157+ /// pub unsafe fn next_value(p: *const i32) -> i32 {
158+ /// // SAFETY: caller invariants guarantee that `p` is not null
159+ /// unsafe { hint::assert_unchecked(!p.is_null()) }
160+ ///
161+ /// if p.is_null() {
162+ /// return -1;
163+ /// } else {
164+ /// // SAFETY: caller invariants guarantee that `p` is valid
165+ /// unsafe { *p + 1 }
166+ /// }
167+ /// }
168+ /// ```
169+ ///
170+ /// Without the `assert_unchecked`, the above function produces the following with optimizations
171+ /// enabled:
172+ ///
173+ /// ```asm
174+ /// next_value:
175+ /// test rdi, rdi
176+ /// je .LBB0_1
177+ /// mov eax, dword ptr [rdi]
178+ /// inc eax
179+ /// ret
180+ /// .LBB0_1:
181+ /// mov eax, -1
182+ /// ret
183+ /// ```
184+ ///
185+ /// Adding the assertion allows the optimizer to remove the extra check:
186+ ///
187+ /// ```asm
188+ /// next_value:
189+ /// mov eax, dword ptr [rdi]
190+ /// inc eax
191+ /// ret
192+ /// ```
143193///
194+ /// This example is quite unlike anything that would happen in the real world: it is redundant to
195+ /// put an an assertion right next to code that checks the same thing, and dereferencing a
196+ /// pointer already has the builtin assumption that it is nonnull. However, the optimizer can
197+ /// make use of this information even when it isn't as obvious, such as when checks happen in
198+ /// called functions.
144199#[ inline( always) ]
145200#[ doc( alias = "assume" ) ]
146201#[ track_caller]
0 commit comments