Skip to content

Commit b81e788

Browse files
Indicate that multiplication in Layout::array cannot overflow
This allows LLVM to optimize comparisons to zero before & after the multiplication into one, saving on code size and eliminating an (always true) branch from most Vec allocations.
1 parent a4a5c97 commit b81e788

File tree

1 file changed

+5
-1
lines changed

1 file changed

+5
-1
lines changed

library/core/src/alloc/layout.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -450,7 +450,11 @@ impl Layout {
450450
return Err(LayoutError);
451451
}
452452

453-
let array_size = element_size * n;
453+
// SAFETY: We just checked that we won't overflow `usize` when we multiply.
454+
// This is a useless hint inside this function, but after inlining this helps
455+
// deduplicate checks for whether the overall capacity is zero (e.g., in RawVec's
456+
// allocation path) before/after this multiplication.
457+
let array_size = unsafe { element_size.unchecked_mul(n) };
454458

455459
// SAFETY: We just checked above that the `array_size` will not
456460
// exceed `isize::MAX` even when rounded up to the alignment.

0 commit comments

Comments
 (0)