Skip to content

Commit 8dd5c41

Browse files
committed
Use doubly-linked block lists in aset.c to reduce large-chunk overhead.
Large chunks (those too large for any palloc freelist) are managed as separate blocks. Formerly, realloc'ing or pfree'ing such a chunk required O(N) time in a context with N blocks, since we had to traipse down the singly-linked block list to locate the block's predecessor before we could fix the list links. This can result in O(N^2) runtime in situations where large numbers of such chunks are manipulated within one context. Cases like that were not foreseen in the original design of aset.c, and indeed didn't arise until fairly recently. But such problems can now occur in reorderbuffer.c and in hash joining, both of which make repeated large requests without scaling up their request size as they do so, and which will free their requests in not-necessarily-LIFO order. To fix, change the block list from singly-linked to doubly-linked. This adds another 4 or 8 bytes to ALLOC_BLOCKHDRSZ, but that doesn't seem like unacceptable overhead, since aset.c's blocks are normally 8K or more, and never less than 1K in current practice. In passing, get rid of some redundant AllocChunkGetPointer() calls in AllocSetRealloc (the compiler might be smart enough to optimize these away anyway, but no need to assume that) and improve AllocSetCheck's checking of block header fields. Back-patch to 9.4 where reorderbuffer.c appeared. We could take this further back, but currently there's no evidence that it would be useful. Discussion: https://postgr.es/m/CAMkU=1x1hvue1XYrZoWk_omG0Ja5nBvTdvgrOeVkkeqs71CV8g@mail.gmail.com
1 parent 9420ea8 commit 8dd5c41

File tree

1 file changed

+66
-42
lines changed

1 file changed

+66
-42
lines changed

src/backend/utils/mmgr/aset.c

Lines changed: 66 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,8 @@ typedef AllocSetContext *AllocSet;
201201
typedef struct AllocBlockData
202202
{
203203
AllocSet aset; /* aset that owns this block */
204-
AllocBlock next; /* next block in aset's blocks list */
204+
AllocBlock prev; /* prev block in aset's blocks list, if any */
205+
AllocBlock next; /* next block in aset's blocks list, if any */
205206
char *freeptr; /* start of free space in this block */
206207
char *endptr; /* end of space in this block */
207208
} AllocBlockData;
@@ -508,7 +509,10 @@ AllocSetContextCreate(MemoryContext parent,
508509
block->aset = context;
509510
block->freeptr = ((char *) block) + ALLOC_BLOCKHDRSZ;
510511
block->endptr = ((char *) block) + blksize;
512+
block->prev = NULL;
511513
block->next = context->blocks;
514+
if (block->next)
515+
block->next->prev = block;
512516
context->blocks = block;
513517
/* Mark block as not to be released at reset time */
514518
context->keeper = block;
@@ -590,6 +594,7 @@ AllocSetReset(MemoryContext context)
590594
VALGRIND_MAKE_MEM_NOACCESS(datastart, block->freeptr - datastart);
591595
#endif
592596
block->freeptr = datastart;
597+
block->prev = NULL;
593598
block->next = NULL;
594599
}
595600
else
@@ -702,16 +707,20 @@ AllocSetAlloc(MemoryContext context, Size size)
702707
#endif
703708

704709
/*
705-
* Stick the new block underneath the active allocation block, so that
706-
* we don't lose the use of the space remaining therein.
710+
* Stick the new block underneath the active allocation block, if any,
711+
* so that we don't lose the use of the space remaining therein.
707712
*/
708713
if (set->blocks != NULL)
709714
{
715+
block->prev = set->blocks;
710716
block->next = set->blocks->next;
717+
if (block->next)
718+
block->next->prev = block;
711719
set->blocks->next = block;
712720
}
713721
else
714722
{
723+
block->prev = NULL;
715724
block->next = NULL;
716725
set->blocks = block;
717726
}
@@ -898,7 +907,10 @@ AllocSetAlloc(MemoryContext context, Size size)
898907
VALGRIND_MAKE_MEM_NOACCESS(block->freeptr,
899908
blksize - ALLOC_BLOCKHDRSZ);
900909

910+
block->prev = NULL;
901911
block->next = set->blocks;
912+
if (block->next)
913+
block->next->prev = block;
902914
set->blocks = block;
903915
}
904916

@@ -958,29 +970,28 @@ AllocSetFree(MemoryContext context, void *pointer)
958970
{
959971
/*
960972
* Big chunks are certain to have been allocated as single-chunk
961-
* blocks. Find the containing block and return it to malloc().
973+
* blocks. Just unlink that block and return it to malloc().
962974
*/
963-
AllocBlock block = set->blocks;
964-
AllocBlock prevblock = NULL;
975+
AllocBlock block = (AllocBlock) (((char *) chunk) - ALLOC_BLOCKHDRSZ);
965976

966-
while (block != NULL)
967-
{
968-
if (chunk == (AllocChunk) (((char *) block) + ALLOC_BLOCKHDRSZ))
969-
break;
970-
prevblock = block;
971-
block = block->next;
972-
}
973-
if (block == NULL)
977+
/*
978+
* Try to verify that we have a sane block pointer: it should
979+
* reference the correct aset, and freeptr and endptr should point
980+
* just past the chunk.
981+
*/
982+
if (block->aset != set ||
983+
block->freeptr != block->endptr ||
984+
block->freeptr != ((char *) block) +
985+
(chunk->size + ALLOC_BLOCKHDRSZ + ALLOC_CHUNKHDRSZ))
974986
elog(ERROR, "could not find block containing chunk %p", chunk);
975-
/* let's just make sure chunk is the only one in the block */
976-
Assert(block->freeptr == ((char *) block) +
977-
(chunk->size + ALLOC_BLOCKHDRSZ + ALLOC_CHUNKHDRSZ));
978987

979988
/* OK, remove block from aset's list and free it */
980-
if (prevblock == NULL)
981-
set->blocks = block->next;
989+
if (block->prev)
990+
block->prev->next = block->next;
982991
else
983-
prevblock->next = block->next;
992+
set->blocks = block->next;
993+
if (block->next)
994+
block->next->prev = block->prev;
984995
#ifdef CLOBBER_FREED_MEMORY
985996
wipe_mem(block, block->freeptr - ((char *) block));
986997
#endif
@@ -1085,27 +1096,24 @@ AllocSetRealloc(MemoryContext context, void *pointer, Size size)
10851096
if (oldsize > set->allocChunkLimit)
10861097
{
10871098
/*
1088-
* The chunk must have been allocated as a single-chunk block. Find
1089-
* the containing block and use realloc() to make it bigger with
1090-
* minimum space wastage.
1099+
* The chunk must have been allocated as a single-chunk block. Use
1100+
* realloc() to make the containing block bigger with minimum space
1101+
* wastage.
10911102
*/
1092-
AllocBlock block = set->blocks;
1093-
AllocBlock prevblock = NULL;
1103+
AllocBlock block = (AllocBlock) (((char *) chunk) - ALLOC_BLOCKHDRSZ);
10941104
Size chksize;
10951105
Size blksize;
10961106

1097-
while (block != NULL)
1098-
{
1099-
if (chunk == (AllocChunk) (((char *) block) + ALLOC_BLOCKHDRSZ))
1100-
break;
1101-
prevblock = block;
1102-
block = block->next;
1103-
}
1104-
if (block == NULL)
1107+
/*
1108+
* Try to verify that we have a sane block pointer: it should
1109+
* reference the correct aset, and freeptr and endptr should point
1110+
* just past the chunk.
1111+
*/
1112+
if (block->aset != set ||
1113+
block->freeptr != block->endptr ||
1114+
block->freeptr != ((char *) block) +
1115+
(chunk->size + ALLOC_BLOCKHDRSZ + ALLOC_CHUNKHDRSZ))
11051116
elog(ERROR, "could not find block containing chunk %p", chunk);
1106-
/* let's just make sure chunk is the only one in the block */
1107-
Assert(block->freeptr == ((char *) block) +
1108-
(chunk->size + ALLOC_BLOCKHDRSZ + ALLOC_CHUNKHDRSZ));
11091117

11101118
/* Do the realloc */
11111119
chksize = MAXALIGN(size);
@@ -1124,10 +1132,12 @@ AllocSetRealloc(MemoryContext context, void *pointer, Size size)
11241132
/* Update pointers since block has likely been moved */
11251133
chunk = (AllocChunk) (((char *) block) + ALLOC_BLOCKHDRSZ);
11261134
pointer = AllocChunkGetPointer(chunk);
1127-
if (prevblock == NULL)
1128-
set->blocks = block;
1135+
if (block->prev)
1136+
block->prev->next = block;
11291137
else
1130-
prevblock->next = block;
1138+
set->blocks = block;
1139+
if (block->next)
1140+
block->next->prev = block;
11311141
chunk->size = chksize;
11321142

11331143
#ifdef MEMORY_CONTEXT_CHECKING
@@ -1151,7 +1161,7 @@ AllocSetRealloc(MemoryContext context, void *pointer, Size size)
11511161

11521162
/* set mark to catch clobber of "unused" space */
11531163
if (size < chunk->size)
1154-
set_sentinel(AllocChunkGetPointer(chunk), size);
1164+
set_sentinel(pointer, size);
11551165
#else /* !MEMORY_CONTEXT_CHECKING */
11561166

11571167
/*
@@ -1164,7 +1174,8 @@ AllocSetRealloc(MemoryContext context, void *pointer, Size size)
11641174

11651175
/* Make any trailing alignment padding NOACCESS. */
11661176
VALGRIND_MAKE_MEM_NOACCESS((char *) pointer + size, chksize - size);
1167-
return AllocChunkGetPointer(chunk);
1177+
1178+
return pointer;
11681179
}
11691180
else
11701181
{
@@ -1297,9 +1308,12 @@ AllocSetCheck(MemoryContext context)
12971308
{
12981309
AllocSet set = (AllocSet) context;
12991310
char *name = set->header.name;
1311+
AllocBlock prevblock;
13001312
AllocBlock block;
13011313

1302-
for (block = set->blocks; block != NULL; block = block->next)
1314+
for (prevblock = NULL, block = set->blocks;
1315+
block != NULL;
1316+
prevblock = block, block = block->next)
13031317
{
13041318
char *bpoz = ((char *) block) + ALLOC_BLOCKHDRSZ;
13051319
long blk_used = block->freeptr - bpoz;
@@ -1316,6 +1330,16 @@ AllocSetCheck(MemoryContext context)
13161330
name, block);
13171331
}
13181332

1333+
/*
1334+
* Check block header fields
1335+
*/
1336+
if (block->aset != set ||
1337+
block->prev != prevblock ||
1338+
block->freeptr < bpoz ||
1339+
block->freeptr > block->endptr)
1340+
elog(WARNING, "problem in alloc set %s: corrupt header in block %p",
1341+
name, block);
1342+
13191343
/*
13201344
* Chunk walker
13211345
*/

0 commit comments

Comments
 (0)