rust-lang / wg-allocators

Home of the Allocators working group: Paving a path for a standard set of allocator traits to be used in collections!
http://bit.ly/hello-wg-allocators
203 stars 9 forks source link

Implement `grow_zeroed` efficiently where possible #100

Open andylizi opened 2 years ago

andylizi commented 2 years ago

This was suggested a long time ago in #14, but although efficiency was mentioned as one of the motivations, the implementation just calls memset without making use of any potentially faster platform APIs.

I think these should be added since they can be efficiently implemented with the mremap system call on Linux. mremap allows you to move/grow/shrink a memory mapping, and any new pages added for growth are guaranteed to be zeroed.

Unfortunately for unix platforms, we're forwarding to libc whose realloc() doesn't support zeroing. But Windows HeapReAlloc does have HEAP_ZERO_MEMORY, and we're already using that for alloc_zeroed. So why not realloc_zeroed?

Here's my proof-of-concept: https://github.com/andylizi/rust/commits/realloc-zeroed