-
Notifications
You must be signed in to change notification settings - Fork 12.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Specialize Vec::from_elem to use calloc #40409
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -44,6 +44,11 @@ pub extern "C" fn __rust_allocate(size: usize, align: usize) -> *mut u8 { | |
unsafe { imp::allocate(size, align) } | ||
} | ||
|
||
#[no_mangle] | ||
pub extern "C" fn __rust_allocate_zeroed(size: usize, align: usize) -> *mut u8 { | ||
unsafe { imp::allocate_zeroed(size, align) } | ||
} | ||
|
||
#[no_mangle] | ||
pub extern "C" fn __rust_deallocate(ptr: *mut u8, old_size: usize, align: usize) { | ||
unsafe { imp::deallocate(ptr, old_size, align) } | ||
|
@@ -121,6 +126,18 @@ mod imp { | |
} | ||
} | ||
|
||
pub unsafe fn allocate_zeroed(size: usize, align: usize) -> *mut u8 { | ||
if align <= MIN_ALIGN { | ||
libc::calloc(size as libc::size_t, 1) as *mut u8 | ||
} else { | ||
let ptr = aligned_malloc(size, align); | ||
if !ptr.is_null() { | ||
ptr::write_bytes(ptr, 0, size); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This fallback seems a bit unfortunate, but I guess there's no other option? |
||
} | ||
ptr | ||
} | ||
} | ||
|
||
pub unsafe fn reallocate(ptr: *mut u8, old_size: usize, size: usize, align: usize) -> *mut u8 { | ||
if align <= MIN_ALIGN { | ||
libc::realloc(ptr as *mut libc::c_void, size as libc::size_t) as *mut u8 | ||
|
@@ -173,6 +190,8 @@ mod imp { | |
#[repr(C)] | ||
struct Header(*mut u8); | ||
|
||
|
||
const HEAP_ZERO_MEMORY: DWORD = 0x00000008; | ||
const HEAP_REALLOC_IN_PLACE_ONLY: DWORD = 0x00000010; | ||
|
||
unsafe fn get_header<'a>(ptr: *mut u8) -> &'a mut Header { | ||
|
@@ -185,18 +204,27 @@ mod imp { | |
aligned | ||
} | ||
|
||
pub unsafe fn allocate(size: usize, align: usize) -> *mut u8 { | ||
#[inline] | ||
unsafe fn allocate_with_flags(size: usize, align: usize, flags: DWORD) -> *mut u8 { | ||
if align <= MIN_ALIGN { | ||
HeapAlloc(GetProcessHeap(), 0, size as SIZE_T) as *mut u8 | ||
HeapAlloc(GetProcessHeap(), flags, size as SIZE_T) as *mut u8 | ||
} else { | ||
let ptr = HeapAlloc(GetProcessHeap(), 0, (size + align) as SIZE_T) as *mut u8; | ||
let ptr = HeapAlloc(GetProcessHeap(), flags, (size + align) as SIZE_T) as *mut u8; | ||
if ptr.is_null() { | ||
return ptr; | ||
} | ||
align_ptr(ptr, align) | ||
} | ||
} | ||
|
||
pub unsafe fn allocate(size: usize, align: usize) -> *mut u8 { | ||
allocate_with_flags(size, align, 0) | ||
} | ||
|
||
pub unsafe fn allocate_zeroed(size: usize, align: usize) -> *mut u8 { | ||
allocate_with_flags(size, align, HEAP_ZERO_MEMORY) | ||
} | ||
|
||
pub unsafe fn reallocate(ptr: *mut u8, _old_size: usize, size: usize, align: usize) -> *mut u8 { | ||
if align <= MIN_ALIGN { | ||
HeapReAlloc(GetProcessHeap(), 0, ptr as LPVOID, size as SIZE_T) as *mut u8 | ||
|
This file contains 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
This file contains 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
This file contains 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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am not super familiar with jemalloc - is
calloc
faster than unconditionally callingmallocx
here?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, mallocx is parsing the options out from flags’ bitmask, whereas calloc sets them directly. Difference likely negligible though.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Cool, thanks. Seems fine either way.