On Monday 29 November 2010 18:34:02 Robert Haas wrote:
> On Mon, Nov 29, 2010 at 12:24 PM, Andres Freund <and...@anarazel.de> wrote:
> > Hm. A quick test shows that its quite a bit faster if you allocate memory
> > with:
> > size_t s = 512*1024*1024;
> > char *bss = mmap(0, s, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_POPULATE|
> > MAP_ANONYMOUS, -1, 0);
> 
> Numbers?
malloc alloc: 43
malloc memset1: 438763
malloc memset2: 98764
total: 537570

mmap alloc: 296065
mmap memset1: 99203
mmap memset2: 100608
total: 495876

But you don't actually need the memset1 in the mmap case as MAP_ANONYMOUS 
memory is already zeroed. We could actually use that knowledge even without 
MAP_POPULATE if we somehow keep track whether an allocated memory region is 
still zeroed.

Taking that into account its:

malloc alloc: 47
malloc memset1: 437819
malloc memset2: 98317
total: 536183
mmap alloc: 292904
mmap memset1: 1
mmap memset2: 99284
total: 392189


I am somewhat reluctant to believe thats the way to go.

Andres

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/time.h>
#include <sys/mman.h>
#include <malloc.h>

char bss[512*1024*1024];

void
print_times(char *tag, struct timeval *before, struct timeval *after)
{
	int result = (after->tv_sec - before->tv_sec) * 1000000
		+ ((int)after->tv_usec) - ((int)before->tv_usec);
	printf("%s: %d\n", tag, result);
}

int
main(int argc, char **argv)
{
	size_t s = 512*1024*1024;

	struct timeval t1_1;
	struct timeval t1_2;
	struct timeval t1_3;
	struct timeval t1_4;
	struct timeval t2_1;
	struct timeval t2_2;
	struct timeval t2_3;
	struct timeval t2_4;

	if (gettimeofday(&t1_1, NULL))
		return 1;
	mallopt(M_MMAP_MAX, 0);
	char* bss1 = malloc(s);

	if (gettimeofday(&t1_2, NULL))
		return 1;

	memset(bss1, 0, s);

	if (gettimeofday(&t1_3, NULL))
		return 1;

	memset(bss1, 0, s);

	if (gettimeofday(&t1_4, NULL))
		return 1;



	if (gettimeofday(&t2_1, NULL))
		return 1;

	char* bss2 = mmap(0, s, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_POPULATE|MAP_ANONYMOUS, -1, 0);

	if (gettimeofday(&t2_2, NULL))
		return 1;

	//memset(bss1, 0, s);

	if (gettimeofday(&t2_3, NULL))
		return 1;

	memset(bss1, 0, s);

	if (gettimeofday(&t2_4, NULL))
		return 1;


	print_times("malloc alloc", &t1_1, &t1_2);
	print_times("malloc memset1", &t1_2, &t1_3);
	print_times("malloc memset2", &t1_3, &t1_4);
	print_times("total", &t1_1, &t1_4);

	print_times("mmap alloc", &t2_1, &t2_2);
	print_times("mmap memset1", &t2_2, &t2_3);
	print_times("mmap memset2", &t2_3, &t2_4);
	print_times("total", &t2_1, &t2_4);

	return 0;
}
-- 
Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-hackers

Reply via email to