From 9c5846cc3f04c6797696a234fac0953815e09e4b Mon Sep 17 00:00:00 2001
From: Justin Pryzby <pryzbyj@telsasoft.com>
Date: Sun, 23 Oct 2022 14:52:48 -0500
Subject: [PATCH 1/6] WIP: do not loop to set an array to false

See also:
9fd45870c1436b477264c0c82eb195df52bc0919
572e3e6634e55accf95e2bcfb1340019c86a21dc
---
 src/backend/access/transam/xlogprefetcher.c |  5 +---
 src/backend/catalog/pg_constraint.c         | 11 ++------
 src/backend/commands/user.c                 |  6 +----
 src/backend/statistics/extended_stats.c     |  6 +----
 src/backend/storage/smgr/md.c               |  3 +--
 src/backend/tcop/pquery.c                   |  3 +--
 src/backend/utils/adt/arrayfuncs.c          | 29 ++++++++-------------
 src/test/isolation/isolationtester.c        |  3 +--
 8 files changed, 19 insertions(+), 47 deletions(-)

diff --git a/src/backend/access/transam/xlogprefetcher.c b/src/backend/access/transam/xlogprefetcher.c
index 0cf03945eec..7fcfd146525 100644
--- a/src/backend/access/transam/xlogprefetcher.c
+++ b/src/backend/access/transam/xlogprefetcher.c
@@ -832,13 +832,10 @@ pg_stat_get_recovery_prefetch(PG_FUNCTION_ARGS)
 #define PG_STAT_GET_RECOVERY_PREFETCH_COLS 10
 	ReturnSetInfo *rsinfo = (ReturnSetInfo *) fcinfo->resultinfo;
 	Datum		values[PG_STAT_GET_RECOVERY_PREFETCH_COLS];
-	bool		nulls[PG_STAT_GET_RECOVERY_PREFETCH_COLS];
+	bool		nulls[PG_STAT_GET_RECOVERY_PREFETCH_COLS] = {0};
 
 	InitMaterializedSRF(fcinfo, 0);
 
-	for (int i = 0; i < PG_STAT_GET_RECOVERY_PREFETCH_COLS; ++i)
-		nulls[i] = false;
-
 	values[0] = TimestampTzGetDatum(pg_atomic_read_u64(&SharedStats->reset_time));
 	values[1] = Int64GetDatum(pg_atomic_read_u64(&SharedStats->prefetch));
 	values[2] = Int64GetDatum(pg_atomic_read_u64(&SharedStats->hit));
