Which of these calls is faster on average? I've heard that mmap
is faster for smaller allocations but I haven't heard a comparison of either. Any information on performance for these would be nice.
mmap vs sbrk, performance comparison
You should tag this with a particular implementation (like linux
) since the answer surely varies by implementation. For now I'll assume Linux since it's the most popular.
With that said, brk
is in theory more optimizable, and in practice it runs about 10% faster on my machine. Allocating one page, these are the times I get:
brk
: min 2550 cycles, typical 2650 cyclesmmap
: min 2700 cycles, typical 2800 cycles
I remember hearing something along the lines of brk
being able to skip locking the mmap
semaphore, which would explain the discrepancy.
Note: I updated these times after adjusting my test to make a dummy calls prior to timing, to ensure that the code would all be in the cache.
oddly enough for me mmap was actually running faster than sbrk but then again with sbrk its possible to keep memory contiguous. –
Garlen
Perhaps your system's
sbrk
is performing some userspace locking or accounting, or even calling the brk
syscall twice. (once to get the old brk
and again to set the new one...?) If you're implementing malloc
I would not rely on the system library's sbrk
but make the brk
syscall yourself. –
Unipersonal I ended up using mmap as the primary way to grab memory from the system and use sbrk as a backup in case I get MAP_FAILED, I might try using the brk call instead to see what the performance of that would be. –
Garlen
© 2022 - 2024 — McMap. All rights reserved.
sbrk
(or worse yetbrk
) in a program that might callmalloc
. And since any standard library function could callmalloc
, that means you cannot use the standard library, period. – Unipersonal