Skip to content

Commit 04e9877

Browse files
Rerevert "Remove checked_add in Layout::repeat"
1 parent b135c73 commit 04e9877

File tree

1 file changed

+9
-2
lines changed

1 file changed

+9
-2
lines changed

src/libcore/alloc.rs

+9-2
Original file line numberDiff line numberDiff line change
@@ -235,11 +235,18 @@ impl Layout {
235235
#[unstable(feature = "alloc_layout_extra", issue = "55724")]
236236
#[inline]
237237
pub fn repeat(&self, n: usize) -> Result<(Self, usize), LayoutErr> {
238-
// This cannot overflow. Quoting from the invariant of Layout:
238+
// `padded_size` cannot overflow. Quoting from the invariant of Layout:
239239
// > `size`, when rounded up to the nearest multiple of `align`,
240240
// > must not overflow (i.e., the rounded value must be less than
241241
// > `usize::MAX`)
242-
let padded_size = self.size() + self.padding_needed_for(self.align());
242+
//
243+
// However, replacing this line with an `unchecked_add` or a regular `+`
244+
// operator caused a noticeable slowdown, see #69710.
245+
let padded_size = self
246+
.size()
247+
.checked_add(self.padding_needed_for(self.align()))
248+
.ok_or(LayoutErr { private: () })?;
249+
243250
let alloc_size = padded_size.checked_mul(n).ok_or(LayoutErr { private: () })?;
244251

245252
unsafe {

0 commit comments

Comments
 (0)