From: Robert Haas Date: Thu, 10 Apr 2014 20:01:52 +0000 (+0000) Subject: Push obsize/nmax computation down. X-Git-Url: http://git.postgresql.org/gitweb/static/close/reject?a=commitdiff_plain;h=4504aff4f4b69cd254cbdceed7d09bcd71bc9daa;p=users%2Frhaas%2Fpostgres.git Push obsize/nmax computation down. Testing shows this is faster (5.4s vs 5.7s for 100m 8-byte allocations). --- diff --git a/src/backend/utils/mmgr/sb_alloc.c b/src/backend/utils/mmgr/sb_alloc.c index 31d4eee7c9..bf4b0586c5 100644 --- a/src/backend/utils/mmgr/sb_alloc.c +++ b/src/backend/utils/mmgr/sb_alloc.c @@ -110,8 +110,7 @@ static char sb_size_class_map[] = { #define SB_NUM_SIZE_CLASSES lengthof(sb_size_classes) /* Helper functions. */ -static char *sb_alloc_from_heap(char *base, sb_heap *heap, - Size obsize, Size nmax); +static char *sb_alloc_from_heap(char *base, sb_heap *heap, int size_class); static char *sb_alloc_guts(char *base, sb_region *region, sb_allocator *a, int size_class); static void sb_init_span(char *base, sb_span *span, sb_heap *heap, @@ -345,12 +344,22 @@ sb_reset_allocator(sb_allocator *a) * superblock that would otherwise become empty soon. */ static char * -sb_alloc_from_heap(char *base, sb_heap *heap, Size obsize, Size nmax) +sb_alloc_from_heap(char *base, sb_heap *heap, int size_class) { sb_span *active_sb; Size fclass; char *superblock; char *result; + Size obsize; + Size nmax; + + /* Work out object size. */ + Assert(size_class < SB_NUM_SIZE_CLASSES); + obsize = sb_size_classes[size_class]; + if (size_class == 0) + nmax = FPM_PAGE_SIZE / obsize; + else + nmax = (FPM_PAGE_SIZE * SB_PAGES_PER_SUPERBLOCK) / obsize; /* * If fullness class 1 is empty, try to find something to put in it by @@ -461,23 +470,13 @@ sb_alloc_guts(char *base, sb_region *region, sb_allocator *a, int size_class) sb_heap *heap = &a->heaps[size_class]; LWLock *lock = relptr_access(base, heap->lock); char *result = NULL; - Size obsize; - Size nmax; - - /* Work out object size. */ - Assert(size_class < SB_NUM_SIZE_CLASSES); - obsize = sb_size_classes[size_class]; - if (size_class == 0) - nmax = FPM_PAGE_SIZE / obsize; - else - nmax = (FPM_PAGE_SIZE * SB_PAGES_PER_SUPERBLOCK) / obsize; /* If locking is in use, acquire the lock. */ if (lock != NULL) LWLockAcquire(lock, LW_EXCLUSIVE); /* Attempt to allocate from the heap. */ - result = sb_alloc_from_heap(base, heap, obsize, nmax); + result = sb_alloc_from_heap(base, heap, size_class); /* * If there's no space in the current heap, but there are multiple heaps @@ -489,7 +488,7 @@ sb_alloc_guts(char *base, sb_region *region, sb_allocator *a, int size_class) if (sb_try_to_steal_superblock(base, a, size_class)) { /* The superblock we stole shouldn't full, so this should work. */ - result = sb_alloc_from_heap(base, heap, obsize, nmax); + result = sb_alloc_from_heap(base, heap, size_class); Assert(result != NULL); } else @@ -548,7 +547,7 @@ sb_alloc_guts(char *base, sb_region *region, sb_allocator *a, int size_class) span->ninitialized = span->nused = 1; /* This should work now. */ - result = sb_alloc_from_heap(base, heap, obsize, nmax); + result = sb_alloc_from_heap(base, heap, size_class); Assert(result != NULL); } }