Adjust memory allocation functions to allow sibling calls

Many modern compilers are able to optimize function calls to functions
where the parameters of the called function match a leading subset of
the calling function's parameters.  If there are no instructions in the
calling function after the function is called, then the compiler is free
to avoid any stack frame setup and implement the function call as a
"jmp" rather than a "call".  This is called sibling call optimization.

Here we adjust the memory allocation functions in mcxt.c to allow this
optimization.  This requires moving some responsibility into the memory
context implementations themselves.  It's now the responsibility of the
MemoryContext to check for malloc failures.  This is good as it both
allows the sibling call optimization, but also because most small and
medium allocations won't call malloc and just allocate memory to an
existing block.  That can't fail, so checking for NULLs in that case
isn't required.

Also, traditionally it's been the responsibility of palloc and the other
allocation functions in mcxt.c to check for invalid allocation size
requests.  Here we also move the responsibility of checking that into the
MemoryContext.  This isn't to allow the sibling call optimization, but
more because most of our allocators handle large allocations separately
and we can just add the size check when doing large allocations.  We no
longer check this for non-large allocations at all.

To make checking the allocation request sizes and ERROR handling easier,
add some helper functions to mcxt.c for the allocators to use.

Author: Andres Freund
Reviewed-by: David Rowley
Discussion: 
https://postgr.es/m/20210719195950.gavgs6ujzmjfa...@alap3.anarazel.de

Branch
------
master

Details
-------
https://git.postgresql.org/pg/commitdiff/743112a2e9938b98d716384fa9374c41afe74e41

Modified Files
--------------
src/backend/utils/mmgr/alignedalloc.c |  11 +-
src/backend/utils/mmgr/aset.c         |  22 ++--
src/backend/utils/mmgr/generation.c   |  17 +--
src/backend/utils/mmgr/mcxt.c         | 229 ++++++++++++++--------------------
src/backend/utils/mmgr/slab.c         |  11 +-
src/include/nodes/memnodes.h          |  42 ++++++-
src/include/utils/memutils_internal.h |  30 +++--
7 files changed, 196 insertions(+), 166 deletions(-)

Reply via email to