On Sun, Feb 12, 2023 at 4:14 AM Andres Freund <and...@anarazel.de> wrote:
>
> > +ssize_t
> > +pg_pwrite_zeros(int fd, size_t size)
> > +{
> > +     PGAlignedBlock  zbuffer;
> > +     size_t  zbuffer_sz;
> > +     struct iovec    iov[PG_IOV_MAX];
> > +     int             blocks;
> > +     size_t  remaining_size = 0;
> > +     int             i;
> > +     ssize_t written;
> > +     ssize_t total_written = 0;
> > +
> > +     zbuffer_sz = sizeof(zbuffer.data);
> > +
> > +     /* Zero-fill the buffer. */
> > +     memset(zbuffer.data, 0, zbuffer_sz);
>
> I previously commented on this - why are we memseting a buffer on every call
> to this?  That's not at all free.
>
> Something like
>     static const PGAlignedBlock zerobuf = {0};
> would do the trick.  You do need to cast the const away, to assign to
> iov_base, but that's not too ugly.

Thanks for looking at it. We know that we don't change the zbuffer in
the function, so can we avoid static const and have just a static
variable, like the attached
v1-0001-Use-static-variable-to-avoid-memset-calls-in-pg_p.patch? Do
you see any problem with it?

FWIW, it comes out like the attached
v1-0001-Use-static-const-variable-to-avoid-memset-calls-i.patch with
static const.

--
Bharath Rupireddy
PostgreSQL Contributors Team
RDS Open Source Databases
Amazon Web Services: https://aws.amazon.com
From 2527c6083bbdf006900be1a1dbf15ed815184d2b Mon Sep 17 00:00:00 2001
From: Bharath Rupireddy <bharath.rupireddyforpostgres@gmail.com>
Date: Sun, 12 Feb 2023 10:58:33 +0000
Subject: [PATCH v1] Use static variable to avoid memset() calls in
 pg_pwrite_zeros()

---
 src/common/file_utils.c | 8 +++-----
 1 file changed, 3 insertions(+), 5 deletions(-)

diff --git a/src/common/file_utils.c b/src/common/file_utils.c
index 4dae534152..0d59488f60 100644
--- a/src/common/file_utils.c
+++ b/src/common/file_utils.c
@@ -539,7 +539,8 @@ pg_pwritev_with_retry(int fd, const struct iovec *iov, int iovcnt, off_t offset)
 ssize_t
 pg_pwrite_zeros(int fd, size_t size)
 {
-	PGAlignedBlock zbuffer;		/* worth BLCKSZ */
+	/* Zero-filled buffer worth BLCKSZ. */
+	static PGAlignedBlock zbuffer = {0};
 	size_t		zbuffer_sz;
 	struct iovec iov[PG_IOV_MAX];
 	int			blocks;
@@ -548,10 +549,7 @@ pg_pwrite_zeros(int fd, size_t size)
 	ssize_t		written;
 	ssize_t		total_written = 0;
 
-	zbuffer_sz = sizeof(zbuffer.data);
-
-	/* Zero-fill the buffer. */
-	memset(zbuffer.data, 0, zbuffer_sz);
+	zbuffer_sz = BLCKSZ;
 
 	/* Prepare to write out a lot of copies of our zero buffer at once. */
 	for (i = 0; i < lengthof(iov); ++i)
-- 
2.34.1

From adc5ea83fb6726a5d8a1f3b87d6b3b03550c7fcb Mon Sep 17 00:00:00 2001
From: Bharath Rupireddy <bharath.rupireddyforpostgres@gmail.com>
Date: Sun, 12 Feb 2023 10:55:09 +0000
Subject: [PATCH v1] Use static const variable to avoid memset calls in
 pg_pwrite_zeros

---
 src/common/file_utils.c | 10 ++++------
 1 file changed, 4 insertions(+), 6 deletions(-)

diff --git a/src/common/file_utils.c b/src/common/file_utils.c
index 4dae534152..cd15cc2fdb 100644
--- a/src/common/file_utils.c
+++ b/src/common/file_utils.c
@@ -539,7 +539,8 @@ pg_pwritev_with_retry(int fd, const struct iovec *iov, int iovcnt, off_t offset)
 ssize_t
 pg_pwrite_zeros(int fd, size_t size)
 {
-	PGAlignedBlock zbuffer;		/* worth BLCKSZ */
+	/* Zero-filled buffer worth BLCKSZ. */
+	static const PGAlignedBlock zbuffer = {0};
 	size_t		zbuffer_sz;
 	struct iovec iov[PG_IOV_MAX];
 	int			blocks;
@@ -548,15 +549,12 @@ pg_pwrite_zeros(int fd, size_t size)
 	ssize_t		written;
 	ssize_t		total_written = 0;
 
-	zbuffer_sz = sizeof(zbuffer.data);
-
-	/* Zero-fill the buffer. */
-	memset(zbuffer.data, 0, zbuffer_sz);
+	zbuffer_sz = BLCKSZ;
 
 	/* Prepare to write out a lot of copies of our zero buffer at once. */
 	for (i = 0; i < lengthof(iov); ++i)
 	{
-		iov[i].iov_base = zbuffer.data;
+		iov[i].iov_base = (void *) (unconstify(PGAlignedBlock *, &zbuffer)->data);
 		iov[i].iov_len = zbuffer_sz;
 	}
 
-- 
2.34.1

Reply via email to