mmap vs sbrk, performance comparison
Asked Answered
G

1

5

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.

Garlen answered 1/4, 2011 at 18:53 Comment(6)
You are aware that they do similar but different things ?Ours
Yes I do. I just wanted to know if replacing one for the other in acceptable circumstances would lead to a performance difference.Garlen
Your mileage may vary. Best to measure against the exact environment you care about.Hypersensitive
Note: if you call sbrk you will risk breaking most malloc implmentations. This has consequences. For example, common C library calls [ like strdup() ], can be affected because they employ malloc().Neptune
Indeed, you definitely cannot use sbrk (or worse yet brk) in a program that might call malloc. And since any standard library function could call malloc, that means you cannot use the standard library, period.Unipersonal
I was writing a memory allocator so that isnt a problem since my allocator will be linked in place of malloc. I was just wondering about performance since it will be internally used for certain specific usages.Garlen
U
12

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 cycles
  • mmap: 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.

Unipersonal answered 1/4, 2011 at 19:33 Comment(3)
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.