v2-0006-Convert-macros-to-static-inline-functions-tupmacs.patch

text/plain

Filename: v2-0006-Convert-macros-to-static-inline-functions-tupmacs.patch
Type: text/plain
Part: 5
Message: Re: Convert macros to static inline functions

Patch

Format: format-patch
Series: patch v2-0006
Subject: Convert macros to static inline functions (tupmacs.h)
File+
src/backend/access/gist/gistutil.c 8 8
src/include/access/itup.h 1 1
src/include/access/tupmacs.h 61 100
From 41cfab80cb71453ee48bebe914ce3814d8696ec1 Mon Sep 17 00:00:00 2001
From: Peter Eisentraut <peter@eisentraut.org>
Date: Mon, 23 May 2022 07:28:13 +0200
Subject: [PATCH v2] Convert macros to static inline functions (tupmacs.h)

XXX The renaming in gistutil.c is unnecessary if the subsequent itup.h
patch is also included.

Discussion: https://www.postgresql.org/message-id/flat/5b558da8-99fb-0a99-83dd-f72f05388517%40enterprisedb.com
---
 src/backend/access/gist/gistutil.c |  16 +--
 src/include/access/itup.h          |   2 +-
 src/include/access/tupmacs.h       | 161 +++++++++++------------------
 3 files changed, 70 insertions(+), 109 deletions(-)

diff --git a/src/backend/access/gist/gistutil.c b/src/backend/access/gist/gistutil.c
index d4bf0c7563..24b2c493e7 100644
--- a/src/backend/access/gist/gistutil.c
+++ b/src/backend/access/gist/gistutil.c
@@ -667,7 +667,7 @@ HeapTuple
 gistFetchTuple(GISTSTATE *giststate, Relation r, IndexTuple tuple)
 {
 	MemoryContext oldcxt = MemoryContextSwitchTo(giststate->tempCxt);
-	Datum		fetchatt[INDEX_MAX_KEYS];
+	Datum		att[INDEX_MAX_KEYS];
 	bool		isnull[INDEX_MAX_KEYS];
 	int			i;
 
@@ -680,9 +680,9 @@ gistFetchTuple(GISTSTATE *giststate, Relation r, IndexTuple tuple)
 		if (giststate->fetchFn[i].fn_oid != InvalidOid)
 		{
 			if (!isnull[i])
-				fetchatt[i] = gistFetchAtt(giststate, i, datum, r);
+				att[i] = gistFetchAtt(giststate, i, datum, r);
 			else
-				fetchatt[i] = (Datum) 0;
+				att[i] = (Datum) 0;
 		}
 		else if (giststate->compressFn[i].fn_oid == InvalidOid)
 		{
@@ -691,9 +691,9 @@ gistFetchTuple(GISTSTATE *giststate, Relation r, IndexTuple tuple)
 			 * original value, att is necessarily stored in original form.
 			 */
 			if (!isnull[i])
-				fetchatt[i] = datum;
+				att[i] = datum;
 			else
-				fetchatt[i] = (Datum) 0;
+				att[i] = (Datum) 0;
 		}
 		else
 		{
@@ -703,7 +703,7 @@ gistFetchTuple(GISTSTATE *giststate, Relation r, IndexTuple tuple)
 			 * in this column, and we can replace it with a NULL.
 			 */
 			isnull[i] = true;
-			fetchatt[i] = (Datum) 0;
+			att[i] = (Datum) 0;
 		}
 	}
 
@@ -712,12 +712,12 @@ gistFetchTuple(GISTSTATE *giststate, Relation r, IndexTuple tuple)
 	 */
 	for (; i < r->rd_att->natts; i++)
 	{
-		fetchatt[i] = index_getattr(tuple, i + 1, giststate->leafTupdesc,
+		att[i] = index_getattr(tuple, i + 1, giststate->leafTupdesc,
 									&isnull[i]);
 	}
 	MemoryContextSwitchTo(oldcxt);
 
-	return heap_form_tuple(giststate->fetchTupdesc, fetchatt, isnull);
+	return heap_form_tuple(giststate->fetchTupdesc, att, isnull);
 }
 
 float
