memory management - mmap vs malloc vs calloc performance microbenchmark: What to expect -
memory management - mmap vs malloc vs calloc performance microbenchmark: What to expect -
i have created microbenchmark compare allocation performance , rss usage malloc vs mmap.
i'm coming conclusion mmap fastest unless utilize memory. hence utilize sparsely populated info structures.
my questions whether conclusion right , numbers came create sense (see below details).
#include <string.h> #include <stdio.h> #include <stdlib.h> #include <sys/mman.h> #include <sys/time.h> int main() { #define size 100 void* allocated[size]; size_t size[size]; timeval then, now; memset(allocated, 0, sizeof allocated ); gettimeofday(&then, null); size_t count = 0; size_t = 0; ( ;; ) { if ( allocated[ ] ) { munmap( allocated[ ], size[ ] ); //free( allocated[ ] ); } size[ ] = rand() % 40000000 + 4096; allocated[ ] = mmap( null, size[ ], prot_read | prot_write, map_private | map_anonymous /* | map_populate */ , -1, 0 ); //allocated[ ] = malloc( size[ ] ); //allocated[ ] = calloc( 1, size[ ] ); //allocated[ ] = calloc( size[ ], 1 ); = (i+1) % size; if ( !( ++count & 0xfff ) ) { gettimeofday(&now, null); double timedelta = now.tv_sec-then.tv_sec + 1.e-6*( now.tv_usec-then.tv_usec ); printf( "%f allocations/s\n", count/timedelta ); } } }
on scheme (sandy bridge desktop machine), get:
mmap: ~900.000 allocations/s, rss ~0.5 mb, vsize ~2gb mmap + map_populate: ~800 allocations/s, rss , vsize ~2gb malloc: ~280.000 allocations/s, rss , vsize ~2gb calloc: ~550 allocations/s, rss , vsize ~2gb malloc, malloc_perturb_ == 1: ~260 allocations/s, rss , vsize ~2gbi have 'perf record'ed bit.
mmap + map_populate spends of time in clear_page_c_e (2m calls/s), called __mm_populate. guessed page fault taxation have pay if code mmap'ed memory, but...
...malloc (without malloc_perturb_ environment variable set anything) spends of time in clear_page_c_e (also 2m calls/s), achieves many more allocations/s.
calloc , malloc malloc_perturb_ spend of time in memset. understand malloc. thought calloc supposed map pages copy-on-write page of zeros, glibc not that, apparently.
everyone except plain mmap increased rss, little surprising me. thought malloc and/or calloc when using memory.
memory-management malloc mmap copy-on-write
Comments
Post a Comment