v1-0002-TupleHashTable-remove-additional-pointer.patch

text/x-patch

Filename: v1-0002-TupleHashTable-remove-additional-pointer.patch
Type: text/x-patch
Part: 1
Message: Reduce TupleHashEntryData struct size by half

Patch

Format: format-patch
Series: patch v1-0002
Subject: TupleHashTable: remove "additional" pointer.
File+
src/backend/executor/execGrouping.c 10 4
From 1d7dfadc55e78915f724a3fc04ef56385d1a433b Mon Sep 17 00:00:00 2001
From: Jeff Davis <jeff@j-davis.com>
Date: Fri, 15 Nov 2024 18:33:14 -0800
Subject: [PATCH v1 2/5] TupleHashTable: remove "additional" pointer.

Store at the end of the MinimalTuple. A hack, but reduces the bucket
size substantially.
---
 src/backend/executor/execGrouping.c | 14 ++++++++++----
 1 file changed, 10 insertions(+), 4 deletions(-)

diff --git a/src/backend/executor/execGrouping.c b/src/backend/executor/execGrouping.c
index c6aa28b17e..40ba15ca54 100644
--- a/src/backend/executor/execGrouping.c
+++ b/src/backend/executor/execGrouping.c
@@ -23,7 +23,6 @@
 struct TupleHashEntryData
 {
 	MinimalTuple firstTuple;	/* copy of first tuple in this group */
-	void	   *additional;		/* user data */
 	uint32		status;			/* hash status */
 	uint32		hash;			/* hash value (cached) */
 };
@@ -380,13 +379,19 @@ TupleHashEntryGetTuple(TupleHashEntry entry)
 void *
 TupleHashEntryGetAdditional(TupleHashEntry entry)
 {
-	return entry->additional;
+	char	   *ptr = (char *) entry->firstTuple;
+	void	   *additional;
+
+	memcpy(&additional, ptr + entry->firstTuple->t_len, sizeof(void *));
+	return additional;
 }
 
 void
 TupleHashEntrySetAdditional(TupleHashEntry entry, void *additional)
 {
-	entry->additional = additional;
+	char	   *ptr = (char *) entry->firstTuple;
+
+	memcpy(ptr + entry->firstTuple->t_len, &additional, sizeof(void *));
 }
 
 /*
@@ -552,10 +557,11 @@ LookupTupleHashEntry_internal(TupleHashTable hashtable, TupleTableSlot *slot,
 			/* created new entry */
 			*isnew = true;
 			/* zero caller data */
-			entry->additional = NULL;
 			MemoryContextSwitchTo(hashtable->tablecxt);
 			/* Copy the first tuple into the table context */
 			entry->firstTuple = ExecCopySlotMinimalTuple(slot);
+			entry->firstTuple = repalloc(entry->firstTuple, entry->firstTuple->t_len + sizeof(void *));
+			memset((char *) entry->firstTuple + entry->firstTuple->t_len, 0, sizeof(void *));
 		}
 	}
 	else
-- 
2.34.1