Skip to content

Commit 67e3f87

Browse files
committed
Auto merge of #341 - JustForFun88:edit_do_alloc, r=Amanieu
Editing `do_alloc` for reducing LLVM IR 1. I think this will speed up compilation, since one way or another everything will come down to this code (but I didn’t compare performance). 2. I don’t know if the compiler was able to optimize that old code, but it literally checked the same thing twice (due to `map` and `map_err` in a row, and roughly speaking it came down to: ```rust pub fn do_alloc<A: Allocator>(alloc: &A, layout: Layout) -> Result<NonNull<u8>, ()> { match match alloc.allocate(layout) { Ok(ptr) => Ok(ptr.as_non_null_ptr()), Err(e) => Err(e), } { Ok(ptr) => Ok(ptr), Err(_) => Err(()), } } ``` And when the code is written explicitly, it is immediately clear that it looks strange. And why force the compiler to think beyond the need? 3. And finally, in my opinion, the readability of the code has not only not decreased, but even increased.
2 parents d11a701 + 9d23b42 commit 67e3f87

File tree

1 file changed

+4
-4
lines changed

1 file changed

+4
-4
lines changed

src/raw/alloc.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@ mod inner {
88

99
#[allow(clippy::map_err_ignore)]
1010
pub fn do_alloc<A: Allocator>(alloc: &A, layout: Layout) -> Result<NonNull<u8>, ()> {
11-
alloc
12-
.allocate(layout)
13-
.map(|ptr| ptr.as_non_null_ptr())
14-
.map_err(|_| ())
11+
match alloc.allocate(layout) {
12+
Ok(ptr) => Ok(ptr.as_non_null_ptr()),
13+
Err(_) => Err(()),
14+
}
1515
}
1616

1717
#[cfg(feature = "bumpalo")]

0 commit comments

Comments
 (0)