Skip to content

Commit 864fba1

Browse files
committed
slightly optimize Layout::repeat_packed
1 parent d3797c7 commit 864fba1

3 files changed

Lines changed: 16 additions & 43 deletions

File tree

.github/workflows/test.macos.tmp.yml

Lines changed: 0 additions & 35 deletions
This file was deleted.

src/allocs/c_alloc.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,6 @@ impl Dealloc for CAlloc {
142142
Ok(())
143143
}
144144
}
145-
// TODO: now that grow and shrink are gone, a manual realloc may actually be good
146145
impl Realloc for CAlloc {}
147146

148147
pub use crate::ffi::c_alloc as ffi;

src/layout.rs

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,17 @@ impl Layout {
176176
let new_align = if a_aln > b_aln { a_aln } else { b_aln };
177177

178178
// check the total size fits within limits and doesn't overflow.
179+
// TODO: maybe use the overflow check trick here instead too
180+
// DRAFT:
181+
// if a_sz > usize::MAX - offset {
182+
// return Err(LayoutErr::ArithErr(ArithErr(a_sz, ArithOp::Add, offset)));
183+
// }
184+
// let total = a_sz + offset;
185+
// if total > USIZE_MAX_NO_HIGH_BIT {
186+
// return Err(LayoutErr::ExceedsMax(offset, new_align, 1));
187+
// }
188+
// // SAFETY: we validated alignment and size constraints above.
189+
// Ok((unsafe { Layout::from_size_align_unchecked(total, new_align) }, offset))
179190
match checked_op(a_sz, ArithOp::Add, offset) {
180191
Ok(total) if total <= USIZE_MAX_NO_HIGH_BIT => {
181192
// SAFETY: we validated alignment and size constraints above.
@@ -419,13 +430,11 @@ impl Layout {
419430
#[::rustversion::attr(since(1.47), const)]
420431
#[inline]
421432
pub fn repeat_packed(&self, count: usize) -> Result<Layout, LayoutErr> {
422-
// TODO: use the overflow check trick here? alternatively, use checked_op more elsewhere?
423-
// need to test both for speed.
424-
let size = match checked_op(self.size(), ArithOp::Mul, count) {
425-
Ok(s) => s,
426-
Err(e) => return Err(LayoutErr::ArithErr(e))
427-
};
428-
match Layout::from_size_align(size, self.align()) {
433+
// this is ~1.6x faster than using checked_op
434+
if count > usize::MAX / self.size() {
435+
return Err(LayoutErr::ArithErr(ArithErr(self.size(), ArithOp::Mul, count)));
436+
}
437+
match Layout::from_size_align(self.size() * count, self.align()) {
429438
Ok(layout) => Ok(layout),
430439
Err(e) => Err(e)
431440
}

0 commit comments

Comments
 (0)