Use PGAlignedBlock instead of "char buf[BLCKSZ]" in more places

From: Bharath Rupireddy <bharath(dot)rupireddyforpostgres(at)gmail(dot)com>
To: PostgreSQL Hackers <pgsql-hackers(at)lists(dot)postgresql(dot)org>
Subject: Use PGAlignedBlock instead of "char buf[BLCKSZ]" in more places
Date: 2023-12-04 01:29:13
Message-ID: CALj2ACUHqyGvTWn3+Yt21eyDmu7SrYVOcGZ7SAwsvsBK7dJEMg@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

Hi,

The commit 44cac934 replaced "char buf[BLCKSZ]" with PGAlignedBlock to
avoid issues on alignment-picky hardware. While it replaced most of the
instances, there are still some more left. How about we use PGAlignedBlock
there too, something like the attached patch? A note [2] in the commit
44cac934 says that ensuring proper alignment makes kernel data transfers
fasters and the left-over "char buf[BLCKSZ]" either do read() or write()
system calls, so it might be worth to align them with PGAlignedBlock.

Thoughts?

PS: FWIW, I verified what difference actually char buf[BLCKSZ] and the
union PGAlignedBlock does make with alignment with a sample code like [3]
which gives a different alignment requirement, see below:

size of data 8192, alignment of data 1
size of data_aligned 8192, alignment of data_aligned 8

[1]
commit 44cac9346479d4b0cc9195b0267fd13eb4e7442c
Author: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
Date: Sat Sep 1 15:27:12 2018 -0400

Avoid using potentially-under-aligned page buffers.

[2]
I used these types even for variables where there's no risk of a
misaligned access, since ensuring proper alignment should make
kernel data transfers faster. I also changed some places where
we had been palloc'ing short-lived buffers, for coding style
uniformity and to save palloc/pfree overhead.

[3]
#include <stdio.h>

#define BLCKSZ 8192

typedef union PGAlignedBlock
{
char data[BLCKSZ];
double force_align_d;
long long int force_align_i64;
} PGAlignedBlock;

int main(int argc, char **argv)
{
char data[BLCKSZ];
PGAlignedBlock data_aligned;

printf("size of data %ld, alignment of data %ld\n", sizeof(data),
_Alignof(data));
printf("size of data_aligned %ld, alignment of data_aligned %ld\n",
sizeof(data_aligned), _Alignof(data_aligned));

return 0;
}

--
Bharath Rupireddy
PostgreSQL Contributors Team
RDS Open Source Databases
Amazon Web Services: https://aws.amazon.com

Attachment Content-Type Size
v1-0001-Use-PGAlignedBlock-instead-of-char-buf-BLCKSZ-in-.patch application/x-patch 6.5 KB

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Tomas Vondra 2023-12-04 01:45:46 Re: Syncrep and improving latency due to WAL throttling
Previous Message Davin Shearer 2023-12-04 01:27:49 Re: Emitting JSON to file using COPY TO