Optimize alignment calculations in tuple form/deform
authorDavid Rowley <drowley@postgresql.org>
Fri, 20 Dec 2024 20:43:26 +0000 (09:43 +1300)
committerDavid Rowley <drowley@postgresql.org>
Fri, 20 Dec 2024 20:43:26 +0000 (09:43 +1300)
commitdb448ce5ad36a2754e4e75900b180260143aacf8
tree54753cad82035c6a3cdf7edaa897291b09622d6b
parent1f81b48a9d567ae9074ab1f3233eae9997b3d7bd
Optimize alignment calculations in tuple form/deform

Here we convert CompactAttribute.attalign from a char, which is directly
derived from pg_attribute.attalign into a uint8, which stores the number
of bytes to align the column's value by in the tuple.

This allows tuple deformation and tuple size calculations to move away
from using the inefficient att_align_nominal() macro, which manually
checks each TYPALIGN_* char to translate that into the alignment bytes
for the given type.  Effectively, this commit changes those to TYPEALIGN
calls, which are branchless and only perform some simple arithmetic with
some bit-twiddling.

The removed branches were often mispredicted by CPUs, especially so in
real-world tables which often contain a mishmash of different types
with different alignment requirements.

Author: David Rowley
Reviewed-by: Andres Freund, Victor Yegorov
Discussion: http://postgr.es/m/CAApHDvrBztXP3yx=NKNmo3xwFAFhEdyPnvrDg3=M0RhDs+4vYw@mail.gmail.com
12 files changed:
contrib/amcheck/verify_heapam.c
contrib/pageinspect/heapfuncs.c
src/backend/access/brin/brin_tuple.c
src/backend/access/common/attmap.c
src/backend/access/common/heaptuple.c
src/backend/access/common/indextuple.c
src/backend/access/common/tupdesc.c
src/backend/executor/execExprInterp.c
src/backend/executor/execTuples.c
src/backend/jit/llvm/llvmjit_deform.c
src/include/access/tupdesc.h
src/include/access/tupmacs.h