perf(alloc): skip `*_aligned calls when alignment fits default - #170
Open
chirizxc wants to merge 1 commit into
Open
perf(alloc): skip `*_aligned calls when alignment fits default#170chirizxc wants to merge 1 commit into
*_aligned calls when alignment fits default#170chirizxc wants to merge 1 commit into
Conversation
chirizxc
commented
Aug 7, 2026
Comment on lines
+86
to
+96
| unsafe fn dealloc(&self, ptr: *mut u8, _layout: Layout) { | ||
| mi_free(ptr as *mut c_void); | ||
| } | ||
|
|
||
| #[inline] | ||
| unsafe fn dealloc(&self, ptr: *mut u8, _layout: Layout) { | ||
| mi_free(ptr as *mut c_void); | ||
| unsafe fn alloc_zeroed(&self, layout: Layout) -> *mut u8 { | ||
| if layout.align() <= MIN_MIMALLOC_ALIGNMENT { | ||
| mi_zalloc(layout.size()) as *mut u8 | ||
| } else { | ||
| mi_zalloc_aligned(layout.size(), layout.align()) as *mut u8 | ||
| } |
chirizxc
commented
Aug 7, 2026
Comment on lines
113
to
168
| #[test] | ||
| fn it_frees_allocated_memory() { | ||
| unsafe { | ||
| let layout = Layout::from_size_align(8, 8).unwrap(); | ||
| let alloc = MiMalloc; | ||
| let layout = Layout::from_size_align(8, 8).unwrap(); | ||
| let alloc = MiMalloc; | ||
|
|
||
| let ptr = alloc.alloc(layout); | ||
| alloc.dealloc(ptr, layout); | ||
| } | ||
| let ptr = unsafe { alloc.alloc(layout) }; | ||
| unsafe { alloc.dealloc(ptr, layout) }; | ||
| } | ||
|
|
||
| #[test] | ||
| fn it_frees_allocated_big_memory() { | ||
| unsafe { | ||
| let layout = Layout::from_size_align(1 << 20, 32).unwrap(); | ||
| let alloc = MiMalloc; | ||
| let layout = Layout::from_size_align(1 << 20, 32).unwrap(); | ||
| let alloc = MiMalloc; | ||
|
|
||
| let ptr = alloc.alloc(layout); | ||
| alloc.dealloc(ptr, layout); | ||
| } | ||
| let ptr = unsafe { alloc.alloc(layout) }; | ||
| unsafe { alloc.dealloc(ptr, layout) }; | ||
| } | ||
|
|
||
| #[test] | ||
| fn it_frees_zero_allocated_memory() { | ||
| unsafe { | ||
| let layout = Layout::from_size_align(8, 8).unwrap(); | ||
| let alloc = MiMalloc; | ||
| let layout = Layout::from_size_align(8, 8).unwrap(); | ||
| let alloc = MiMalloc; | ||
|
|
||
| let ptr = alloc.alloc_zeroed(layout); | ||
| alloc.dealloc(ptr, layout); | ||
| } | ||
| let ptr = unsafe { alloc.alloc_zeroed(layout) }; | ||
| unsafe { alloc.dealloc(ptr, layout) }; | ||
| } | ||
|
|
||
| #[test] | ||
| fn it_frees_zero_allocated_big_memory() { | ||
| unsafe { | ||
| let layout = Layout::from_size_align(1 << 20, 32).unwrap(); | ||
| let alloc = MiMalloc; | ||
| let layout = Layout::from_size_align(1 << 20, 32).unwrap(); | ||
| let alloc = MiMalloc; | ||
|
|
||
| let ptr = alloc.alloc_zeroed(layout); | ||
| alloc.dealloc(ptr, layout); | ||
| } | ||
| let ptr = unsafe { alloc.alloc_zeroed(layout) }; | ||
| unsafe { alloc.dealloc(ptr, layout) }; | ||
| } | ||
|
|
||
| #[test] | ||
| fn it_frees_reallocated_memory() { | ||
| unsafe { | ||
| let layout = Layout::from_size_align(8, 8).unwrap(); | ||
| let alloc = MiMalloc; | ||
| let layout = Layout::from_size_align(8, 8).unwrap(); | ||
| let alloc = MiMalloc; | ||
|
|
||
| let ptr = alloc.alloc(layout); | ||
| let ptr = alloc.realloc(ptr, layout, 16); | ||
| alloc.dealloc(ptr, layout); | ||
| } | ||
| let ptr = unsafe { alloc.alloc(layout) }; | ||
| let ptr = unsafe { alloc.realloc(ptr, layout, 16) }; | ||
| unsafe { alloc.dealloc(ptr, layout) }; | ||
| } | ||
|
|
||
| #[test] | ||
| fn it_frees_reallocated_big_memory() { | ||
| unsafe { | ||
| let layout = Layout::from_size_align(1 << 20, 32).unwrap(); | ||
| let alloc = MiMalloc; | ||
| let layout = Layout::from_size_align(1 << 20, 32).unwrap(); | ||
| let alloc = MiMalloc; | ||
|
|
||
| let ptr = alloc.alloc(layout); | ||
| let ptr = alloc.realloc(ptr, layout, 2 << 20); | ||
| alloc.dealloc(ptr, layout); | ||
| } | ||
| let ptr = unsafe { alloc.alloc(layout) }; | ||
| let ptr = unsafe { alloc.realloc(ptr, layout, 2 << 20) }; | ||
| unsafe { alloc.dealloc(ptr, layout) }; | ||
| } | ||
| } |
Author
There was a problem hiding this comment.
Not everything inside those blocks was unsafe
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.

For my library, this provides a small speedup (about ~3.2%):