diff --git a/src/backend/catalog/pg_constraint.c b/src/backend/catalog/pg_constraint.c
index 3a5d4e96439..bd8f32bf62d 100644
--- a/src/backend/catalog/pg_constraint.c
+++ b/src/backend/catalog/pg_constraint.c
@@ -82,8 +82,8 @@ CreateConstraintEntry(const char *constraintName,
 	Relation	conDesc;
 	Oid			conOid;
 	HeapTuple	tup;
-	bool		nulls[Natts_pg_constraint];
-	Datum		values[Natts_pg_constraint];
+	bool		nulls[Natts_pg_constraint] = {0};
+	Datum		values[Natts_pg_constraint] = {0};
 	ArrayType  *conkeyArray;
 	ArrayType  *confkeyArray;
 	ArrayType  *conpfeqopArray;
@@ -165,13 +165,6 @@ CreateConstraintEntry(const char *constraintName,
 	else
 		conexclopArray = NULL;
 
-	/* initialize nulls and values */
-	for (i = 0; i < Natts_pg_constraint; i++)
-	{
-		nulls[i] = false;
-		values[i] = (Datum) NULL;
-	}
-
 	conOid = GetNewOidWithIndex(conDesc, ConstraintOidIndexId,
 								Anum_pg_constraint_oid);
 	values[Anum_pg_constraint_oid - 1] = ObjectIdGetDatum(conOid);
diff --git a/src/backend/commands/user.c b/src/backend/commands/user.c
index 8b6543edee2..5af79792136 100644
--- a/src/backend/commands/user.c
+++ b/src/backend/commands/user.c
@@ -1234,8 +1234,7 @@ RenameRole(const char *oldname, const char *newname)
 	bool		isnull;
 	Datum		repl_val[Natts_pg_authid];
 	bool		repl_null[Natts_pg_authid];
-	bool		repl_repl[Natts_pg_authid];
-	int			i;
+	bool		repl_repl[Natts_pg_authid] = {0};
 	Oid			roleid;
 	ObjectAddress address;
 	Form_pg_authid authform;
@@ -1321,9 +1320,6 @@ RenameRole(const char *oldname, const char *newname)
 	}
 
 	/* OK, construct the modified tuple */
-	for (i = 0; i < Natts_pg_authid; i++)
-		repl_repl[i] = false;
-
 	repl_repl[Anum_pg_authid_rolname - 1] = true;
 	repl_val[Anum_pg_authid_rolname - 1] = DirectFunctionCall1(namein,
 															   CStringGetDatum(newname));
diff --git a/src/backend/statistics/extended_stats.c b/src/backend/statistics/extended_stats.c
index ab97e71dd79..93a9d3eeafb 100644
--- a/src/backend/statistics/extended_stats.c
+++ b/src/backend/statistics/extended_stats.c
@@ -2343,7 +2343,7 @@ serialize_expr_stats(AnlExprData *exprdata, int nexprs)
 		VacAttrStats *stats = exprdata[exprno].vacattrstat;
 
 		Datum		values[Natts_pg_statistic];
-		bool		nulls[Natts_pg_statistic];
+		bool		nulls[Natts_pg_statistic] = {0};
 		HeapTuple	stup;
 
 		if (!stats->stats_valid)
@@ -2359,10 +2359,6 @@ serialize_expr_stats(AnlExprData *exprdata, int nexprs)
 		/*
 		 * Construct a new pg_statistic tuple
 		 */
-		for (i = 0; i < Natts_pg_statistic; ++i)
-		{
-			nulls[i] = false;
-		}
 
 		values[Anum_pg_statistic_starelid - 1] = ObjectIdGetDatum(InvalidOid);
 		values[Anum_pg_statistic_staattnum - 1] = Int16GetDatum(InvalidAttrNumber);
diff --git a/src/backend/storage/smgr/md.c b/src/backend/storage/smgr/md.c
index 14b6fa0fd90..2d5cc97eaa9 100644
--- a/src/backend/storage/smgr/md.c
+++ b/src/backend/storage/smgr/md.c
@@ -557,8 +557,7 @@ void
 mdopen(SMgrRelation reln)
 {
 	/* mark it not open */
-	for (int forknum = 0; forknum <= MAX_FORKNUM; forknum++)
-		reln->md_num_open_segs[forknum] = 0;
+	memset(reln->md_num_open_segs, 0, (1+MAX_FORKNUM) * sizeof(*reln->md_num_open_segs));
 }
 
 /*
diff --git a/src/backend/tcop/pquery.c b/src/backend/tcop/pquery.c
index 52e2db6452b..53b712fd3b5 100644
--- a/src/backend/tcop/pquery.c
+++ b/src/backend/tcop/pquery.c
@@ -653,8 +653,7 @@ PortalSetResultFormat(Portal portal, int nFormats, int16 *formats)
 	else
 	{
 		/* use default format for all columns */
-		for (i = 0; i < natts; i++)
-			portal->formats[i] = 0;
+		memset(portal->formats, 0, natts * sizeof(*portal->formats));
 	}
 }
 
