pg.ddx.io  pgsql-hackers@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Jeff Davis <pgsql@j-davis.com>
To: John Naylor <johncnaylorls@gmail.com>
Cc: Andres Freund <andres@anarazel.de>
Cc: Tom Lane <tgl@sss.pgh.pa.us>
Cc: Gurjeet Singh <gurjeet@singh.im>
Cc: pgsql-hackers@postgresql.org
Subject: Re: Change GUC hashtable to use simplehash?
Date: Mon, 18 Dec 2023 23:32:30 -0800
Message-ID: <3820f030fd008ff14134b3e9ce5cc6dd623ed479.camel@j-davis.com> (raw)
In-Reply-To: <CANWCAZYqKBU3zfrXGXcYEuapvpB4E3DMgXABk6mCvaWW7x8P6w@mail.gmail.com>
References: <c95e1821b6a69ec0c40eb0252e037b68a62cdae3.camel@j-davis.com>
	<20231117232712.mpnzsho7ydssg2oc@awork3.anarazel.de>
	<7fee9f31c2e82948fa56fe57d68eecb0cce36f10.camel@j-davis.com>
	<20231118001003.n2d7tvdgeckun2y6@awork3.anarazel.de>
	<27a7a289d5b8f42e1b1e79b1bcaeef3a40583bd2.camel@j-davis.com>
	<CANWCAZY7Cr-GdUhrCLoR4+JGLChTb0pQxq9ZPi1KTLs+_KDFqg@mail.gmail.com>
	<20231122205030.v6yllmmj6dgxpg5f@awork3.anarazel.de>
	<2330615.1700686581@sss.pgh.pa.us>
	<20231122212221.ahmlio2yfx7jla6z@awork3.anarazel.de>
	<2334462.1700688476@sss.pgh.pa.us>
	<20231122223432.lywt4yz2bn7tlp27@awork3.anarazel.de>
	<CANWCAZYDNjTh=8FxgZKmS8HEE=wcmLMoJx+Vvg3+xgAaGY=2oQ@mail.gmail.com>
	<CANWCAZb_17xdPP7v1_mXXg5TVbWkDzkP_WDgbCWpqUvz_VZ+OQ@mail.gmail.com>
	<40718ab2acf2af7a495a33f6f6f07b876b49635d.camel@j-davis.com>
	<CANWCAZaJrQ9esBQuQ46YoXMeAm0eRqyED2SMJMUxfCOoDKyrnA@mail.gmail.com>
	<b40292c99e623defe5eadedab1d438cf51a4107c.camel@j-davis.com>
	<CANWCAZYGzFrLHWoWUQ3-d4WfjECXa=02ya_htGbfz_M_qNtA=w@mail.gmail.com>
	<CANWCAZYqKBU3zfrXGXcYEuapvpB4E3DMgXABk6mCvaWW7x8P6w@mail.gmail.com>

On Mon, 2023-12-18 at 13:39 +0700, John Naylor wrote:
> For now just two:
> v10-0002 is Jeff's change to the search path cache, but with the
> chunked interface that I found to be faster.

Did you consider specializing for the case of an aligned pointer? If
it's a string (c string or byte string) it's almost always going to be
aligned, right?

I hacked up a patch (attached). I lost track of which benchmark we're
using to test the performance, but when I test in a loop it seems
substantially faster.

It reads past the NUL byte, but only to the next alignment boundary,
which I think is OK (though I think I'd need to fix the patch for when
maxalign < 8).

Regards,
	Jeff Davis

Attachments:

