From e01df35e8a2558b626bfbff861ff520ee5134b70 Mon Sep 17 00:00:00 2001 From: James Coleman Date: Thu, 30 Apr 2020 21:36:07 -0400 Subject: [PATCH v2 1/3] Summarize trade-offs between simplehash and dynahash When reading the code it's not obvious when one should prefer dynahash over simplehash and vice-versa, so, for programmer-friendliness, add comments to inform that decision. --- src/backend/utils/hash/dynahash.c | 12 +++++++++++- src/include/lib/simplehash.h | 25 +++++++++++++++++++++---- 2 files changed, 32 insertions(+), 5 deletions(-) diff --git a/src/backend/utils/hash/dynahash.c b/src/backend/utils/hash/dynahash.c index 5948b01abc..f4fbccdd7e 100644 --- a/src/backend/utils/hash/dynahash.c +++ b/src/backend/utils/hash/dynahash.c @@ -1,7 +1,7 @@ /*------------------------------------------------------------------------- * * dynahash.c - * dynamic hash tables + * dynamic chained hash tables * * dynahash.c supports both local-to-a-backend hash tables and hash tables in * shared memory. For shared hash tables, it is the caller's responsibility @@ -41,6 +41,16 @@ * function must be supplied; comparison defaults to memcmp() and key copying * to memcpy() when a user-defined hashing function is selected. * + * Compared to simplehash, dynahash has the following benefits: + * + * - It supports partitioning, which is useful for shared memory access using + * locks. + * - Shared memory hashes are allocated in a fixed size area at startup and + * are discoverable by name from other processes. + * - Because entries don't need to be moved in the case of hash conflicts, has + * better performance for large entries + * - Guarantees stable pointers to entries. + * * Portions Copyright (c) 1996-2020, PostgreSQL Global Development Group * Portions Copyright (c) 1994, Regents of the University of California * diff --git a/src/include/lib/simplehash.h b/src/include/lib/simplehash.h index 90dfa8a695..b0472a3fc7 100644 --- a/src/include/lib/simplehash.h +++ b/src/include/lib/simplehash.h @@ -1,10 +1,27 @@ /* * simplehash.h * - * Hash table implementation which will be specialized to user-defined - * types, by including this file to generate the required code. It's - * probably not worthwhile to do so for hash tables that aren't performance - * or space sensitive. + * When included this file generates a "templated" (by way of macros) + * open-addressing hash table implementation specialized to user-defined + * types. + * + * It's probably not worthwhile to generate such a specialized implementation + * for hash tables that aren't performance or space sensitive. + * + * Compared to dynahash, simplehash has the following benefits: + * + * - Due to the "templated" code generation has known structure sizes and no + * indirect function calls (which show up substantially in dynahash + * profiles). These features considerably increase speed for small + * entries. + * - Open addressing has better CPU cache behavior than dynahash's chained + * hashtables. + * - The generated interface is type-safe and easier to use than dynahash, + * though at the cost of more complex setup. + * - Allocates memory in a MemoryContext or another allocator with a + * malloc/free style interface (which isn't easily usable in a shared + * memory context) + * - Does not require the overhead of a separate memory context. * * Usage notes: * -- 2.20.1 (Apple Git-117)