diff --git a/src/include/access/itup.h b/src/include/access/itup.h
index 2c8877e991..6e6aec1043 100644
--- a/src/include/access/itup.h
+++ b/src/include/access/itup.h
@@ -114,7 +114,7 @@ typedef IndexAttributeBitMapData * IndexAttributeBitMap;
 	) \
 	: \
 	( \
-		(att_isnull((attnum)-1, (char *)(tup) + sizeof(IndexTupleData))) ? \
+		(att_isnull((attnum)-1, (bits8 *)(tup) + sizeof(IndexTupleData))) ? \
 		( \
 			*(isnull) = true, \
 			(Datum)NULL \
diff --git a/src/include/access/tupmacs.h b/src/include/access/tupmacs.h
index 16c74a581e..3f6e9d146f 100644
--- a/src/include/access/tupmacs.h
+++ b/src/include/access/tupmacs.h
@@ -14,6 +14,7 @@
 #ifndef TUPMACS_H
 #define TUPMACS_H
 
+#include "catalog/pg_attribute.h"
 #include "catalog/pg_type_d.h"	/* for TYPALIGN macros */
 
 
@@ -22,10 +23,14 @@
  * Note that a 0 in the null bitmap indicates a null, while 1 indicates
  * non-null.
  */
-#define att_isnull(ATT, BITS) (!((BITS)[(ATT) >> 3] & (1 << ((ATT) & 0x07))))
+static inline bool
+att_isnull(int ATT, const bits8 *BITS)
+{
+	return !(BITS[ATT >> 3] & (1 << (ATT & 0x07)));
+}
 
 /*
- * Given a Form_pg_attribute and a pointer into a tuple's data area,
+ * Given byval/len parameters and a pointer into a tuple's data area,
  * return the correct value or pointer.
  *
  * We return a Datum value in all cases.  If the attribute has "byval" false,
@@ -38,61 +43,40 @@
  *
  * Note that T must already be properly aligned for this to work correctly.
  */
-#define fetchatt(A,T) fetch_att(T, (A)->attbyval, (A)->attlen)
+static inline Datum
+fetch_att(const void *T, bool attbyval, int attlen)
+{
+	if (attbyval)
+	{
+		switch (attlen)
+		{
+			case sizeof(char):
+				return CharGetDatum(*((const char *) T));
+			case sizeof(int16):
+				return Int16GetDatum(*((const int16 *) T));
+			case sizeof(int32):
+				return Int32GetDatum(*((const int32 *) T));
+#if SIZEOF_DATUM == 8
+			case sizeof(Datum):
+				return *((const Datum *) T);
+#endif
+			default:
+				elog(ERROR, "unsupported byval length: %d", attlen);
+				return 0;
+		}
+	}
+	else
+		return PointerGetDatum(T);
+}
 
 /*
- * Same, but work from byval/len parameters rather than Form_pg_attribute.
+ * Same, but work from a Form_pg_attribute rather than byval/len parameters.
  */
-#if SIZEOF_DATUM == 8
-
-#define fetch_att(T,attbyval,attlen) \
-( \
-	(attbyval) ? \
-	( \
-		(attlen) == (int) sizeof(Datum) ? \
-			*((Datum *)(T)) \
-		: \
-	  ( \
-		(attlen) == (int) sizeof(int32) ? \
-			Int32GetDatum(*((int32 *)(T))) \
-		: \
-		( \
-			(attlen) == (int) sizeof(int16) ? \
-				Int16GetDatum(*((int16 *)(T))) \
-			: \
-			( \
-				AssertMacro((attlen) == 1), \
-				CharGetDatum(*((char *)(T))) \
-			) \
-		) \
-	  ) \
-	) \
-	: \
-	PointerGetDatum((char *) (T)) \
-)
-#else							/* SIZEOF_DATUM != 8 */
-
-#define fetch_att(T,attbyval,attlen) \
-( \
-	(attbyval) ? \
-	( \
-		(attlen) == (int) sizeof(int32) ? \
-			Int32GetDatum(*((int32 *)(T))) \
-		: \
-		( \
-			(attlen) == (int) sizeof(int16) ? \
-				Int16GetDatum(*((int16 *)(T))) \
-			: \
-			( \
-				AssertMacro((attlen) == 1), \
-				CharGetDatum(*((char *)(T))) \
-			) \
-		) \
-	) \
-	: \
-	PointerGetDatum((char *) (T)) \
-)
-#endif							/* SIZEOF_DATUM == 8 */
+static inline Datum
+fetchatt(const FormData_pg_attribute *A, const void *T)
+{
+	return fetch_att(T, A->attbyval, A->attlen);
+}
 
 /*
  * att_align_datum aligns the given offset as needed for a datum of alignment
@@ -194,54 +178,31 @@
  * store_att_byval is a partial inverse of fetch_att: store a given Datum
  * value into a tuple data area at the specified address.  However, it only
  * handles the byval case, because in typical usage the caller needs to
- * distinguish by-val and by-ref cases anyway, and so a do-it-all macro
+ * distinguish by-val and by-ref cases anyway, and so a do-it-all function
  * wouldn't be convenient.
  */
+static inline void
+store_att_byval(void *T, Datum newdatum, int attlen)
+{
+	switch (attlen)
+	{
+		case sizeof(char):
+			*(char *) T = DatumGetChar(newdatum);
+			break;
+		case sizeof(int16):
+			*(int16 *) T = DatumGetInt16(newdatum);
+			break;
+		case sizeof(int32):
+			*(int32 *) T = DatumGetInt32(newdatum);
+			break;
 #if SIZEOF_DATUM == 8
-
-#define store_att_byval(T,newdatum,attlen) \
-	do { \
-		switch (attlen) \
-		{ \
-			case sizeof(char): \
-				*(char *) (T) = DatumGetChar(newdatum); \
-				break; \
-			case sizeof(int16): \
-				*(int16 *) (T) = DatumGetInt16(newdatum); \
-				break; \
-			case sizeof(int32): \
-				*(int32 *) (T) = DatumGetInt32(newdatum); \
-				break; \
-			case sizeof(Datum): \
-				*(Datum *) (T) = (newdatum); \
-				break; \
-			default: \
-				elog(ERROR, "unsupported byval length: %d", \
-					 (int) (attlen)); \
-				break; \
-		} \
-	} while (0)
-#else							/* SIZEOF_DATUM != 8 */
-
-#define store_att_byval(T,newdatum,attlen) \
-	do { \
-		switch (attlen) \
-		{ \
-			case sizeof(char): \
-				*(char *) (T) = DatumGetChar(newdatum); \
-				break; \
-			case sizeof(int16): \
-				*(int16 *) (T) = DatumGetInt16(newdatum); \
-				break; \
-			case sizeof(int32): \
-				*(int32 *) (T) = DatumGetInt32(newdatum); \
-				break; \
-			default: \
-				elog(ERROR, "unsupported byval length: %d", \
-					 (int) (attlen)); \
-				break; \
-		} \
-	} while (0)
-#endif							/* SIZEOF_DATUM == 8 */
+		case sizeof(Datum):
+			*(Datum *) T = newdatum;
+			break;
+#endif
+		default:
+			elog(ERROR, "unsupported byval length: %d", attlen);
+	}
+}
 
 #endif
-- 
2.36.1