v13-0005-fixup-use-simplehash-instead-of-dynahash.patch

application/x-patch

Filename: v13-0005-fixup-use-simplehash-instead-of-dynahash.patch
Type: application/x-patch
Part: 4
Message: Re: MultiXact\SLRU buffers configuration

Patch

Format: format-patch
Series: patch v13-0005
Subject: fixup: use simplehash instead of dynahash
File+
src/backend/access/transam/slru.c 27 14
src/include/access/slru.h 3 1
From ef3c1e45a1f869544280f65d1eeba8544b028735 Mon Sep 17 00:00:00 2001
From: Thomas Munro <thomas.munro@gmail.com>
Date: Sat, 27 Mar 2021 09:07:22 +1300
Subject: [PATCH v13 5/5] fixup: use simplehash instead of dynahash

---
 src/backend/access/transam/slru.c | 41 ++++++++++++++++++++-----------
 src/include/access/slru.h         |  4 ++-
 2 files changed, 30 insertions(+), 15 deletions(-)

diff --git a/src/backend/access/transam/slru.c b/src/backend/access/transam/slru.c
index f823c2a0c8..ba9c0efc81 100644
--- a/src/backend/access/transam/slru.c
+++ b/src/backend/access/transam/slru.c
@@ -54,12 +54,11 @@
 #include "access/slru.h"
 #include "access/transam.h"
 #include "access/xlog.h"
+#include "common/hashfn.h"
 #include "miscadmin.h"
 #include "pgstat.h"
 #include "storage/fd.h"
 #include "storage/shmem.h"
-#include "utils/dynahash.h"
-#include "utils/hsearch.h"
 
 #define SlruFileName(ctl, path, seg) \
 	snprintf(path, MAXPGPATH, "%s/%04X", (ctl)->Dir, seg)
@@ -85,8 +84,24 @@ typedef struct SlruMappingTableEntry
 {
 	int			pageno;
 	int			slotno;
+	char		status;
 } SlruMappingTableEntry;
 
+/* Instantiate specialized hash table routines. */
+#define SH_PREFIX smte
+#define SH_ELEMENT_TYPE SlruMappingTableEntry
+#define SH_KEY_TYPE int
+#define SH_KEY pageno
+#define SH_HASH_KEY(table, key) murmurhash32(key)
+#define SH_EQUAL(table, a, b) a == b
+#define SH_IS_EMPTY_KEY(table, pageno) pageno == -1
+#define SH_EMPTY_KEY(table) -1
+#define SH_DECLARE
+#define SH_DEFINE
+#define SH_SCOPE static inline
+#define SH_IN_PLACE
+#include "lib/simplehash.h"
+
 /*
  * Populate a file tag describing a segment file.  We only use the segment
  * number, since we can derive everything else we need by having separate
@@ -179,8 +194,7 @@ SimpleLruShmemSize(int nslots, int nlsns)
 	if (nlsns > 0)
 		sz += MAXALIGN(nslots * nlsns * sizeof(XLogRecPtr));	/* group_lsn[] */
 
-	return BUFFERALIGN(sz) + BLCKSZ * nslots +
-		hash_estimate_size(nslots, sizeof(SlruMappingTableEntry));
+	return BUFFERALIGN(sz) + BLCKSZ * nslots + smte_estimate_size(nslots);
 }
 
 /*
@@ -200,8 +214,7 @@ SimpleLruInit(SlruCtl ctl, const char *name, int nslots, int nlsns,
 			  SyncRequestHandler sync_handler)
 {
 	char		mapping_table_name[SHMEM_INDEX_KEYSIZE];
-	HASHCTL		mapping_table_info;
-	HTAB	   *mapping_table;
+	smte_hash  *mapping_table;
 	SlruShared	shared;
 	bool		found;
 
@@ -274,13 +287,13 @@ SimpleLruInit(SlruCtl ctl, const char *name, int nslots, int nlsns,
 		Assert(found);
 
 	/* Create or find the buffer mapping table. */
-	memset(&mapping_table_info, 0, sizeof(mapping_table_info));
-	mapping_table_info.keysize = sizeof(int);
-	mapping_table_info.entrysize = sizeof(SlruMappingTableEntry);
 	snprintf(mapping_table_name, sizeof(mapping_table_name),
 			 "%s Mapping Table", name);
-	mapping_table = ShmemInitHash(mapping_table_name, nslots, nslots,
-								  &mapping_table_info, HASH_ELEM | HASH_BLOBS);
+	mapping_table = ShmemInitStruct(mapping_table_name,
+									smte_estimate_size(nslots),
+									&found);
+	if (!found)
+		smte_create(mapping_table, nslots, NULL);
 
 	/*
 	 * Initialize the unshared control struct, including directory path. We
@@ -1658,7 +1671,7 @@ SlruMappingFind(SlruCtl ctl, int pageno)
 {
 	SlruMappingTableEntry *mapping;
 
-	mapping = hash_search(ctl->mapping_table, &pageno, HASH_FIND, NULL);
+	mapping = smte_lookup(ctl->mapping_table, pageno);
 	if (mapping)
 		return mapping->slotno;
 
@@ -1671,7 +1684,7 @@ SlruMappingAdd(SlruCtl ctl, int pageno, int slotno)
 	SlruMappingTableEntry *mapping;
 	bool		found PG_USED_FOR_ASSERTS_ONLY;
 
-	mapping = hash_search(ctl->mapping_table, &pageno, HASH_ENTER, &found);
+	mapping = smte_insert(ctl->mapping_table, pageno, &found);
 	mapping->slotno = slotno;
 
 	Assert(!found);
@@ -1682,7 +1695,7 @@ SlruMappingRemove(SlruCtl ctl, int pageno)
 {
 	bool		found PG_USED_FOR_ASSERTS_ONLY;
 
-	hash_search(ctl->mapping_table, &pageno, HASH_REMOVE, &found);
+	found = smte_delete(ctl->mapping_table, pageno);
 
 	Assert(found);
 }
diff --git a/src/include/access/slru.h b/src/include/access/slru.h
index 8aa3efc0ee..b3d28ff135 100644
--- a/src/include/access/slru.h
+++ b/src/include/access/slru.h
@@ -104,6 +104,8 @@ typedef struct SlruSharedData
 
 typedef SlruSharedData *SlruShared;
 
+struct smte_hash;
+
 /*
  * SlruCtlData is an unshared structure that points to the active information
  * in shared memory.
@@ -111,7 +113,7 @@ typedef SlruSharedData *SlruShared;
 typedef struct SlruCtlData
 {
 	SlruShared	shared;
-	HTAB	   *mapping_table;
+	struct smte_hash *mapping_table;
 
 	/*
 	 * Which sync handler function to use when handing sync requests over to
-- 
2.30.1