v10jd-0004-Optimize-hash-function-further.patch (text/x-patch, 3.0K) download From 055d5cc24404584fd98109fabdcf83348e5c49b4 Mon Sep 17 00:00:00 2001 From: Jeff Davis <jeff@j-davis.com> Date: Mon, 18 Dec 2023 16:44:27 -0800 Subject: [PATCH v10jd] Optimize hash function further. --- src/backend/catalog/namespace.c | 46 +++++++++++++++++++++++++--- src/include/common/hashfn_unstable.h | 9 ++++++ 2 files changed, 51 insertions(+), 4 deletions(-) diff --git a/src/backend/catalog/namespace.c b/src/backend/catalog/namespace.c index c23f46aca3..368a7fabec 100644 --- a/src/backend/catalog/namespace.c +++ b/src/backend/catalog/namespace.c @@ -245,15 +245,44 @@ static bool MatchNamedCall(HeapTuple proctup, int nargs, List *argnames, * offers a more convenient API. */ +/* From: https://graphics.stanford.edu/~seander/bithacks.html#ZeroInWord */ +#define haszero64(v) \ + (((v) - 0x0101010101010101UL) & ~(v) & 0x8080808080808080UL) + static inline uint32 -spcachekey_hash(SearchPathCacheKey key) +cstring_hash_aligned(const char *str, uint64 seed) +{ + const char *const start = str; + const char *buf = start; + int chunk_len = 0; + fasthash_state hs; + + fasthash_init(&hs, FH_UNKNOWN_LENGTH, seed); + + Assert(PointerIsAligned(start, uint64)); + while (!haszero64(*(uint64 *)buf)) + { + fasthash_accum64(&hs, buf); + buf += sizeof(uint64); + } + + while (buf[chunk_len] != '\0') + chunk_len++; + fasthash_accum(&hs, buf, chunk_len); + buf += chunk_len; + + return fasthash_final32(&hs, buf - start); +} + +static inline uint32 +cstring_hash_unaligned(const char *str, uint64 seed) { - const char *const start = key.searchPath; - const char *buf = key.searchPath; + const char *const start = str; + const char *buf = str; fasthash_state hs; /* WIP: maybe roleid should be mixed in normally */ - fasthash_init(&hs, FH_UNKNOWN_LENGTH, key.roleid); + fasthash_init(&hs, FH_UNKNOWN_LENGTH, seed); while (*buf) { int chunk_len = 0; @@ -269,6 +298,15 @@ spcachekey_hash(SearchPathCacheKey key) return fasthash_final32(&hs, buf - start); } +static inline uint32 +spcachekey_hash(SearchPathCacheKey key) +{ + if (PointerIsAligned(key.searchPath, uint64)) + return cstring_hash_aligned(key.searchPath, key.roleid); + else + return cstring_hash_unaligned(key.searchPath, key.roleid); +} + static inline bool spcachekey_equal(SearchPathCacheKey a, SearchPathCacheKey b) { diff --git a/src/include/common/hashfn_unstable.h b/src/include/common/hashfn_unstable.h index bf1dbee28d..553fab0415 100644 --- a/src/include/common/hashfn_unstable.h +++ b/src/include/common/hashfn_unstable.h @@ -105,6 +105,15 @@ fasthash_combine(fasthash_state *hs) hs->accum = 0; } +/* Accumulate 8 bytes from aligned pointer and combine it into the hash */ +static inline void +fasthash_accum64(fasthash_state *hs, const char *ptr) +{ + Assert(PointerIsAligned(ptr, uint64)); + hs->accum = *(uint64 *)ptr; + fasthash_combine(hs); +} + /* Accumulate up to 8 bytes of input and combine it into the hash */ static inline void fasthash_accum(fasthash_state *hs, const char *k, int len) -- 2.34.1

view thread (103+ messages)  latest in thread

Message-ID: <3820f030fd008ff14134b3e9ce5cc6dd623ed479.camel@j-davis.com>
Permalink:  ../3820f030fd008ff14134b3e9ce5cc6dd623ed479.camel@j-davis.com/
Also on:    postgresql.org/message-id/3820f030fd008ff14134b3e9ce5cc6dd623ed479.camel@j-davis.com

 ·  · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-hackers@postgresql.org
  Cc: pgsql@j-davis.com, johncnaylorls@gmail.com, andres@anarazel.de, tgl@sss.pgh.pa.us, gurjeet@singh.im
  Subject: Re: Change GUC hashtable to use simplehash?
  In-Reply-To: <3820f030fd008ff14134b3e9ce5cc6dd623ed479.camel@j-davis.com>
  References: <c95e1821b6a69ec0c40eb0252e037b68a62cdae3.camel@j-davis.com> <27a7a289d5b8f42e1b1e79b1bcaeef3a40583bd2.camel@j-davis.com> <CANWCAZY7Cr-GdUhrCLoR4+JGLChTb0pQxq9ZPi1KTLs+_KDFqg@mail.gmail.com> <20231122205030.v6yllmmj6dgxpg5f@awork3.anarazel.de> <2330615.1700686581@sss.pgh.pa.us> <20231122212221.ahmlio2yfx7jla6z@awork3.anarazel.de> <2334462.1700688476@sss.pgh.pa.us> <20231122223432.lywt4yz2bn7tlp27@awork3.anarazel.de> <CANWCAZYDNjTh=8FxgZKmS8HEE=wcmLMoJx+Vvg3+xgAaGY=2oQ@mail.gmail.com> <CANWCAZb_17xdPP7v1_mXXg5TVbWkDzkP_WDgbCWpqUvz_VZ+OQ@mail.gmail.com> <40718ab2acf2af7a495a33f6f6f07b876b49635d.camel@j-davis.com> <CANWCAZaJrQ9esBQuQ46YoXMeAm0eRqyED2SMJMUxfCOoDKyrnA@mail.gmail.com> <b40292c99e623defe5eadedab1d438cf51a4107c.camel@j-davis.com> <CANWCAZYGzFrLHWoWUQ3-d4WfjECXa=02ya_htGbfz_M_qNtA=w@mail.gmail.com> <CANWCAZYqKBU3zfrXGXcYEuapvpB4E3DMgXABk6mCvaWW7x8P6w@mail.gmail.com> <3820f030fd008ff14134b3e9ce5cc6dd623ed479.camel@j-davis.com>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox