postgresql/src/backend
David Rowley d21ded75fd Improve the performance of the slab memory allocator
Slab has traditionally been fairly slow when compared with the AllocSet or
Generation memory allocators.  Part of this slowness came from having to
write out an entire block when we allocate a new block in order to
populate the free list indexes within the block's memory.  Additional
slowness came from having to move a block onto another dlist each time we
palloc or pfree a chunk from it.

Here we optimize both of those cases and do a little bit extra to improve
the performance of the slab allocator.

Here, instead of writing out the free list indexes when allocating a new
block, we introduce the concept of "unused" chunks.  When a block is first
allocated all chunks are unused.  These chunks only make it onto the
free list when they are pfree'd.  When allocating new chunks on an
existing block, we have the choice of consuming a chunk from the free list
or an unused chunk.  When both exist, we opt to use one from the free
list, as these have been used already and the memory of them is more
likely to be cached by the CPU.

Here we also reduce the number of block lists from there being one for
every possible value of free chunks on a block to just having a small
fixed number of block lists.  We keep the 0th block list for completely
full blocks and anything else stores blocks for some range of free chunks
with fuller blocks appearing on lower block list array elements.  This
reduces how often we must move a block to another list when we allocate or
free chunks, but still allows us to prefer to put new chunks on fuller
blocks and perhaps allow blocks with fewer chunks to be free'd later
once all their remaining chunks have been pfree'd.

Additionally, we now store a list of "emptyblocks", which are blocks that
no longer contain any allocated chunks.  We now keep up to 10 of these
around to avoid having to thrash malloc/free when allocation patterns
continually cause blocks to become free of any allocated chunks only to
allocate more chunks again.  Now only once we have 10 of these, we free
the block.  This does raise the high water mark for the total memory that
a slab context can consume.  It does not seem entirely unreasonable that
we might one day want to make this a property of SlabContext rather than a
compile-time constant.  Let's wait and see if there is any evidence to
support that this is required before doing it.

Author: Andres Freund, David Rowley
Tested-by: Tomas Vondra, John Naylor
Discussion: https://postgr.es/m/20210717194333.mr5io3zup3kxahfm@alap3.anarazel.de
2022-12-20 21:48:51 +13:00
..
access Add pg_dissect_walfile_name() 2022-12-20 13:36:27 +09:00
backup Static assertions cleanup 2022-12-15 10:10:32 +01:00
bootstrap Order getopt arguments 2022-12-12 15:20:00 +01:00
catalog Static assertions cleanup 2022-12-15 10:10:32 +01:00
commands Add grantable MAINTAIN privilege and pg_maintain role. 2022-12-13 17:33:28 -08:00
executor Static assertions cleanup 2022-12-15 10:10:32 +01:00
foreign Rename SetSingleFuncCall() to InitMaterializedSRF() 2022-10-18 10:22:35 +09:00
jit Replace SQLValueFunction by COERCE_SQL_SYNTAX 2022-11-21 18:31:59 +09:00
lib Add doubly linked count list implementation 2022-11-02 14:06:05 +13:00
libpq Remove hardcoded dependency to cryptohash type in the internals of SCRAM 2022-12-20 08:53:22 +09:00
main Add workaround to make ubsan and ps_status.c compatible 2022-11-21 13:56:23 -08:00
nodes Create infrastructure for "soft" error reporting. 2022-12-09 09:58:38 -05:00
optimizer Remove pessimistic cost penalization from Incremental Sort 2022-12-16 15:22:23 +13:00
parser Fix inability to reference CYCLE column from inside its CTE. 2022-12-16 13:07:42 -05:00
partitioning Move PartitioPruneInfo out of plan nodes into PlannedStmt 2022-12-01 12:56:21 +01:00
po meson: Add initial version of meson based build system 2022-09-21 22:37:17 -07:00
port Static assertions cleanup 2022-12-15 10:10:32 +01:00
postmaster Order getopt arguments 2022-12-12 15:20:00 +01:00
regex Remove uses of register due to incompatibility with C++17 and up 2022-09-24 12:08:37 -07:00
replication Avoid unnecessary streaming of transactions during logical replication. 2022-12-08 06:05:09 +05:30
rewrite Update outdated comment in ApplyRetrieveRule 2022-12-07 12:35:59 +01:00
snowball meson: Add support for building with precompiled headers 2022-10-06 17:19:30 -07:00
statistics Stop accessing checkAsUser via RTE in some cases 2022-11-30 12:07:03 +01:00
storage Expose some information about backend subxact status. 2022-12-19 14:43:09 -05:00
tcop Speed up creation of command completion tags 2022-12-16 10:31:25 +13:00
tsearch Add comments and a missing CHECK_FOR_INTERRUPTS in ts_headline. 2022-11-21 17:07:29 -05:00
utils Improve the performance of the slab memory allocator 2022-12-20 21:48:51 +13:00
.gitignore
Makefile autoconf: Move export_dynamic determination to configure 2022-12-06 18:55:28 -08:00
common.mk Blind attempt to fix LLVM dependency in the backend 2022-09-15 10:53:48 +07:00
meson.build meson: Add support for building with precompiled headers 2022-10-06 17:19:30 -07:00
nls.mk NLS: Put list of available languages into LINGUAS files 2022-07-13 08:19:17 +02:00