postgresql/src/backend/utils
Andres Freund 58b25e9810 Add "Slab" MemoryContext implementation for efficient equal-sized allocations.
The default general purpose aset.c style memory context is not a great
choice for allocations that are all going to be evenly sized,
especially when those objects aren't small, and have varying
lifetimes.  There tends to be a lot of fragmentation, larger
allocations always directly go to libc rather than have their cost
amortized over several pallocs.

These problems lead to the introduction of ad-hoc slab allocators in
reorderbuffer.c. But it turns out that the simplistic implementation
leads to problems when a lot of objects are allocated and freed, as
aset.c is still the underlying implementation. Especially freeing can
easily run into O(n^2) behavior in aset.c.

While the O(n^2) behavior in aset.c can, and probably will, be
addressed, custom allocators for this behavior are more efficient
both in space and time.

This allocator is for evenly sized allocations, and supports both
cheap allocations and freeing, without fragmenting significantly.  It
does so by allocating evenly sized blocks via malloc(), and carves
them into chunks that can be used for allocations.  In order to
release blocks to the OS as early as possible, chunks are allocated
from the fullest block that still has free objects, increasing the
likelihood of a block being entirely unused.

A subsequent commit uses this in reorderbuffer.c, but a further
allocator is needed to resolve the performance problems triggering
this work.

There likely are further potentialy uses of this allocator besides
reorderbuffer.c.

There's potential further optimizations of the new slab.c, in
particular the array of freelists could be replaced by a more
intelligent structure - but for now this looks more than good enough.

Author: Tomas Vondra, editorialized by Andres Freund
Reviewed-By: Andres Freund, Petr Jelinek, Robert Haas, Jim Nasby
Discussion: https://postgr.es/m/d15dff83-0b37-28ed-0809-95a5cc7292ad@2ndquadrant.com
2017-02-27 03:41:44 -08:00
..
adt Remove useless duplicate inclusions of system header files. 2017-02-25 16:12:55 -05:00
cache Consistently declare timestamp variables as TimestampTz. 2017-02-23 15:57:08 -05:00
error Update copyright via script for 2017 2017-01-03 13:48:53 -05:00
fmgr Fix typos in comments. 2017-02-06 11:33:58 +02:00
hash Update copyright via script for 2017 2017-01-03 13:48:53 -05:00
init Remove useless duplicate inclusions of system header files. 2017-02-25 16:12:55 -05:00
mb Add KOI8-U map files to Makefile. 2017-02-02 14:12:35 +02:00
misc Remove now-dead code for !HAVE_INT64_TIMESTAMP. 2017-02-23 14:04:43 -05:00
mmgr Add "Slab" MemoryContext implementation for efficient equal-sized allocations. 2017-02-27 03:41:44 -08:00
resowner Update copyright via script for 2017 2017-01-03 13:48:53 -05:00
sort Don't create "holes" in BufFiles, in the new logtape code. 2017-02-01 12:17:38 +02:00
time Consistently declare timestamp variables as TimestampTz. 2017-02-23 15:57:08 -05:00
.gitignore Generate fmgr prototypes automatically 2017-01-17 14:06:07 -05:00
errcodes.txt Update copyright via script for 2017 2017-01-03 13:48:53 -05:00
Gen_dummy_probes.pl Update copyright via script for 2017 2017-01-03 13:48:53 -05:00
Gen_dummy_probes.sed Update copyright via script for 2017 2017-01-03 13:48:53 -05:00
Gen_fmgrtab.pl Generate fmgr prototypes automatically 2017-01-17 14:06:07 -05:00
generate-errcodes.pl Update copyright via script for 2017 2017-01-03 13:48:53 -05:00
Makefile Fix parallel make issue with new fmgrprotos.h 2017-01-23 15:36:27 -05:00
probes.d Rename dtrace probes for ongoing xlog -> wal conversion. 2017-02-09 16:40:19 -05:00