diff --git a/src/backend/utils/adt/arrayfuncs.c b/src/backend/utils/adt/arrayfuncs.c
index 495e449a9e9..ff62eb01dfd 100644
--- a/src/backend/utils/adt/arrayfuncs.c
+++ b/src/backend/utils/adt/arrayfuncs.c
@@ -1038,7 +1038,7 @@ array_out(PG_FUNCTION_ARGS)
 				i,
 				j,
 				k,
-				indx[MAXDIM];
+				indx[MAXDIM] = {0};
 	int			ndim,
 			   *dims,
 			   *lb;
@@ -1203,8 +1203,7 @@ array_out(PG_FUNCTION_ARGS)
 	if (needdims)
 		APPENDSTR(dims_str);
 	APPENDCHAR('{');
-	for (i = 0; i < ndim; i++)
-		indx[i] = 0;
+
 	j = 0;
 	k = 0;
 	do
@@ -4988,10 +4987,9 @@ array_slice_size(char *arraydataptr, bits8 *arraynullsptr,
 				span[MAXDIM],
 				prod[MAXDIM],
 				dist[MAXDIM],
-				indx[MAXDIM];
+				indx[MAXDIM] = {0};
 	char	   *ptr;
-	int			i,
-				j,
+	int			j,
 				inc;
 	int			count = 0;
 
@@ -5007,8 +5005,7 @@ array_slice_size(char *arraydataptr, bits8 *arraynullsptr,
 					 typlen, typbyval, typalign);
 	mda_get_prod(ndim, dim, prod);
 	mda_get_offset_values(ndim, dist, prod, span);
-	for (i = 0; i < ndim; i++)
-		indx[i] = 0;
+
 	j = ndim - 1;
 	do
 	{
@@ -5059,9 +5056,8 @@ array_extract_slice(ArrayType *newarray,
 				prod[MAXDIM],
 				span[MAXDIM],
 				dist[MAXDIM],
-				indx[MAXDIM];
-	int			i,
-				j,
+				indx[MAXDIM] = {0};
+	int			j,
 				inc;
 
 	src_offset = ArrayGetOffset(ndim, dim, lb, st);
@@ -5070,8 +5066,7 @@ array_extract_slice(ArrayType *newarray,
 	mda_get_prod(ndim, dim, prod);
 	mda_get_range(ndim, span, st, endp);
 	mda_get_offset_values(ndim, dist, prod, span);
-	for (i = 0; i < ndim; i++)
-		indx[i] = 0;
+
 	dest_offset = 0;
 	j = ndim - 1;
 	do
@@ -5138,9 +5133,8 @@ array_insert_slice(ArrayType *destArray,
 				prod[MAXDIM],
 				span[MAXDIM],
 				dist[MAXDIM],
-				indx[MAXDIM];
-	int			i,
-				j,
+				indx[MAXDIM] = {0};
+	int			j,
 				inc;
 
 	dest_offset = ArrayGetOffset(ndim, dim, lb, st);
@@ -5156,8 +5150,7 @@ array_insert_slice(ArrayType *destArray,
 	mda_get_prod(ndim, dim, prod);
 	mda_get_range(ndim, span, st, endp);
 	mda_get_offset_values(ndim, dist, prod, span);
-	for (i = 0; i < ndim; i++)
-		indx[i] = 0;
+
 	src_offset = 0;
 	j = ndim - 1;
 	do
diff --git a/src/test/isolation/isolationtester.c b/src/test/isolation/isolationtester.c
index 0a66235153a..19808c59892 100644
--- a/src/test/isolation/isolationtester.c
+++ b/src/test/isolation/isolationtester.c
@@ -432,8 +432,7 @@ run_all_permutations(TestSpec *testspec)
 	 * already picked from this pile.
 	 */
 	piles = pg_malloc(sizeof(int) * testspec->nsessions);
-	for (i = 0; i < testspec->nsessions; i++)
-		piles[i] = 0;
+	memset(piles, 0, sizeof(*piles) * testspec->nsessions);
 
 	run_all_permutations_recurse(testspec, piles, 0, stepptrs);
 
-- 
2.25.1

