public inbox for [email protected]help / color / mirror / Atom feed
[PATCH v7 3/3] Optimize hash function 48+ messages / 2 participants [nested] [flat]
* [PATCH v7 3/3] Optimize hash function @ 2021-01-20 22:06 Heikki Linnakangas <[email protected]> 0 siblings, 0 replies; 48+ messages in thread From: Heikki Linnakangas @ 2021-01-20 22:06 UTC (permalink / raw) --- src/backend/utils/resowner/resowner.c | 24 ++++++++++++++++++------ src/include/common/hashfn.h | 15 +++++++++++++++ 2 files changed, 33 insertions(+), 6 deletions(-) diff --git a/src/backend/utils/resowner/resowner.c b/src/backend/utils/resowner/resowner.c index d62b1495dd3..6189d051072 100644 --- a/src/backend/utils/resowner/resowner.c +++ b/src/backend/utils/resowner/resowner.c @@ -163,15 +163,27 @@ static void ReleaseAuxProcessResourcesCallback(int code, Datum arg); * INTERNAL ROUTINES * *****************************************************************************/ +/* + * Hash function for value+kind combination. + */ static inline uint32 hash_resource_elem(Datum value, ResourceOwnerFuncs *kind) { - Datum data[2]; - - data[0] = value; - data[1] = PointerGetDatum(kind); - - return hash_bytes((unsigned char *) &data, 2 * SIZEOF_DATUM); + /* + * Most resources types store a pointer in 'value', and pointers are + * unique all on their own. But some resources store plain integers (Files + * and Buffers as of this writing), so we want to incorporate the 'kind' + * in the hash too, otherwise those resources will collide a lot. But + * because there are only a few resource kinds like that - and only a few + * resource kinds to begin with - we don't need to work too hard to mix + * 'kind' into the hash. Just add it with hash_combine(), it perturbs the + * result enough for our purposes. + */ +#if SIZEOF_DATUM == 8 + return hash_combine64(murmurhash64((uint64) value), (uint64) kind); +#else + return hash_combine(murmurhash32((uint32) value), (uint32) kind); +#endif } static void diff --git a/src/include/common/hashfn.h b/src/include/common/hashfn.h index c634cc067a1..009ffbbdd38 100644 --- a/src/include/common/hashfn.h +++ b/src/include/common/hashfn.h @@ -101,4 +101,19 @@ murmurhash32(uint32 data) return h; } +/* 64-bit variant */ +static inline uint64 +murmurhash64(uint64 data) +{ + uint64 h = data; + + h ^= h >> 33; + h *= 0xff51afd7ed558ccd; + h ^= h >> 33; + h *= 0xc4ceb9fe1a85ec53; + h ^= h >> 33; + + return h; +} + #endif /* HASHFN_H */ -- 2.30.1 --------------C6A03312D0F99F9183A0DF46-- ^ permalink raw reply [nested|flat] 48+ messages in thread
* [PATCH v6 3/3] Optimize hash function @ 2021-01-20 22:06 Heikki Linnakangas <[email protected]> 0 siblings, 0 replies; 48+ messages in thread From: Heikki Linnakangas @ 2021-01-20 22:06 UTC (permalink / raw) --- src/backend/utils/resowner/resowner.c | 24 ++++++++++++++++++------ src/include/common/hashfn.h | 15 +++++++++++++++ 2 files changed, 33 insertions(+), 6 deletions(-) diff --git a/src/backend/utils/resowner/resowner.c b/src/backend/utils/resowner/resowner.c index 1db1e7cf14b..72e4d8a784c 100644 --- a/src/backend/utils/resowner/resowner.c +++ b/src/backend/utils/resowner/resowner.c @@ -163,15 +163,27 @@ static void ReleaseAuxProcessResourcesCallback(int code, Datum arg); * INTERNAL ROUTINES * *****************************************************************************/ +/* + * Hash function for value+kind combination. + */ static inline uint32 hash_resource_elem(Datum value, ResourceOwnerFuncs *kind) { - Datum data[2]; - - data[0] = value; - data[1] = PointerGetDatum(kind); - - return hash_bytes((unsigned char *) &data, 2 * SIZEOF_DATUM); + /* + * Most resources types store a pointer in 'value', and pointers are + * unique all on their own. But some resources store plain integers (Files + * and Buffers as of this writing), so we want to incorporate the 'kind' + * in the hash too, otherwise those resources will collide a lot. But + * because there are only a few resource kinds like that - and only a few + * resource kinds to begin with - we don't need to work too hard to mix + * 'kind' into the hash. Just add it with hash_combine(), it perturbs the + * result enough for our purposes. + */ +#if SIZEOF_DATUM == 8 + return hash_combine64(murmurhash64((uint64) value), (uint64) kind); +#else + return hash_combine(murmurhash32((uint32) value), (uint32) kind); +#endif } static void diff --git a/src/include/common/hashfn.h b/src/include/common/hashfn.h index c634cc067a1..009ffbbdd38 100644 --- a/src/include/common/hashfn.h +++ b/src/include/common/hashfn.h @@ -101,4 +101,19 @@ murmurhash32(uint32 data) return h; } +/* 64-bit variant */ +static inline uint64 +murmurhash64(uint64 data) +{ + uint64 h = data; + + h ^= h >> 33; + h *= 0xff51afd7ed558ccd; + h ^= h >> 33; + h *= 0xc4ceb9fe1a85ec53; + h ^= h >> 33; + + return h; +} + #endif /* HASHFN_H */ -- 2.29.2 --------------F30A5443A5D524A5CBC51AE7 Content-Type: text/x-patch; charset=UTF-8; name="v2-0001-resownerbench.patch" Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename="v2-0001-resownerbench.patch" ^ permalink raw reply [nested|flat] 48+ messages in thread
* [PATCH v7 3/3] Optimize hash function @ 2021-01-20 22:06 Heikki Linnakangas <[email protected]> 0 siblings, 0 replies; 48+ messages in thread From: Heikki Linnakangas @ 2021-01-20 22:06 UTC (permalink / raw) --- src/backend/utils/resowner/resowner.c | 24 ++++++++++++++++++------ src/include/common/hashfn.h | 15 +++++++++++++++ 2 files changed, 33 insertions(+), 6 deletions(-) diff --git a/src/backend/utils/resowner/resowner.c b/src/backend/utils/resowner/resowner.c index d62b1495dd3..6189d051072 100644 --- a/src/backend/utils/resowner/resowner.c +++ b/src/backend/utils/resowner/resowner.c @@ -163,15 +163,27 @@ static void ReleaseAuxProcessResourcesCallback(int code, Datum arg); * INTERNAL ROUTINES * *****************************************************************************/ +/* + * Hash function for value+kind combination. + */ static inline uint32 hash_resource_elem(Datum value, ResourceOwnerFuncs *kind) { - Datum data[2]; - - data[0] = value; - data[1] = PointerGetDatum(kind); - - return hash_bytes((unsigned char *) &data, 2 * SIZEOF_DATUM); + /* + * Most resources types store a pointer in 'value', and pointers are + * unique all on their own. But some resources store plain integers (Files + * and Buffers as of this writing), so we want to incorporate the 'kind' + * in the hash too, otherwise those resources will collide a lot. But + * because there are only a few resource kinds like that - and only a few + * resource kinds to begin with - we don't need to work too hard to mix + * 'kind' into the hash. Just add it with hash_combine(), it perturbs the + * result enough for our purposes. + */ +#if SIZEOF_DATUM == 8 + return hash_combine64(murmurhash64((uint64) value), (uint64) kind); +#else + return hash_combine(murmurhash32((uint32) value), (uint32) kind); +#endif } static void diff --git a/src/include/common/hashfn.h b/src/include/common/hashfn.h index c634cc067a1..009ffbbdd38 100644 --- a/src/include/common/hashfn.h +++ b/src/include/common/hashfn.h @@ -101,4 +101,19 @@ murmurhash32(uint32 data) return h; } +/* 64-bit variant */ +static inline uint64 +murmurhash64(uint64 data) +{ + uint64 h = data; + + h ^= h >> 33; + h *= 0xff51afd7ed558ccd; + h ^= h >> 33; + h *= 0xc4ceb9fe1a85ec53; + h ^= h >> 33; + + return h; +} + #endif /* HASHFN_H */ -- 2.30.1 --------------C6A03312D0F99F9183A0DF46-- ^ permalink raw reply [nested|flat] 48+ messages in thread
* [PATCH v8 3/3] Optimize hash function @ 2021-01-20 22:06 Heikki Linnakangas <[email protected]> 0 siblings, 0 replies; 48+ messages in thread From: Heikki Linnakangas @ 2021-01-20 22:06 UTC (permalink / raw) --- src/backend/utils/resowner/resowner.c | 24 ++++++++++++++++++------ src/include/common/hashfn.h | 15 +++++++++++++++ 2 files changed, 33 insertions(+), 6 deletions(-) diff --git a/src/backend/utils/resowner/resowner.c b/src/backend/utils/resowner/resowner.c index d62b1495dd3..6189d051072 100644 --- a/src/backend/utils/resowner/resowner.c +++ b/src/backend/utils/resowner/resowner.c @@ -163,15 +163,27 @@ static void ReleaseAuxProcessResourcesCallback(int code, Datum arg); * INTERNAL ROUTINES * *****************************************************************************/ +/* + * Hash function for value+kind combination. + */ static inline uint32 hash_resource_elem(Datum value, ResourceOwnerFuncs *kind) { - Datum data[2]; - - data[0] = value; - data[1] = PointerGetDatum(kind); - - return hash_bytes((unsigned char *) &data, 2 * SIZEOF_DATUM); + /* + * Most resources types store a pointer in 'value', and pointers are + * unique all on their own. But some resources store plain integers (Files + * and Buffers as of this writing), so we want to incorporate the 'kind' + * in the hash too, otherwise those resources will collide a lot. But + * because there are only a few resource kinds like that - and only a few + * resource kinds to begin with - we don't need to work too hard to mix + * 'kind' into the hash. Just add it with hash_combine(), it perturbs the + * result enough for our purposes. + */ +#if SIZEOF_DATUM == 8 + return hash_combine64(murmurhash64((uint64) value), (uint64) kind); +#else + return hash_combine(murmurhash32((uint32) value), (uint32) kind); +#endif } static void diff --git a/src/include/common/hashfn.h b/src/include/common/hashfn.h index c634cc067a1..009ffbbdd38 100644 --- a/src/include/common/hashfn.h +++ b/src/include/common/hashfn.h @@ -101,4 +101,19 @@ murmurhash32(uint32 data) return h; } +/* 64-bit variant */ +static inline uint64 +murmurhash64(uint64 data) +{ + uint64 h = data; + + h ^= h >> 33; + h *= 0xff51afd7ed558ccd; + h ^= h >> 33; + h *= 0xc4ceb9fe1a85ec53; + h ^= h >> 33; + + return h; +} + #endif /* HASHFN_H */ -- 2.30.2 --------------846F221A8A016B7AA8E567B3-- ^ permalink raw reply [nested|flat] 48+ messages in thread
* [PATCH v7 3/3] Optimize hash function @ 2021-01-20 22:06 Heikki Linnakangas <[email protected]> 0 siblings, 0 replies; 48+ messages in thread From: Heikki Linnakangas @ 2021-01-20 22:06 UTC (permalink / raw) --- src/backend/utils/resowner/resowner.c | 24 ++++++++++++++++++------ src/include/common/hashfn.h | 15 +++++++++++++++ 2 files changed, 33 insertions(+), 6 deletions(-) diff --git a/src/backend/utils/resowner/resowner.c b/src/backend/utils/resowner/resowner.c index d62b1495dd3..6189d051072 100644 --- a/src/backend/utils/resowner/resowner.c +++ b/src/backend/utils/resowner/resowner.c @@ -163,15 +163,27 @@ static void ReleaseAuxProcessResourcesCallback(int code, Datum arg); * INTERNAL ROUTINES * *****************************************************************************/ +/* + * Hash function for value+kind combination. + */ static inline uint32 hash_resource_elem(Datum value, ResourceOwnerFuncs *kind) { - Datum data[2]; - - data[0] = value; - data[1] = PointerGetDatum(kind); - - return hash_bytes((unsigned char *) &data, 2 * SIZEOF_DATUM); + /* + * Most resources types store a pointer in 'value', and pointers are + * unique all on their own. But some resources store plain integers (Files + * and Buffers as of this writing), so we want to incorporate the 'kind' + * in the hash too, otherwise those resources will collide a lot. But + * because there are only a few resource kinds like that - and only a few + * resource kinds to begin with - we don't need to work too hard to mix + * 'kind' into the hash. Just add it with hash_combine(), it perturbs the + * result enough for our purposes. + */ +#if SIZEOF_DATUM == 8 + return hash_combine64(murmurhash64((uint64) value), (uint64) kind); +#else + return hash_combine(murmurhash32((uint32) value), (uint32) kind); +#endif } static void diff --git a/src/include/common/hashfn.h b/src/include/common/hashfn.h index c634cc067a1..009ffbbdd38 100644 --- a/src/include/common/hashfn.h +++ b/src/include/common/hashfn.h @@ -101,4 +101,19 @@ murmurhash32(uint32 data) return h; } +/* 64-bit variant */ +static inline uint64 +murmurhash64(uint64 data) +{ + uint64 h = data; + + h ^= h >> 33; + h *= 0xff51afd7ed558ccd; + h ^= h >> 33; + h *= 0xc4ceb9fe1a85ec53; + h ^= h >> 33; + + return h; +} + #endif /* HASHFN_H */ -- 2.30.1 --------------C6A03312D0F99F9183A0DF46-- ^ permalink raw reply [nested|flat] 48+ messages in thread
* [PATCH v6 3/3] Optimize hash function @ 2021-01-20 22:06 Heikki Linnakangas <[email protected]> 0 siblings, 0 replies; 48+ messages in thread From: Heikki Linnakangas @ 2021-01-20 22:06 UTC (permalink / raw) --- src/backend/utils/resowner/resowner.c | 24 ++++++++++++++++++------ src/include/common/hashfn.h | 15 +++++++++++++++ 2 files changed, 33 insertions(+), 6 deletions(-) diff --git a/src/backend/utils/resowner/resowner.c b/src/backend/utils/resowner/resowner.c index 1db1e7cf14b..72e4d8a784c 100644 --- a/src/backend/utils/resowner/resowner.c +++ b/src/backend/utils/resowner/resowner.c @@ -163,15 +163,27 @@ static void ReleaseAuxProcessResourcesCallback(int code, Datum arg); * INTERNAL ROUTINES * *****************************************************************************/ +/* + * Hash function for value+kind combination. + */ static inline uint32 hash_resource_elem(Datum value, ResourceOwnerFuncs *kind) { - Datum data[2]; - - data[0] = value; - data[1] = PointerGetDatum(kind); - - return hash_bytes((unsigned char *) &data, 2 * SIZEOF_DATUM); + /* + * Most resources types store a pointer in 'value', and pointers are + * unique all on their own. But some resources store plain integers (Files + * and Buffers as of this writing), so we want to incorporate the 'kind' + * in the hash too, otherwise those resources will collide a lot. But + * because there are only a few resource kinds like that - and only a few + * resource kinds to begin with - we don't need to work too hard to mix + * 'kind' into the hash. Just add it with hash_combine(), it perturbs the + * result enough for our purposes. + */ +#if SIZEOF_DATUM == 8 + return hash_combine64(murmurhash64((uint64) value), (uint64) kind); +#else + return hash_combine(murmurhash32((uint32) value), (uint32) kind); +#endif } static void diff --git a/src/include/common/hashfn.h b/src/include/common/hashfn.h index c634cc067a1..009ffbbdd38 100644 --- a/src/include/common/hashfn.h +++ b/src/include/common/hashfn.h @@ -101,4 +101,19 @@ murmurhash32(uint32 data) return h; } +/* 64-bit variant */ +static inline uint64 +murmurhash64(uint64 data) +{ + uint64 h = data; + + h ^= h >> 33; + h *= 0xff51afd7ed558ccd; + h ^= h >> 33; + h *= 0xc4ceb9fe1a85ec53; + h ^= h >> 33; + + return h; +} + #endif /* HASHFN_H */ -- 2.29.2 --------------F30A5443A5D524A5CBC51AE7 Content-Type: text/x-patch; charset=UTF-8; name="v2-0001-resownerbench.patch" Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename="v2-0001-resownerbench.patch" ^ permalink raw reply [nested|flat] 48+ messages in thread
* [PATCH v7 3/3] Optimize hash function @ 2021-01-20 22:06 Heikki Linnakangas <[email protected]> 0 siblings, 0 replies; 48+ messages in thread From: Heikki Linnakangas @ 2021-01-20 22:06 UTC (permalink / raw) --- src/backend/utils/resowner/resowner.c | 24 ++++++++++++++++++------ src/include/common/hashfn.h | 15 +++++++++++++++ 2 files changed, 33 insertions(+), 6 deletions(-) diff --git a/src/backend/utils/resowner/resowner.c b/src/backend/utils/resowner/resowner.c index d62b1495dd3..6189d051072 100644 --- a/src/backend/utils/resowner/resowner.c +++ b/src/backend/utils/resowner/resowner.c @@ -163,15 +163,27 @@ static void ReleaseAuxProcessResourcesCallback(int code, Datum arg); * INTERNAL ROUTINES * *****************************************************************************/ +/* + * Hash function for value+kind combination. + */ static inline uint32 hash_resource_elem(Datum value, ResourceOwnerFuncs *kind) { - Datum data[2]; - - data[0] = value; - data[1] = PointerGetDatum(kind); - - return hash_bytes((unsigned char *) &data, 2 * SIZEOF_DATUM); + /* + * Most resources types store a pointer in 'value', and pointers are + * unique all on their own. But some resources store plain integers (Files + * and Buffers as of this writing), so we want to incorporate the 'kind' + * in the hash too, otherwise those resources will collide a lot. But + * because there are only a few resource kinds like that - and only a few + * resource kinds to begin with - we don't need to work too hard to mix + * 'kind' into the hash. Just add it with hash_combine(), it perturbs the + * result enough for our purposes. + */ +#if SIZEOF_DATUM == 8 + return hash_combine64(murmurhash64((uint64) value), (uint64) kind); +#else + return hash_combine(murmurhash32((uint32) value), (uint32) kind); +#endif } static void diff --git a/src/include/common/hashfn.h b/src/include/common/hashfn.h index c634cc067a1..009ffbbdd38 100644 --- a/src/include/common/hashfn.h +++ b/src/include/common/hashfn.h @@ -101,4 +101,19 @@ murmurhash32(uint32 data) return h; } +/* 64-bit variant */ +static inline uint64 +murmurhash64(uint64 data) +{ + uint64 h = data; + + h ^= h >> 33; + h *= 0xff51afd7ed558ccd; + h ^= h >> 33; + h *= 0xc4ceb9fe1a85ec53; + h ^= h >> 33; + + return h; +} + #endif /* HASHFN_H */ -- 2.30.1 --------------C6A03312D0F99F9183A0DF46-- ^ permalink raw reply [nested|flat] 48+ messages in thread
* [PATCH v6 3/3] Optimize hash function @ 2021-01-20 22:06 Heikki Linnakangas <[email protected]> 0 siblings, 0 replies; 48+ messages in thread From: Heikki Linnakangas @ 2021-01-20 22:06 UTC (permalink / raw) --- src/backend/utils/resowner/resowner.c | 24 ++++++++++++++++++------ src/include/common/hashfn.h | 15 +++++++++++++++ 2 files changed, 33 insertions(+), 6 deletions(-) diff --git a/src/backend/utils/resowner/resowner.c b/src/backend/utils/resowner/resowner.c index 1db1e7cf14b..72e4d8a784c 100644 --- a/src/backend/utils/resowner/resowner.c +++ b/src/backend/utils/resowner/resowner.c @@ -163,15 +163,27 @@ static void ReleaseAuxProcessResourcesCallback(int code, Datum arg); * INTERNAL ROUTINES * *****************************************************************************/ +/* + * Hash function for value+kind combination. + */ static inline uint32 hash_resource_elem(Datum value, ResourceOwnerFuncs *kind) { - Datum data[2]; - - data[0] = value; - data[1] = PointerGetDatum(kind); - - return hash_bytes((unsigned char *) &data, 2 * SIZEOF_DATUM); + /* + * Most resources types store a pointer in 'value', and pointers are + * unique all on their own. But some resources store plain integers (Files + * and Buffers as of this writing), so we want to incorporate the 'kind' + * in the hash too, otherwise those resources will collide a lot. But + * because there are only a few resource kinds like that - and only a few + * resource kinds to begin with - we don't need to work too hard to mix + * 'kind' into the hash. Just add it with hash_combine(), it perturbs the + * result enough for our purposes. + */ +#if SIZEOF_DATUM == 8 + return hash_combine64(murmurhash64((uint64) value), (uint64) kind); +#else + return hash_combine(murmurhash32((uint32) value), (uint32) kind); +#endif } static void diff --git a/src/include/common/hashfn.h b/src/include/common/hashfn.h index c634cc067a1..009ffbbdd38 100644 --- a/src/include/common/hashfn.h +++ b/src/include/common/hashfn.h @@ -101,4 +101,19 @@ murmurhash32(uint32 data) return h; } +/* 64-bit variant */ +static inline uint64 +murmurhash64(uint64 data) +{ + uint64 h = data; + + h ^= h >> 33; + h *= 0xff51afd7ed558ccd; + h ^= h >> 33; + h *= 0xc4ceb9fe1a85ec53; + h ^= h >> 33; + + return h; +} + #endif /* HASHFN_H */ -- 2.29.2 --------------F30A5443A5D524A5CBC51AE7 Content-Type: text/x-patch; charset=UTF-8; name="v2-0001-resownerbench.patch" Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename="v2-0001-resownerbench.patch" ^ permalink raw reply [nested|flat] 48+ messages in thread
* [PATCH v7 3/3] Optimize hash function @ 2021-01-20 22:06 Heikki Linnakangas <[email protected]> 0 siblings, 0 replies; 48+ messages in thread From: Heikki Linnakangas @ 2021-01-20 22:06 UTC (permalink / raw) --- src/backend/utils/resowner/resowner.c | 24 ++++++++++++++++++------ src/include/common/hashfn.h | 15 +++++++++++++++ 2 files changed, 33 insertions(+), 6 deletions(-) diff --git a/src/backend/utils/resowner/resowner.c b/src/backend/utils/resowner/resowner.c index d62b1495dd3..6189d051072 100644 --- a/src/backend/utils/resowner/resowner.c +++ b/src/backend/utils/resowner/resowner.c @@ -163,15 +163,27 @@ static void ReleaseAuxProcessResourcesCallback(int code, Datum arg); * INTERNAL ROUTINES * *****************************************************************************/ +/* + * Hash function for value+kind combination. + */ static inline uint32 hash_resource_elem(Datum value, ResourceOwnerFuncs *kind) { - Datum data[2]; - - data[0] = value; - data[1] = PointerGetDatum(kind); - - return hash_bytes((unsigned char *) &data, 2 * SIZEOF_DATUM); + /* + * Most resources types store a pointer in 'value', and pointers are + * unique all on their own. But some resources store plain integers (Files + * and Buffers as of this writing), so we want to incorporate the 'kind' + * in the hash too, otherwise those resources will collide a lot. But + * because there are only a few resource kinds like that - and only a few + * resource kinds to begin with - we don't need to work too hard to mix + * 'kind' into the hash. Just add it with hash_combine(), it perturbs the + * result enough for our purposes. + */ +#if SIZEOF_DATUM == 8 + return hash_combine64(murmurhash64((uint64) value), (uint64) kind); +#else + return hash_combine(murmurhash32((uint32) value), (uint32) kind); +#endif } static void diff --git a/src/include/common/hashfn.h b/src/include/common/hashfn.h index c634cc067a1..009ffbbdd38 100644 --- a/src/include/common/hashfn.h +++ b/src/include/common/hashfn.h @@ -101,4 +101,19 @@ murmurhash32(uint32 data) return h; } +/* 64-bit variant */ +static inline uint64 +murmurhash64(uint64 data) +{ + uint64 h = data; + + h ^= h >> 33; + h *= 0xff51afd7ed558ccd; + h ^= h >> 33; + h *= 0xc4ceb9fe1a85ec53; + h ^= h >> 33; + + return h; +} + #endif /* HASHFN_H */ -- 2.30.1 --------------C6A03312D0F99F9183A0DF46-- ^ permalink raw reply [nested|flat] 48+ messages in thread
* [PATCH v6 3/3] Optimize hash function @ 2021-01-20 22:06 Heikki Linnakangas <[email protected]> 0 siblings, 0 replies; 48+ messages in thread From: Heikki Linnakangas @ 2021-01-20 22:06 UTC (permalink / raw) --- src/backend/utils/resowner/resowner.c | 24 ++++++++++++++++++------ src/include/common/hashfn.h | 15 +++++++++++++++ 2 files changed, 33 insertions(+), 6 deletions(-) diff --git a/src/backend/utils/resowner/resowner.c b/src/backend/utils/resowner/resowner.c index 1db1e7cf14b..72e4d8a784c 100644 --- a/src/backend/utils/resowner/resowner.c +++ b/src/backend/utils/resowner/resowner.c @@ -163,15 +163,27 @@ static void ReleaseAuxProcessResourcesCallback(int code, Datum arg); * INTERNAL ROUTINES * *****************************************************************************/ +/* + * Hash function for value+kind combination. + */ static inline uint32 hash_resource_elem(Datum value, ResourceOwnerFuncs *kind) { - Datum data[2]; - - data[0] = value; - data[1] = PointerGetDatum(kind); - - return hash_bytes((unsigned char *) &data, 2 * SIZEOF_DATUM); + /* + * Most resources types store a pointer in 'value', and pointers are + * unique all on their own. But some resources store plain integers (Files + * and Buffers as of this writing), so we want to incorporate the 'kind' + * in the hash too, otherwise those resources will collide a lot. But + * because there are only a few resource kinds like that - and only a few + * resource kinds to begin with - we don't need to work too hard to mix + * 'kind' into the hash. Just add it with hash_combine(), it perturbs the + * result enough for our purposes. + */ +#if SIZEOF_DATUM == 8 + return hash_combine64(murmurhash64((uint64) value), (uint64) kind); +#else + return hash_combine(murmurhash32((uint32) value), (uint32) kind); +#endif } static void diff --git a/src/include/common/hashfn.h b/src/include/common/hashfn.h index c634cc067a1..009ffbbdd38 100644 --- a/src/include/common/hashfn.h +++ b/src/include/common/hashfn.h @@ -101,4 +101,19 @@ murmurhash32(uint32 data) return h; } +/* 64-bit variant */ +static inline uint64 +murmurhash64(uint64 data) +{ + uint64 h = data; + + h ^= h >> 33; + h *= 0xff51afd7ed558ccd; + h ^= h >> 33; + h *= 0xc4ceb9fe1a85ec53; + h ^= h >> 33; + + return h; +} + #endif /* HASHFN_H */ -- 2.29.2 --------------F30A5443A5D524A5CBC51AE7 Content-Type: text/x-patch; charset=UTF-8; name="v2-0001-resownerbench.patch" Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename="v2-0001-resownerbench.patch" ^ permalink raw reply [nested|flat] 48+ messages in thread
* [PATCH v7 3/3] Optimize hash function @ 2021-01-20 22:06 Heikki Linnakangas <[email protected]> 0 siblings, 0 replies; 48+ messages in thread From: Heikki Linnakangas @ 2021-01-20 22:06 UTC (permalink / raw) --- src/backend/utils/resowner/resowner.c | 24 ++++++++++++++++++------ src/include/common/hashfn.h | 15 +++++++++++++++ 2 files changed, 33 insertions(+), 6 deletions(-) diff --git a/src/backend/utils/resowner/resowner.c b/src/backend/utils/resowner/resowner.c index d62b1495dd3..6189d051072 100644 --- a/src/backend/utils/resowner/resowner.c +++ b/src/backend/utils/resowner/resowner.c @@ -163,15 +163,27 @@ static void ReleaseAuxProcessResourcesCallback(int code, Datum arg); * INTERNAL ROUTINES * *****************************************************************************/ +/* + * Hash function for value+kind combination. + */ static inline uint32 hash_resource_elem(Datum value, ResourceOwnerFuncs *kind) { - Datum data[2]; - - data[0] = value; - data[1] = PointerGetDatum(kind); - - return hash_bytes((unsigned char *) &data, 2 * SIZEOF_DATUM); + /* + * Most resources types store a pointer in 'value', and pointers are + * unique all on their own. But some resources store plain integers (Files + * and Buffers as of this writing), so we want to incorporate the 'kind' + * in the hash too, otherwise those resources will collide a lot. But + * because there are only a few resource kinds like that - and only a few + * resource kinds to begin with - we don't need to work too hard to mix + * 'kind' into the hash. Just add it with hash_combine(), it perturbs the + * result enough for our purposes. + */ +#if SIZEOF_DATUM == 8 + return hash_combine64(murmurhash64((uint64) value), (uint64) kind); +#else + return hash_combine(murmurhash32((uint32) value), (uint32) kind); +#endif } static void diff --git a/src/include/common/hashfn.h b/src/include/common/hashfn.h index c634cc067a1..009ffbbdd38 100644 --- a/src/include/common/hashfn.h +++ b/src/include/common/hashfn.h @@ -101,4 +101,19 @@ murmurhash32(uint32 data) return h; } +/* 64-bit variant */ +static inline uint64 +murmurhash64(uint64 data) +{ + uint64 h = data; + + h ^= h >> 33; + h *= 0xff51afd7ed558ccd; + h ^= h >> 33; + h *= 0xc4ceb9fe1a85ec53; + h ^= h >> 33; + + return h; +} + #endif /* HASHFN_H */ -- 2.30.1 --------------C6A03312D0F99F9183A0DF46-- ^ permalink raw reply [nested|flat] 48+ messages in thread
* [PATCH v6 3/3] Optimize hash function @ 2021-01-20 22:06 Heikki Linnakangas <[email protected]> 0 siblings, 0 replies; 48+ messages in thread From: Heikki Linnakangas @ 2021-01-20 22:06 UTC (permalink / raw) --- src/backend/utils/resowner/resowner.c | 24 ++++++++++++++++++------ src/include/common/hashfn.h | 15 +++++++++++++++ 2 files changed, 33 insertions(+), 6 deletions(-) diff --git a/src/backend/utils/resowner/resowner.c b/src/backend/utils/resowner/resowner.c index 1db1e7cf14b..72e4d8a784c 100644 --- a/src/backend/utils/resowner/resowner.c +++ b/src/backend/utils/resowner/resowner.c @@ -163,15 +163,27 @@ static void ReleaseAuxProcessResourcesCallback(int code, Datum arg); * INTERNAL ROUTINES * *****************************************************************************/ +/* + * Hash function for value+kind combination. + */ static inline uint32 hash_resource_elem(Datum value, ResourceOwnerFuncs *kind) { - Datum data[2]; - - data[0] = value; - data[1] = PointerGetDatum(kind); - - return hash_bytes((unsigned char *) &data, 2 * SIZEOF_DATUM); + /* + * Most resources types store a pointer in 'value', and pointers are + * unique all on their own. But some resources store plain integers (Files + * and Buffers as of this writing), so we want to incorporate the 'kind' + * in the hash too, otherwise those resources will collide a lot. But + * because there are only a few resource kinds like that - and only a few + * resource kinds to begin with - we don't need to work too hard to mix + * 'kind' into the hash. Just add it with hash_combine(), it perturbs the + * result enough for our purposes. + */ +#if SIZEOF_DATUM == 8 + return hash_combine64(murmurhash64((uint64) value), (uint64) kind); +#else + return hash_combine(murmurhash32((uint32) value), (uint32) kind); +#endif } static void diff --git a/src/include/common/hashfn.h b/src/include/common/hashfn.h index c634cc067a1..009ffbbdd38 100644 --- a/src/include/common/hashfn.h +++ b/src/include/common/hashfn.h @@ -101,4 +101,19 @@ murmurhash32(uint32 data) return h; } +/* 64-bit variant */ +static inline uint64 +murmurhash64(uint64 data) +{ + uint64 h = data; + + h ^= h >> 33; + h *= 0xff51afd7ed558ccd; + h ^= h >> 33; + h *= 0xc4ceb9fe1a85ec53; + h ^= h >> 33; + + return h; +} + #endif /* HASHFN_H */ -- 2.29.2 --------------F30A5443A5D524A5CBC51AE7 Content-Type: text/x-patch; charset=UTF-8; name="v2-0001-resownerbench.patch" Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename="v2-0001-resownerbench.patch" ^ permalink raw reply [nested|flat] 48+ messages in thread
* [PATCH v7 3/3] Optimize hash function @ 2021-01-20 22:06 Heikki Linnakangas <[email protected]> 0 siblings, 0 replies; 48+ messages in thread From: Heikki Linnakangas @ 2021-01-20 22:06 UTC (permalink / raw) --- src/backend/utils/resowner/resowner.c | 24 ++++++++++++++++++------ src/include/common/hashfn.h | 15 +++++++++++++++ 2 files changed, 33 insertions(+), 6 deletions(-) diff --git a/src/backend/utils/resowner/resowner.c b/src/backend/utils/resowner/resowner.c index d62b1495dd3..6189d051072 100644 --- a/src/backend/utils/resowner/resowner.c +++ b/src/backend/utils/resowner/resowner.c @@ -163,15 +163,27 @@ static void ReleaseAuxProcessResourcesCallback(int code, Datum arg); * INTERNAL ROUTINES * *****************************************************************************/ +/* + * Hash function for value+kind combination. + */ static inline uint32 hash_resource_elem(Datum value, ResourceOwnerFuncs *kind) { - Datum data[2]; - - data[0] = value; - data[1] = PointerGetDatum(kind); - - return hash_bytes((unsigned char *) &data, 2 * SIZEOF_DATUM); + /* + * Most resources types store a pointer in 'value', and pointers are + * unique all on their own. But some resources store plain integers (Files + * and Buffers as of this writing), so we want to incorporate the 'kind' + * in the hash too, otherwise those resources will collide a lot. But + * because there are only a few resource kinds like that - and only a few + * resource kinds to begin with - we don't need to work too hard to mix + * 'kind' into the hash. Just add it with hash_combine(), it perturbs the + * result enough for our purposes. + */ +#if SIZEOF_DATUM == 8 + return hash_combine64(murmurhash64((uint64) value), (uint64) kind); +#else + return hash_combine(murmurhash32((uint32) value), (uint32) kind); +#endif } static void diff --git a/src/include/common/hashfn.h b/src/include/common/hashfn.h index c634cc067a1..009ffbbdd38 100644 --- a/src/include/common/hashfn.h +++ b/src/include/common/hashfn.h @@ -101,4 +101,19 @@ murmurhash32(uint32 data) return h; } +/* 64-bit variant */ +static inline uint64 +murmurhash64(uint64 data) +{ + uint64 h = data; + + h ^= h >> 33; + h *= 0xff51afd7ed558ccd; + h ^= h >> 33; + h *= 0xc4ceb9fe1a85ec53; + h ^= h >> 33; + + return h; +} + #endif /* HASHFN_H */ -- 2.30.1 --------------C6A03312D0F99F9183A0DF46-- ^ permalink raw reply [nested|flat] 48+ messages in thread
* [PATCH v6 3/3] Optimize hash function @ 2021-01-20 22:06 Heikki Linnakangas <[email protected]> 0 siblings, 0 replies; 48+ messages in thread From: Heikki Linnakangas @ 2021-01-20 22:06 UTC (permalink / raw) --- src/backend/utils/resowner/resowner.c | 24 ++++++++++++++++++------ src/include/common/hashfn.h | 15 +++++++++++++++ 2 files changed, 33 insertions(+), 6 deletions(-) diff --git a/src/backend/utils/resowner/resowner.c b/src/backend/utils/resowner/resowner.c index 1db1e7cf14b..72e4d8a784c 100644 --- a/src/backend/utils/resowner/resowner.c +++ b/src/backend/utils/resowner/resowner.c @@ -163,15 +163,27 @@ static void ReleaseAuxProcessResourcesCallback(int code, Datum arg); * INTERNAL ROUTINES * *****************************************************************************/ +/* + * Hash function for value+kind combination. + */ static inline uint32 hash_resource_elem(Datum value, ResourceOwnerFuncs *kind) { - Datum data[2]; - - data[0] = value; - data[1] = PointerGetDatum(kind); - - return hash_bytes((unsigned char *) &data, 2 * SIZEOF_DATUM); + /* + * Most resources types store a pointer in 'value', and pointers are + * unique all on their own. But some resources store plain integers (Files + * and Buffers as of this writing), so we want to incorporate the 'kind' + * in the hash too, otherwise those resources will collide a lot. But + * because there are only a few resource kinds like that - and only a few + * resource kinds to begin with - we don't need to work too hard to mix + * 'kind' into the hash. Just add it with hash_combine(), it perturbs the + * result enough for our purposes. + */ +#if SIZEOF_DATUM == 8 + return hash_combine64(murmurhash64((uint64) value), (uint64) kind); +#else + return hash_combine(murmurhash32((uint32) value), (uint32) kind); +#endif } static void diff --git a/src/include/common/hashfn.h b/src/include/common/hashfn.h index c634cc067a1..009ffbbdd38 100644 --- a/src/include/common/hashfn.h +++ b/src/include/common/hashfn.h @@ -101,4 +101,19 @@ murmurhash32(uint32 data) return h; } +/* 64-bit variant */ +static inline uint64 +murmurhash64(uint64 data) +{ + uint64 h = data; + + h ^= h >> 33; + h *= 0xff51afd7ed558ccd; + h ^= h >> 33; + h *= 0xc4ceb9fe1a85ec53; + h ^= h >> 33; + + return h; +} + #endif /* HASHFN_H */ -- 2.29.2 --------------F30A5443A5D524A5CBC51AE7 Content-Type: text/x-patch; charset=UTF-8; name="v2-0001-resownerbench.patch" Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename="v2-0001-resownerbench.patch" ^ permalink raw reply [nested|flat] 48+ messages in thread
* [PATCH v7 3/3] Optimize hash function @ 2021-01-20 22:06 Heikki Linnakangas <[email protected]> 0 siblings, 0 replies; 48+ messages in thread From: Heikki Linnakangas @ 2021-01-20 22:06 UTC (permalink / raw) --- src/backend/utils/resowner/resowner.c | 24 ++++++++++++++++++------ src/include/common/hashfn.h | 15 +++++++++++++++ 2 files changed, 33 insertions(+), 6 deletions(-) diff --git a/src/backend/utils/resowner/resowner.c b/src/backend/utils/resowner/resowner.c index d62b1495dd3..6189d051072 100644 --- a/src/backend/utils/resowner/resowner.c +++ b/src/backend/utils/resowner/resowner.c @@ -163,15 +163,27 @@ static void ReleaseAuxProcessResourcesCallback(int code, Datum arg); * INTERNAL ROUTINES * *****************************************************************************/ +/* + * Hash function for value+kind combination. + */ static inline uint32 hash_resource_elem(Datum value, ResourceOwnerFuncs *kind) { - Datum data[2]; - - data[0] = value; - data[1] = PointerGetDatum(kind); - - return hash_bytes((unsigned char *) &data, 2 * SIZEOF_DATUM); + /* + * Most resources types store a pointer in 'value', and pointers are + * unique all on their own. But some resources store plain integers (Files + * and Buffers as of this writing), so we want to incorporate the 'kind' + * in the hash too, otherwise those resources will collide a lot. But + * because there are only a few resource kinds like that - and only a few + * resource kinds to begin with - we don't need to work too hard to mix + * 'kind' into the hash. Just add it with hash_combine(), it perturbs the + * result enough for our purposes. + */ +#if SIZEOF_DATUM == 8 + return hash_combine64(murmurhash64((uint64) value), (uint64) kind); +#else + return hash_combine(murmurhash32((uint32) value), (uint32) kind); +#endif } static void diff --git a/src/include/common/hashfn.h b/src/include/common/hashfn.h index c634cc067a1..009ffbbdd38 100644 --- a/src/include/common/hashfn.h +++ b/src/include/common/hashfn.h @@ -101,4 +101,19 @@ murmurhash32(uint32 data) return h; } +/* 64-bit variant */ +static inline uint64 +murmurhash64(uint64 data) +{ + uint64 h = data; + + h ^= h >> 33; + h *= 0xff51afd7ed558ccd; + h ^= h >> 33; + h *= 0xc4ceb9fe1a85ec53; + h ^= h >> 33; + + return h; +} + #endif /* HASHFN_H */ -- 2.30.1 --------------C6A03312D0F99F9183A0DF46-- ^ permalink raw reply [nested|flat] 48+ messages in thread
* [PATCH v6 3/3] Optimize hash function @ 2021-01-20 22:06 Heikki Linnakangas <[email protected]> 0 siblings, 0 replies; 48+ messages in thread From: Heikki Linnakangas @ 2021-01-20 22:06 UTC (permalink / raw) --- src/backend/utils/resowner/resowner.c | 24 ++++++++++++++++++------ src/include/common/hashfn.h | 15 +++++++++++++++ 2 files changed, 33 insertions(+), 6 deletions(-) diff --git a/src/backend/utils/resowner/resowner.c b/src/backend/utils/resowner/resowner.c index 1db1e7cf14b..72e4d8a784c 100644 --- a/src/backend/utils/resowner/resowner.c +++ b/src/backend/utils/resowner/resowner.c @@ -163,15 +163,27 @@ static void ReleaseAuxProcessResourcesCallback(int code, Datum arg); * INTERNAL ROUTINES * *****************************************************************************/ +/* + * Hash function for value+kind combination. + */ static inline uint32 hash_resource_elem(Datum value, ResourceOwnerFuncs *kind) { - Datum data[2]; - - data[0] = value; - data[1] = PointerGetDatum(kind); - - return hash_bytes((unsigned char *) &data, 2 * SIZEOF_DATUM); + /* + * Most resources types store a pointer in 'value', and pointers are + * unique all on their own. But some resources store plain integers (Files + * and Buffers as of this writing), so we want to incorporate the 'kind' + * in the hash too, otherwise those resources will collide a lot. But + * because there are only a few resource kinds like that - and only a few + * resource kinds to begin with - we don't need to work too hard to mix + * 'kind' into the hash. Just add it with hash_combine(), it perturbs the + * result enough for our purposes. + */ +#if SIZEOF_DATUM == 8 + return hash_combine64(murmurhash64((uint64) value), (uint64) kind); +#else + return hash_combine(murmurhash32((uint32) value), (uint32) kind); +#endif } static void diff --git a/src/include/common/hashfn.h b/src/include/common/hashfn.h index c634cc067a1..009ffbbdd38 100644 --- a/src/include/common/hashfn.h +++ b/src/include/common/hashfn.h @@ -101,4 +101,19 @@ murmurhash32(uint32 data) return h; } +/* 64-bit variant */ +static inline uint64 +murmurhash64(uint64 data) +{ + uint64 h = data; + + h ^= h >> 33; + h *= 0xff51afd7ed558ccd; + h ^= h >> 33; + h *= 0xc4ceb9fe1a85ec53; + h ^= h >> 33; + + return h; +} + #endif /* HASHFN_H */ -- 2.29.2 --------------F30A5443A5D524A5CBC51AE7 Content-Type: text/x-patch; charset=UTF-8; name="v2-0001-resownerbench.patch" Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename="v2-0001-resownerbench.patch" ^ permalink raw reply [nested|flat] 48+ messages in thread
* [PATCH v7 3/3] Optimize hash function @ 2021-01-20 22:06 Heikki Linnakangas <[email protected]> 0 siblings, 0 replies; 48+ messages in thread From: Heikki Linnakangas @ 2021-01-20 22:06 UTC (permalink / raw) --- src/backend/utils/resowner/resowner.c | 24 ++++++++++++++++++------ src/include/common/hashfn.h | 15 +++++++++++++++ 2 files changed, 33 insertions(+), 6 deletions(-) diff --git a/src/backend/utils/resowner/resowner.c b/src/backend/utils/resowner/resowner.c index d62b1495dd3..6189d051072 100644 --- a/src/backend/utils/resowner/resowner.c +++ b/src/backend/utils/resowner/resowner.c @@ -163,15 +163,27 @@ static void ReleaseAuxProcessResourcesCallback(int code, Datum arg); * INTERNAL ROUTINES * *****************************************************************************/ +/* + * Hash function for value+kind combination. + */ static inline uint32 hash_resource_elem(Datum value, ResourceOwnerFuncs *kind) { - Datum data[2]; - - data[0] = value; - data[1] = PointerGetDatum(kind); - - return hash_bytes((unsigned char *) &data, 2 * SIZEOF_DATUM); + /* + * Most resources types store a pointer in 'value', and pointers are + * unique all on their own. But some resources store plain integers (Files + * and Buffers as of this writing), so we want to incorporate the 'kind' + * in the hash too, otherwise those resources will collide a lot. But + * because there are only a few resource kinds like that - and only a few + * resource kinds to begin with - we don't need to work too hard to mix + * 'kind' into the hash. Just add it with hash_combine(), it perturbs the + * result enough for our purposes. + */ +#if SIZEOF_DATUM == 8 + return hash_combine64(murmurhash64((uint64) value), (uint64) kind); +#else + return hash_combine(murmurhash32((uint32) value), (uint32) kind); +#endif } static void diff --git a/src/include/common/hashfn.h b/src/include/common/hashfn.h index c634cc067a1..009ffbbdd38 100644 --- a/src/include/common/hashfn.h +++ b/src/include/common/hashfn.h @@ -101,4 +101,19 @@ murmurhash32(uint32 data) return h; } +/* 64-bit variant */ +static inline uint64 +murmurhash64(uint64 data) +{ + uint64 h = data; + + h ^= h >> 33; + h *= 0xff51afd7ed558ccd; + h ^= h >> 33; + h *= 0xc4ceb9fe1a85ec53; + h ^= h >> 33; + + return h; +} + #endif /* HASHFN_H */ -- 2.30.1 --------------C6A03312D0F99F9183A0DF46-- ^ permalink raw reply [nested|flat] 48+ messages in thread
* [PATCH v6 3/3] Optimize hash function @ 2021-01-20 22:06 Heikki Linnakangas <[email protected]> 0 siblings, 0 replies; 48+ messages in thread From: Heikki Linnakangas @ 2021-01-20 22:06 UTC (permalink / raw) --- src/backend/utils/resowner/resowner.c | 24 ++++++++++++++++++------ src/include/common/hashfn.h | 15 +++++++++++++++ 2 files changed, 33 insertions(+), 6 deletions(-) diff --git a/src/backend/utils/resowner/resowner.c b/src/backend/utils/resowner/resowner.c index 1db1e7cf14b..72e4d8a784c 100644 --- a/src/backend/utils/resowner/resowner.c +++ b/src/backend/utils/resowner/resowner.c @@ -163,15 +163,27 @@ static void ReleaseAuxProcessResourcesCallback(int code, Datum arg); * INTERNAL ROUTINES * *****************************************************************************/ +/* + * Hash function for value+kind combination. + */ static inline uint32 hash_resource_elem(Datum value, ResourceOwnerFuncs *kind) { - Datum data[2]; - - data[0] = value; - data[1] = PointerGetDatum(kind); - - return hash_bytes((unsigned char *) &data, 2 * SIZEOF_DATUM); + /* + * Most resources types store a pointer in 'value', and pointers are + * unique all on their own. But some resources store plain integers (Files + * and Buffers as of this writing), so we want to incorporate the 'kind' + * in the hash too, otherwise those resources will collide a lot. But + * because there are only a few resource kinds like that - and only a few + * resource kinds to begin with - we don't need to work too hard to mix + * 'kind' into the hash. Just add it with hash_combine(), it perturbs the + * result enough for our purposes. + */ +#if SIZEOF_DATUM == 8 + return hash_combine64(murmurhash64((uint64) value), (uint64) kind); +#else + return hash_combine(murmurhash32((uint32) value), (uint32) kind); +#endif } static void diff --git a/src/include/common/hashfn.h b/src/include/common/hashfn.h index c634cc067a1..009ffbbdd38 100644 --- a/src/include/common/hashfn.h +++ b/src/include/common/hashfn.h @@ -101,4 +101,19 @@ murmurhash32(uint32 data) return h; } +/* 64-bit variant */ +static inline uint64 +murmurhash64(uint64 data) +{ + uint64 h = data; + + h ^= h >> 33; + h *= 0xff51afd7ed558ccd; + h ^= h >> 33; + h *= 0xc4ceb9fe1a85ec53; + h ^= h >> 33; + + return h; +} + #endif /* HASHFN_H */ -- 2.29.2 --------------F30A5443A5D524A5CBC51AE7 Content-Type: text/x-patch; charset=UTF-8; name="v2-0001-resownerbench.patch" Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename="v2-0001-resownerbench.patch" ^ permalink raw reply [nested|flat] 48+ messages in thread
* [PATCH v7 3/3] Optimize hash function @ 2021-01-20 22:06 Heikki Linnakangas <[email protected]> 0 siblings, 0 replies; 48+ messages in thread From: Heikki Linnakangas @ 2021-01-20 22:06 UTC (permalink / raw) --- src/backend/utils/resowner/resowner.c | 24 ++++++++++++++++++------ src/include/common/hashfn.h | 15 +++++++++++++++ 2 files changed, 33 insertions(+), 6 deletions(-) diff --git a/src/backend/utils/resowner/resowner.c b/src/backend/utils/resowner/resowner.c index d62b1495dd3..6189d051072 100644 --- a/src/backend/utils/resowner/resowner.c +++ b/src/backend/utils/resowner/resowner.c @@ -163,15 +163,27 @@ static void ReleaseAuxProcessResourcesCallback(int code, Datum arg); * INTERNAL ROUTINES * *****************************************************************************/ +/* + * Hash function for value+kind combination. + */ static inline uint32 hash_resource_elem(Datum value, ResourceOwnerFuncs *kind) { - Datum data[2]; - - data[0] = value; - data[1] = PointerGetDatum(kind); - - return hash_bytes((unsigned char *) &data, 2 * SIZEOF_DATUM); + /* + * Most resources types store a pointer in 'value', and pointers are + * unique all on their own. But some resources store plain integers (Files + * and Buffers as of this writing), so we want to incorporate the 'kind' + * in the hash too, otherwise those resources will collide a lot. But + * because there are only a few resource kinds like that - and only a few + * resource kinds to begin with - we don't need to work too hard to mix + * 'kind' into the hash. Just add it with hash_combine(), it perturbs the + * result enough for our purposes. + */ +#if SIZEOF_DATUM == 8 + return hash_combine64(murmurhash64((uint64) value), (uint64) kind); +#else + return hash_combine(murmurhash32((uint32) value), (uint32) kind); +#endif } static void diff --git a/src/include/common/hashfn.h b/src/include/common/hashfn.h index c634cc067a1..009ffbbdd38 100644 --- a/src/include/common/hashfn.h +++ b/src/include/common/hashfn.h @@ -101,4 +101,19 @@ murmurhash32(uint32 data) return h; } +/* 64-bit variant */ +static inline uint64 +murmurhash64(uint64 data) +{ + uint64 h = data; + + h ^= h >> 33; + h *= 0xff51afd7ed558ccd; + h ^= h >> 33; + h *= 0xc4ceb9fe1a85ec53; + h ^= h >> 33; + + return h; +} + #endif /* HASHFN_H */ -- 2.30.1 --------------C6A03312D0F99F9183A0DF46-- ^ permalink raw reply [nested|flat] 48+ messages in thread
* [PATCH v6 3/3] Optimize hash function @ 2021-01-20 22:06 Heikki Linnakangas <[email protected]> 0 siblings, 0 replies; 48+ messages in thread From: Heikki Linnakangas @ 2021-01-20 22:06 UTC (permalink / raw) --- src/backend/utils/resowner/resowner.c | 24 ++++++++++++++++++------ src/include/common/hashfn.h | 15 +++++++++++++++ 2 files changed, 33 insertions(+), 6 deletions(-) diff --git a/src/backend/utils/resowner/resowner.c b/src/backend/utils/resowner/resowner.c index 1db1e7cf14b..72e4d8a784c 100644 --- a/src/backend/utils/resowner/resowner.c +++ b/src/backend/utils/resowner/resowner.c @@ -163,15 +163,27 @@ static void ReleaseAuxProcessResourcesCallback(int code, Datum arg); * INTERNAL ROUTINES * *****************************************************************************/ +/* + * Hash function for value+kind combination. + */ static inline uint32 hash_resource_elem(Datum value, ResourceOwnerFuncs *kind) { - Datum data[2]; - - data[0] = value; - data[1] = PointerGetDatum(kind); - - return hash_bytes((unsigned char *) &data, 2 * SIZEOF_DATUM); + /* + * Most resources types store a pointer in 'value', and pointers are + * unique all on their own. But some resources store plain integers (Files + * and Buffers as of this writing), so we want to incorporate the 'kind' + * in the hash too, otherwise those resources will collide a lot. But + * because there are only a few resource kinds like that - and only a few + * resource kinds to begin with - we don't need to work too hard to mix + * 'kind' into the hash. Just add it with hash_combine(), it perturbs the + * result enough for our purposes. + */ +#if SIZEOF_DATUM == 8 + return hash_combine64(murmurhash64((uint64) value), (uint64) kind); +#else + return hash_combine(murmurhash32((uint32) value), (uint32) kind); +#endif } static void diff --git a/src/include/common/hashfn.h b/src/include/common/hashfn.h index c634cc067a1..009ffbbdd38 100644 --- a/src/include/common/hashfn.h +++ b/src/include/common/hashfn.h @@ -101,4 +101,19 @@ murmurhash32(uint32 data) return h; } +/* 64-bit variant */ +static inline uint64 +murmurhash64(uint64 data) +{ + uint64 h = data; + + h ^= h >> 33; + h *= 0xff51afd7ed558ccd; + h ^= h >> 33; + h *= 0xc4ceb9fe1a85ec53; + h ^= h >> 33; + + return h; +} + #endif /* HASHFN_H */ -- 2.29.2 --------------F30A5443A5D524A5CBC51AE7 Content-Type: text/x-patch; charset=UTF-8; name="v2-0001-resownerbench.patch" Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename="v2-0001-resownerbench.patch" ^ permalink raw reply [nested|flat] 48+ messages in thread
* [PATCH v7 3/3] Optimize hash function @ 2021-01-20 22:06 Heikki Linnakangas <[email protected]> 0 siblings, 0 replies; 48+ messages in thread From: Heikki Linnakangas @ 2021-01-20 22:06 UTC (permalink / raw) --- src/backend/utils/resowner/resowner.c | 24 ++++++++++++++++++------ src/include/common/hashfn.h | 15 +++++++++++++++ 2 files changed, 33 insertions(+), 6 deletions(-) diff --git a/src/backend/utils/resowner/resowner.c b/src/backend/utils/resowner/resowner.c index d62b1495dd3..6189d051072 100644 --- a/src/backend/utils/resowner/resowner.c +++ b/src/backend/utils/resowner/resowner.c @@ -163,15 +163,27 @@ static void ReleaseAuxProcessResourcesCallback(int code, Datum arg); * INTERNAL ROUTINES * *****************************************************************************/ +/* + * Hash function for value+kind combination. + */ static inline uint32 hash_resource_elem(Datum value, ResourceOwnerFuncs *kind) { - Datum data[2]; - - data[0] = value; - data[1] = PointerGetDatum(kind); - - return hash_bytes((unsigned char *) &data, 2 * SIZEOF_DATUM); + /* + * Most resources types store a pointer in 'value', and pointers are + * unique all on their own. But some resources store plain integers (Files + * and Buffers as of this writing), so we want to incorporate the 'kind' + * in the hash too, otherwise those resources will collide a lot. But + * because there are only a few resource kinds like that - and only a few + * resource kinds to begin with - we don't need to work too hard to mix + * 'kind' into the hash. Just add it with hash_combine(), it perturbs the + * result enough for our purposes. + */ +#if SIZEOF_DATUM == 8 + return hash_combine64(murmurhash64((uint64) value), (uint64) kind); +#else + return hash_combine(murmurhash32((uint32) value), (uint32) kind); +#endif } static void diff --git a/src/include/common/hashfn.h b/src/include/common/hashfn.h index c634cc067a1..009ffbbdd38 100644 --- a/src/include/common/hashfn.h +++ b/src/include/common/hashfn.h @@ -101,4 +101,19 @@ murmurhash32(uint32 data) return h; } +/* 64-bit variant */ +static inline uint64 +murmurhash64(uint64 data) +{ + uint64 h = data; + + h ^= h >> 33; + h *= 0xff51afd7ed558ccd; + h ^= h >> 33; + h *= 0xc4ceb9fe1a85ec53; + h ^= h >> 33; + + return h; +} + #endif /* HASHFN_H */ -- 2.30.1 --------------C6A03312D0F99F9183A0DF46-- ^ permalink raw reply [nested|flat] 48+ messages in thread
* [PATCH v6 3/3] Optimize hash function @ 2021-01-20 22:06 Heikki Linnakangas <[email protected]> 0 siblings, 0 replies; 48+ messages in thread From: Heikki Linnakangas @ 2021-01-20 22:06 UTC (permalink / raw) --- src/backend/utils/resowner/resowner.c | 24 ++++++++++++++++++------ src/include/common/hashfn.h | 15 +++++++++++++++ 2 files changed, 33 insertions(+), 6 deletions(-) diff --git a/src/backend/utils/resowner/resowner.c b/src/backend/utils/resowner/resowner.c index 1db1e7cf14b..72e4d8a784c 100644 --- a/src/backend/utils/resowner/resowner.c +++ b/src/backend/utils/resowner/resowner.c @@ -163,15 +163,27 @@ static void ReleaseAuxProcessResourcesCallback(int code, Datum arg); * INTERNAL ROUTINES * *****************************************************************************/ +/* + * Hash function for value+kind combination. + */ static inline uint32 hash_resource_elem(Datum value, ResourceOwnerFuncs *kind) { - Datum data[2]; - - data[0] = value; - data[1] = PointerGetDatum(kind); - - return hash_bytes((unsigned char *) &data, 2 * SIZEOF_DATUM); + /* + * Most resources types store a pointer in 'value', and pointers are + * unique all on their own. But some resources store plain integers (Files + * and Buffers as of this writing), so we want to incorporate the 'kind' + * in the hash too, otherwise those resources will collide a lot. But + * because there are only a few resource kinds like that - and only a few + * resource kinds to begin with - we don't need to work too hard to mix + * 'kind' into the hash. Just add it with hash_combine(), it perturbs the + * result enough for our purposes. + */ +#if SIZEOF_DATUM == 8 + return hash_combine64(murmurhash64((uint64) value), (uint64) kind); +#else + return hash_combine(murmurhash32((uint32) value), (uint32) kind); +#endif } static void diff --git a/src/include/common/hashfn.h b/src/include/common/hashfn.h index c634cc067a1..009ffbbdd38 100644 --- a/src/include/common/hashfn.h +++ b/src/include/common/hashfn.h @@ -101,4 +101,19 @@ murmurhash32(uint32 data) return h; } +/* 64-bit variant */ +static inline uint64 +murmurhash64(uint64 data) +{ + uint64 h = data; + + h ^= h >> 33; + h *= 0xff51afd7ed558ccd; + h ^= h >> 33; + h *= 0xc4ceb9fe1a85ec53; + h ^= h >> 33; + + return h; +} + #endif /* HASHFN_H */ -- 2.29.2 --------------F30A5443A5D524A5CBC51AE7 Content-Type: text/x-patch; charset=UTF-8; name="v2-0001-resownerbench.patch" Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename="v2-0001-resownerbench.patch" ^ permalink raw reply [nested|flat] 48+ messages in thread
* [PATCH v7 3/3] Optimize hash function @ 2021-01-20 22:06 Heikki Linnakangas <[email protected]> 0 siblings, 0 replies; 48+ messages in thread From: Heikki Linnakangas @ 2021-01-20 22:06 UTC (permalink / raw) --- src/backend/utils/resowner/resowner.c | 24 ++++++++++++++++++------ src/include/common/hashfn.h | 15 +++++++++++++++ 2 files changed, 33 insertions(+), 6 deletions(-) diff --git a/src/backend/utils/resowner/resowner.c b/src/backend/utils/resowner/resowner.c index d62b1495dd3..6189d051072 100644 --- a/src/backend/utils/resowner/resowner.c +++ b/src/backend/utils/resowner/resowner.c @@ -163,15 +163,27 @@ static void ReleaseAuxProcessResourcesCallback(int code, Datum arg); * INTERNAL ROUTINES * *****************************************************************************/ +/* + * Hash function for value+kind combination. + */ static inline uint32 hash_resource_elem(Datum value, ResourceOwnerFuncs *kind) { - Datum data[2]; - - data[0] = value; - data[1] = PointerGetDatum(kind); - - return hash_bytes((unsigned char *) &data, 2 * SIZEOF_DATUM); + /* + * Most resources types store a pointer in 'value', and pointers are + * unique all on their own. But some resources store plain integers (Files + * and Buffers as of this writing), so we want to incorporate the 'kind' + * in the hash too, otherwise those resources will collide a lot. But + * because there are only a few resource kinds like that - and only a few + * resource kinds to begin with - we don't need to work too hard to mix + * 'kind' into the hash. Just add it with hash_combine(), it perturbs the + * result enough for our purposes. + */ +#if SIZEOF_DATUM == 8 + return hash_combine64(murmurhash64((uint64) value), (uint64) kind); +#else + return hash_combine(murmurhash32((uint32) value), (uint32) kind); +#endif } static void diff --git a/src/include/common/hashfn.h b/src/include/common/hashfn.h index c634cc067a1..009ffbbdd38 100644 --- a/src/include/common/hashfn.h +++ b/src/include/common/hashfn.h @@ -101,4 +101,19 @@ murmurhash32(uint32 data) return h; } +/* 64-bit variant */ +static inline uint64 +murmurhash64(uint64 data) +{ + uint64 h = data; + + h ^= h >> 33; + h *= 0xff51afd7ed558ccd; + h ^= h >> 33; + h *= 0xc4ceb9fe1a85ec53; + h ^= h >> 33; + + return h; +} + #endif /* HASHFN_H */ -- 2.30.1 --------------C6A03312D0F99F9183A0DF46-- ^ permalink raw reply [nested|flat] 48+ messages in thread
* [PATCH v7 3/3] Optimize hash function @ 2021-01-20 22:06 Heikki Linnakangas <[email protected]> 0 siblings, 0 replies; 48+ messages in thread From: Heikki Linnakangas @ 2021-01-20 22:06 UTC (permalink / raw) --- src/backend/utils/resowner/resowner.c | 24 ++++++++++++++++++------ src/include/common/hashfn.h | 15 +++++++++++++++ 2 files changed, 33 insertions(+), 6 deletions(-) diff --git a/src/backend/utils/resowner/resowner.c b/src/backend/utils/resowner/resowner.c index d62b1495dd3..6189d051072 100644 --- a/src/backend/utils/resowner/resowner.c +++ b/src/backend/utils/resowner/resowner.c @@ -163,15 +163,27 @@ static void ReleaseAuxProcessResourcesCallback(int code, Datum arg); * INTERNAL ROUTINES * *****************************************************************************/ +/* + * Hash function for value+kind combination. + */ static inline uint32 hash_resource_elem(Datum value, ResourceOwnerFuncs *kind) { - Datum data[2]; - - data[0] = value; - data[1] = PointerGetDatum(kind); - - return hash_bytes((unsigned char *) &data, 2 * SIZEOF_DATUM); + /* + * Most resources types store a pointer in 'value', and pointers are + * unique all on their own. But some resources store plain integers (Files + * and Buffers as of this writing), so we want to incorporate the 'kind' + * in the hash too, otherwise those resources will collide a lot. But + * because there are only a few resource kinds like that - and only a few + * resource kinds to begin with - we don't need to work too hard to mix + * 'kind' into the hash. Just add it with hash_combine(), it perturbs the + * result enough for our purposes. + */ +#if SIZEOF_DATUM == 8 + return hash_combine64(murmurhash64((uint64) value), (uint64) kind); +#else + return hash_combine(murmurhash32((uint32) value), (uint32) kind); +#endif } static void diff --git a/src/include/common/hashfn.h b/src/include/common/hashfn.h index c634cc067a1..009ffbbdd38 100644 --- a/src/include/common/hashfn.h +++ b/src/include/common/hashfn.h @@ -101,4 +101,19 @@ murmurhash32(uint32 data) return h; } +/* 64-bit variant */ +static inline uint64 +murmurhash64(uint64 data) +{ + uint64 h = data; + + h ^= h >> 33; + h *= 0xff51afd7ed558ccd; + h ^= h >> 33; + h *= 0xc4ceb9fe1a85ec53; + h ^= h >> 33; + + return h; +} + #endif /* HASHFN_H */ -- 2.30.1 --------------C6A03312D0F99F9183A0DF46-- ^ permalink raw reply [nested|flat] 48+ messages in thread
* [PATCH v6 3/3] Optimize hash function @ 2021-01-20 22:06 Heikki Linnakangas <[email protected]> 0 siblings, 0 replies; 48+ messages in thread From: Heikki Linnakangas @ 2021-01-20 22:06 UTC (permalink / raw) --- src/backend/utils/resowner/resowner.c | 24 ++++++++++++++++++------ src/include/common/hashfn.h | 15 +++++++++++++++ 2 files changed, 33 insertions(+), 6 deletions(-) diff --git a/src/backend/utils/resowner/resowner.c b/src/backend/utils/resowner/resowner.c index 1db1e7cf14b..72e4d8a784c 100644 --- a/src/backend/utils/resowner/resowner.c +++ b/src/backend/utils/resowner/resowner.c @@ -163,15 +163,27 @@ static void ReleaseAuxProcessResourcesCallback(int code, Datum arg); * INTERNAL ROUTINES * *****************************************************************************/ +/* + * Hash function for value+kind combination. + */ static inline uint32 hash_resource_elem(Datum value, ResourceOwnerFuncs *kind) { - Datum data[2]; - - data[0] = value; - data[1] = PointerGetDatum(kind); - - return hash_bytes((unsigned char *) &data, 2 * SIZEOF_DATUM); + /* + * Most resources types store a pointer in 'value', and pointers are + * unique all on their own. But some resources store plain integers (Files + * and Buffers as of this writing), so we want to incorporate the 'kind' + * in the hash too, otherwise those resources will collide a lot. But + * because there are only a few resource kinds like that - and only a few + * resource kinds to begin with - we don't need to work too hard to mix + * 'kind' into the hash. Just add it with hash_combine(), it perturbs the + * result enough for our purposes. + */ +#if SIZEOF_DATUM == 8 + return hash_combine64(murmurhash64((uint64) value), (uint64) kind); +#else + return hash_combine(murmurhash32((uint32) value), (uint32) kind); +#endif } static void diff --git a/src/include/common/hashfn.h b/src/include/common/hashfn.h index c634cc067a1..009ffbbdd38 100644 --- a/src/include/common/hashfn.h +++ b/src/include/common/hashfn.h @@ -101,4 +101,19 @@ murmurhash32(uint32 data) return h; } +/* 64-bit variant */ +static inline uint64 +murmurhash64(uint64 data) +{ + uint64 h = data; + + h ^= h >> 33; + h *= 0xff51afd7ed558ccd; + h ^= h >> 33; + h *= 0xc4ceb9fe1a85ec53; + h ^= h >> 33; + + return h; +} + #endif /* HASHFN_H */ -- 2.29.2 --------------F30A5443A5D524A5CBC51AE7 Content-Type: text/x-patch; charset=UTF-8; name="v2-0001-resownerbench.patch" Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename="v2-0001-resownerbench.patch" ^ permalink raw reply [nested|flat] 48+ messages in thread
* [PATCH v7 3/3] Optimize hash function @ 2021-01-20 22:06 Heikki Linnakangas <[email protected]> 0 siblings, 0 replies; 48+ messages in thread From: Heikki Linnakangas @ 2021-01-20 22:06 UTC (permalink / raw) --- src/backend/utils/resowner/resowner.c | 24 ++++++++++++++++++------ src/include/common/hashfn.h | 15 +++++++++++++++ 2 files changed, 33 insertions(+), 6 deletions(-) diff --git a/src/backend/utils/resowner/resowner.c b/src/backend/utils/resowner/resowner.c index d62b1495dd3..6189d051072 100644 --- a/src/backend/utils/resowner/resowner.c +++ b/src/backend/utils/resowner/resowner.c @@ -163,15 +163,27 @@ static void ReleaseAuxProcessResourcesCallback(int code, Datum arg); * INTERNAL ROUTINES * *****************************************************************************/ +/* + * Hash function for value+kind combination. + */ static inline uint32 hash_resource_elem(Datum value, ResourceOwnerFuncs *kind) { - Datum data[2]; - - data[0] = value; - data[1] = PointerGetDatum(kind); - - return hash_bytes((unsigned char *) &data, 2 * SIZEOF_DATUM); + /* + * Most resources types store a pointer in 'value', and pointers are + * unique all on their own. But some resources store plain integers (Files + * and Buffers as of this writing), so we want to incorporate the 'kind' + * in the hash too, otherwise those resources will collide a lot. But + * because there are only a few resource kinds like that - and only a few + * resource kinds to begin with - we don't need to work too hard to mix + * 'kind' into the hash. Just add it with hash_combine(), it perturbs the + * result enough for our purposes. + */ +#if SIZEOF_DATUM == 8 + return hash_combine64(murmurhash64((uint64) value), (uint64) kind); +#else + return hash_combine(murmurhash32((uint32) value), (uint32) kind); +#endif } static void diff --git a/src/include/common/hashfn.h b/src/include/common/hashfn.h index c634cc067a1..009ffbbdd38 100644 --- a/src/include/common/hashfn.h +++ b/src/include/common/hashfn.h @@ -101,4 +101,19 @@ murmurhash32(uint32 data) return h; } +/* 64-bit variant */ +static inline uint64 +murmurhash64(uint64 data) +{ + uint64 h = data; + + h ^= h >> 33; + h *= 0xff51afd7ed558ccd; + h ^= h >> 33; + h *= 0xc4ceb9fe1a85ec53; + h ^= h >> 33; + + return h; +} + #endif /* HASHFN_H */ -- 2.30.1 --------------C6A03312D0F99F9183A0DF46-- ^ permalink raw reply [nested|flat] 48+ messages in thread
* [PATCH v6 3/3] Optimize hash function @ 2021-01-20 22:06 Heikki Linnakangas <[email protected]> 0 siblings, 0 replies; 48+ messages in thread From: Heikki Linnakangas @ 2021-01-20 22:06 UTC (permalink / raw) --- src/backend/utils/resowner/resowner.c | 24 ++++++++++++++++++------ src/include/common/hashfn.h | 15 +++++++++++++++ 2 files changed, 33 insertions(+), 6 deletions(-) diff --git a/src/backend/utils/resowner/resowner.c b/src/backend/utils/resowner/resowner.c index 1db1e7cf14b..72e4d8a784c 100644 --- a/src/backend/utils/resowner/resowner.c +++ b/src/backend/utils/resowner/resowner.c @@ -163,15 +163,27 @@ static void ReleaseAuxProcessResourcesCallback(int code, Datum arg); * INTERNAL ROUTINES * *****************************************************************************/ +/* + * Hash function for value+kind combination. + */ static inline uint32 hash_resource_elem(Datum value, ResourceOwnerFuncs *kind) { - Datum data[2]; - - data[0] = value; - data[1] = PointerGetDatum(kind); - - return hash_bytes((unsigned char *) &data, 2 * SIZEOF_DATUM); + /* + * Most resources types store a pointer in 'value', and pointers are + * unique all on their own. But some resources store plain integers (Files + * and Buffers as of this writing), so we want to incorporate the 'kind' + * in the hash too, otherwise those resources will collide a lot. But + * because there are only a few resource kinds like that - and only a few + * resource kinds to begin with - we don't need to work too hard to mix + * 'kind' into the hash. Just add it with hash_combine(), it perturbs the + * result enough for our purposes. + */ +#if SIZEOF_DATUM == 8 + return hash_combine64(murmurhash64((uint64) value), (uint64) kind); +#else + return hash_combine(murmurhash32((uint32) value), (uint32) kind); +#endif } static void diff --git a/src/include/common/hashfn.h b/src/include/common/hashfn.h index c634cc067a1..009ffbbdd38 100644 --- a/src/include/common/hashfn.h +++ b/src/include/common/hashfn.h @@ -101,4 +101,19 @@ murmurhash32(uint32 data) return h; } +/* 64-bit variant */ +static inline uint64 +murmurhash64(uint64 data) +{ + uint64 h = data; + + h ^= h >> 33; + h *= 0xff51afd7ed558ccd; + h ^= h >> 33; + h *= 0xc4ceb9fe1a85ec53; + h ^= h >> 33; + + return h; +} + #endif /* HASHFN_H */ -- 2.29.2 --------------F30A5443A5D524A5CBC51AE7 Content-Type: text/x-patch; charset=UTF-8; name="v2-0001-resownerbench.patch" Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename="v2-0001-resownerbench.patch" ^ permalink raw reply [nested|flat] 48+ messages in thread
* [PATCH v7 3/3] Optimize hash function @ 2021-01-20 22:06 Heikki Linnakangas <[email protected]> 0 siblings, 0 replies; 48+ messages in thread From: Heikki Linnakangas @ 2021-01-20 22:06 UTC (permalink / raw) --- src/backend/utils/resowner/resowner.c | 24 ++++++++++++++++++------ src/include/common/hashfn.h | 15 +++++++++++++++ 2 files changed, 33 insertions(+), 6 deletions(-) diff --git a/src/backend/utils/resowner/resowner.c b/src/backend/utils/resowner/resowner.c index d62b1495dd3..6189d051072 100644 --- a/src/backend/utils/resowner/resowner.c +++ b/src/backend/utils/resowner/resowner.c @@ -163,15 +163,27 @@ static void ReleaseAuxProcessResourcesCallback(int code, Datum arg); * INTERNAL ROUTINES * *****************************************************************************/ +/* + * Hash function for value+kind combination. + */ static inline uint32 hash_resource_elem(Datum value, ResourceOwnerFuncs *kind) { - Datum data[2]; - - data[0] = value; - data[1] = PointerGetDatum(kind); - - return hash_bytes((unsigned char *) &data, 2 * SIZEOF_DATUM); + /* + * Most resources types store a pointer in 'value', and pointers are + * unique all on their own. But some resources store plain integers (Files + * and Buffers as of this writing), so we want to incorporate the 'kind' + * in the hash too, otherwise those resources will collide a lot. But + * because there are only a few resource kinds like that - and only a few + * resource kinds to begin with - we don't need to work too hard to mix + * 'kind' into the hash. Just add it with hash_combine(), it perturbs the + * result enough for our purposes. + */ +#if SIZEOF_DATUM == 8 + return hash_combine64(murmurhash64((uint64) value), (uint64) kind); +#else + return hash_combine(murmurhash32((uint32) value), (uint32) kind); +#endif } static void diff --git a/src/include/common/hashfn.h b/src/include/common/hashfn.h index c634cc067a1..009ffbbdd38 100644 --- a/src/include/common/hashfn.h +++ b/src/include/common/hashfn.h @@ -101,4 +101,19 @@ murmurhash32(uint32 data) return h; } +/* 64-bit variant */ +static inline uint64 +murmurhash64(uint64 data) +{ + uint64 h = data; + + h ^= h >> 33; + h *= 0xff51afd7ed558ccd; + h ^= h >> 33; + h *= 0xc4ceb9fe1a85ec53; + h ^= h >> 33; + + return h; +} + #endif /* HASHFN_H */ -- 2.30.1 --------------C6A03312D0F99F9183A0DF46-- ^ permalink raw reply [nested|flat] 48+ messages in thread
* [PATCH v6 3/3] Optimize hash function @ 2021-01-20 22:06 Heikki Linnakangas <[email protected]> 0 siblings, 0 replies; 48+ messages in thread From: Heikki Linnakangas @ 2021-01-20 22:06 UTC (permalink / raw) --- src/backend/utils/resowner/resowner.c | 24 ++++++++++++++++++------ src/include/common/hashfn.h | 15 +++++++++++++++ 2 files changed, 33 insertions(+), 6 deletions(-) diff --git a/src/backend/utils/resowner/resowner.c b/src/backend/utils/resowner/resowner.c index 1db1e7cf14b..72e4d8a784c 100644 --- a/src/backend/utils/resowner/resowner.c +++ b/src/backend/utils/resowner/resowner.c @@ -163,15 +163,27 @@ static void ReleaseAuxProcessResourcesCallback(int code, Datum arg); * INTERNAL ROUTINES * *****************************************************************************/ +/* + * Hash function for value+kind combination. + */ static inline uint32 hash_resource_elem(Datum value, ResourceOwnerFuncs *kind) { - Datum data[2]; - - data[0] = value; - data[1] = PointerGetDatum(kind); - - return hash_bytes((unsigned char *) &data, 2 * SIZEOF_DATUM); + /* + * Most resources types store a pointer in 'value', and pointers are + * unique all on their own. But some resources store plain integers (Files + * and Buffers as of this writing), so we want to incorporate the 'kind' + * in the hash too, otherwise those resources will collide a lot. But + * because there are only a few resource kinds like that - and only a few + * resource kinds to begin with - we don't need to work too hard to mix + * 'kind' into the hash. Just add it with hash_combine(), it perturbs the + * result enough for our purposes. + */ +#if SIZEOF_DATUM == 8 + return hash_combine64(murmurhash64((uint64) value), (uint64) kind); +#else + return hash_combine(murmurhash32((uint32) value), (uint32) kind); +#endif } static void diff --git a/src/include/common/hashfn.h b/src/include/common/hashfn.h index c634cc067a1..009ffbbdd38 100644 --- a/src/include/common/hashfn.h +++ b/src/include/common/hashfn.h @@ -101,4 +101,19 @@ murmurhash32(uint32 data) return h; } +/* 64-bit variant */ +static inline uint64 +murmurhash64(uint64 data) +{ + uint64 h = data; + + h ^= h >> 33; + h *= 0xff51afd7ed558ccd; + h ^= h >> 33; + h *= 0xc4ceb9fe1a85ec53; + h ^= h >> 33; + + return h; +} + #endif /* HASHFN_H */ -- 2.29.2 --------------F30A5443A5D524A5CBC51AE7 Content-Type: text/x-patch; charset=UTF-8; name="v2-0001-resownerbench.patch" Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename="v2-0001-resownerbench.patch" ^ permalink raw reply [nested|flat] 48+ messages in thread
* [PATCH v7 3/3] Optimize hash function @ 2021-01-20 22:06 Heikki Linnakangas <[email protected]> 0 siblings, 0 replies; 48+ messages in thread From: Heikki Linnakangas @ 2021-01-20 22:06 UTC (permalink / raw) --- src/backend/utils/resowner/resowner.c | 24 ++++++++++++++++++------ src/include/common/hashfn.h | 15 +++++++++++++++ 2 files changed, 33 insertions(+), 6 deletions(-) diff --git a/src/backend/utils/resowner/resowner.c b/src/backend/utils/resowner/resowner.c index d62b1495dd3..6189d051072 100644 --- a/src/backend/utils/resowner/resowner.c +++ b/src/backend/utils/resowner/resowner.c @@ -163,15 +163,27 @@ static void ReleaseAuxProcessResourcesCallback(int code, Datum arg); * INTERNAL ROUTINES * *****************************************************************************/ +/* + * Hash function for value+kind combination. + */ static inline uint32 hash_resource_elem(Datum value, ResourceOwnerFuncs *kind) { - Datum data[2]; - - data[0] = value; - data[1] = PointerGetDatum(kind); - - return hash_bytes((unsigned char *) &data, 2 * SIZEOF_DATUM); + /* + * Most resources types store a pointer in 'value', and pointers are + * unique all on their own. But some resources store plain integers (Files + * and Buffers as of this writing), so we want to incorporate the 'kind' + * in the hash too, otherwise those resources will collide a lot. But + * because there are only a few resource kinds like that - and only a few + * resource kinds to begin with - we don't need to work too hard to mix + * 'kind' into the hash. Just add it with hash_combine(), it perturbs the + * result enough for our purposes. + */ +#if SIZEOF_DATUM == 8 + return hash_combine64(murmurhash64((uint64) value), (uint64) kind); +#else + return hash_combine(murmurhash32((uint32) value), (uint32) kind); +#endif } static void diff --git a/src/include/common/hashfn.h b/src/include/common/hashfn.h index c634cc067a1..009ffbbdd38 100644 --- a/src/include/common/hashfn.h +++ b/src/include/common/hashfn.h @@ -101,4 +101,19 @@ murmurhash32(uint32 data) return h; } +/* 64-bit variant */ +static inline uint64 +murmurhash64(uint64 data) +{ + uint64 h = data; + + h ^= h >> 33; + h *= 0xff51afd7ed558ccd; + h ^= h >> 33; + h *= 0xc4ceb9fe1a85ec53; + h ^= h >> 33; + + return h; +} + #endif /* HASHFN_H */ -- 2.30.1 --------------C6A03312D0F99F9183A0DF46-- ^ permalink raw reply [nested|flat] 48+ messages in thread
* [PATCH v6 3/3] Optimize hash function @ 2021-01-20 22:06 Heikki Linnakangas <[email protected]> 0 siblings, 0 replies; 48+ messages in thread From: Heikki Linnakangas @ 2021-01-20 22:06 UTC (permalink / raw) --- src/backend/utils/resowner/resowner.c | 24 ++++++++++++++++++------ src/include/common/hashfn.h | 15 +++++++++++++++ 2 files changed, 33 insertions(+), 6 deletions(-) diff --git a/src/backend/utils/resowner/resowner.c b/src/backend/utils/resowner/resowner.c index 1db1e7cf14b..72e4d8a784c 100644 --- a/src/backend/utils/resowner/resowner.c +++ b/src/backend/utils/resowner/resowner.c @@ -163,15 +163,27 @@ static void ReleaseAuxProcessResourcesCallback(int code, Datum arg); * INTERNAL ROUTINES * *****************************************************************************/ +/* + * Hash function for value+kind combination. + */ static inline uint32 hash_resource_elem(Datum value, ResourceOwnerFuncs *kind) { - Datum data[2]; - - data[0] = value; - data[1] = PointerGetDatum(kind); - - return hash_bytes((unsigned char *) &data, 2 * SIZEOF_DATUM); + /* + * Most resources types store a pointer in 'value', and pointers are + * unique all on their own. But some resources store plain integers (Files + * and Buffers as of this writing), so we want to incorporate the 'kind' + * in the hash too, otherwise those resources will collide a lot. But + * because there are only a few resource kinds like that - and only a few + * resource kinds to begin with - we don't need to work too hard to mix + * 'kind' into the hash. Just add it with hash_combine(), it perturbs the + * result enough for our purposes. + */ +#if SIZEOF_DATUM == 8 + return hash_combine64(murmurhash64((uint64) value), (uint64) kind); +#else + return hash_combine(murmurhash32((uint32) value), (uint32) kind); +#endif } static void diff --git a/src/include/common/hashfn.h b/src/include/common/hashfn.h index c634cc067a1..009ffbbdd38 100644 --- a/src/include/common/hashfn.h +++ b/src/include/common/hashfn.h @@ -101,4 +101,19 @@ murmurhash32(uint32 data) return h; } +/* 64-bit variant */ +static inline uint64 +murmurhash64(uint64 data) +{ + uint64 h = data; + + h ^= h >> 33; + h *= 0xff51afd7ed558ccd; + h ^= h >> 33; + h *= 0xc4ceb9fe1a85ec53; + h ^= h >> 33; + + return h; +} + #endif /* HASHFN_H */ -- 2.29.2 --------------F30A5443A5D524A5CBC51AE7 Content-Type: text/x-patch; charset=UTF-8; name="v2-0001-resownerbench.patch" Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename="v2-0001-resownerbench.patch" ^ permalink raw reply [nested|flat] 48+ messages in thread
* [PATCH v7 3/3] Optimize hash function @ 2021-01-20 22:06 Heikki Linnakangas <[email protected]> 0 siblings, 0 replies; 48+ messages in thread From: Heikki Linnakangas @ 2021-01-20 22:06 UTC (permalink / raw) --- src/backend/utils/resowner/resowner.c | 24 ++++++++++++++++++------ src/include/common/hashfn.h | 15 +++++++++++++++ 2 files changed, 33 insertions(+), 6 deletions(-) diff --git a/src/backend/utils/resowner/resowner.c b/src/backend/utils/resowner/resowner.c index d62b1495dd3..6189d051072 100644 --- a/src/backend/utils/resowner/resowner.c +++ b/src/backend/utils/resowner/resowner.c @@ -163,15 +163,27 @@ static void ReleaseAuxProcessResourcesCallback(int code, Datum arg); * INTERNAL ROUTINES * *****************************************************************************/ +/* + * Hash function for value+kind combination. + */ static inline uint32 hash_resource_elem(Datum value, ResourceOwnerFuncs *kind) { - Datum data[2]; - - data[0] = value; - data[1] = PointerGetDatum(kind); - - return hash_bytes((unsigned char *) &data, 2 * SIZEOF_DATUM); + /* + * Most resources types store a pointer in 'value', and pointers are + * unique all on their own. But some resources store plain integers (Files + * and Buffers as of this writing), so we want to incorporate the 'kind' + * in the hash too, otherwise those resources will collide a lot. But + * because there are only a few resource kinds like that - and only a few + * resource kinds to begin with - we don't need to work too hard to mix + * 'kind' into the hash. Just add it with hash_combine(), it perturbs the + * result enough for our purposes. + */ +#if SIZEOF_DATUM == 8 + return hash_combine64(murmurhash64((uint64) value), (uint64) kind); +#else + return hash_combine(murmurhash32((uint32) value), (uint32) kind); +#endif } static void diff --git a/src/include/common/hashfn.h b/src/include/common/hashfn.h index c634cc067a1..009ffbbdd38 100644 --- a/src/include/common/hashfn.h +++ b/src/include/common/hashfn.h @@ -101,4 +101,19 @@ murmurhash32(uint32 data) return h; } +/* 64-bit variant */ +static inline uint64 +murmurhash64(uint64 data) +{ + uint64 h = data; + + h ^= h >> 33; + h *= 0xff51afd7ed558ccd; + h ^= h >> 33; + h *= 0xc4ceb9fe1a85ec53; + h ^= h >> 33; + + return h; +} + #endif /* HASHFN_H */ -- 2.30.1 --------------C6A03312D0F99F9183A0DF46-- ^ permalink raw reply [nested|flat] 48+ messages in thread
* [PATCH v6 3/3] Optimize hash function @ 2021-01-20 22:06 Heikki Linnakangas <[email protected]> 0 siblings, 0 replies; 48+ messages in thread From: Heikki Linnakangas @ 2021-01-20 22:06 UTC (permalink / raw) --- src/backend/utils/resowner/resowner.c | 24 ++++++++++++++++++------ src/include/common/hashfn.h | 15 +++++++++++++++ 2 files changed, 33 insertions(+), 6 deletions(-) diff --git a/src/backend/utils/resowner/resowner.c b/src/backend/utils/resowner/resowner.c index 1db1e7cf14b..72e4d8a784c 100644 --- a/src/backend/utils/resowner/resowner.c +++ b/src/backend/utils/resowner/resowner.c @@ -163,15 +163,27 @@ static void ReleaseAuxProcessResourcesCallback(int code, Datum arg); * INTERNAL ROUTINES * *****************************************************************************/ +/* + * Hash function for value+kind combination. + */ static inline uint32 hash_resource_elem(Datum value, ResourceOwnerFuncs *kind) { - Datum data[2]; - - data[0] = value; - data[1] = PointerGetDatum(kind); - - return hash_bytes((unsigned char *) &data, 2 * SIZEOF_DATUM); + /* + * Most resources types store a pointer in 'value', and pointers are + * unique all on their own. But some resources store plain integers (Files + * and Buffers as of this writing), so we want to incorporate the 'kind' + * in the hash too, otherwise those resources will collide a lot. But + * because there are only a few resource kinds like that - and only a few + * resource kinds to begin with - we don't need to work too hard to mix + * 'kind' into the hash. Just add it with hash_combine(), it perturbs the + * result enough for our purposes. + */ +#if SIZEOF_DATUM == 8 + return hash_combine64(murmurhash64((uint64) value), (uint64) kind); +#else + return hash_combine(murmurhash32((uint32) value), (uint32) kind); +#endif } static void diff --git a/src/include/common/hashfn.h b/src/include/common/hashfn.h index c634cc067a1..009ffbbdd38 100644 --- a/src/include/common/hashfn.h +++ b/src/include/common/hashfn.h @@ -101,4 +101,19 @@ murmurhash32(uint32 data) return h; } +/* 64-bit variant */ +static inline uint64 +murmurhash64(uint64 data) +{ + uint64 h = data; + + h ^= h >> 33; + h *= 0xff51afd7ed558ccd; + h ^= h >> 33; + h *= 0xc4ceb9fe1a85ec53; + h ^= h >> 33; + + return h; +} + #endif /* HASHFN_H */ -- 2.29.2 --------------F30A5443A5D524A5CBC51AE7 Content-Type: text/x-patch; charset=UTF-8; name="v2-0001-resownerbench.patch" Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename="v2-0001-resownerbench.patch" ^ permalink raw reply [nested|flat] 48+ messages in thread
* [PATCH v7 3/3] Optimize hash function @ 2021-01-20 22:06 Heikki Linnakangas <[email protected]> 0 siblings, 0 replies; 48+ messages in thread From: Heikki Linnakangas @ 2021-01-20 22:06 UTC (permalink / raw) --- src/backend/utils/resowner/resowner.c | 24 ++++++++++++++++++------ src/include/common/hashfn.h | 15 +++++++++++++++ 2 files changed, 33 insertions(+), 6 deletions(-) diff --git a/src/backend/utils/resowner/resowner.c b/src/backend/utils/resowner/resowner.c index d62b1495dd3..6189d051072 100644 --- a/src/backend/utils/resowner/resowner.c +++ b/src/backend/utils/resowner/resowner.c @@ -163,15 +163,27 @@ static void ReleaseAuxProcessResourcesCallback(int code, Datum arg); * INTERNAL ROUTINES * *****************************************************************************/ +/* + * Hash function for value+kind combination. + */ static inline uint32 hash_resource_elem(Datum value, ResourceOwnerFuncs *kind) { - Datum data[2]; - - data[0] = value; - data[1] = PointerGetDatum(kind); - - return hash_bytes((unsigned char *) &data, 2 * SIZEOF_DATUM); + /* + * Most resources types store a pointer in 'value', and pointers are + * unique all on their own. But some resources store plain integers (Files + * and Buffers as of this writing), so we want to incorporate the 'kind' + * in the hash too, otherwise those resources will collide a lot. But + * because there are only a few resource kinds like that - and only a few + * resource kinds to begin with - we don't need to work too hard to mix + * 'kind' into the hash. Just add it with hash_combine(), it perturbs the + * result enough for our purposes. + */ +#if SIZEOF_DATUM == 8 + return hash_combine64(murmurhash64((uint64) value), (uint64) kind); +#else + return hash_combine(murmurhash32((uint32) value), (uint32) kind); +#endif } static void diff --git a/src/include/common/hashfn.h b/src/include/common/hashfn.h index c634cc067a1..009ffbbdd38 100644 --- a/src/include/common/hashfn.h +++ b/src/include/common/hashfn.h @@ -101,4 +101,19 @@ murmurhash32(uint32 data) return h; } +/* 64-bit variant */ +static inline uint64 +murmurhash64(uint64 data) +{ + uint64 h = data; + + h ^= h >> 33; + h *= 0xff51afd7ed558ccd; + h ^= h >> 33; + h *= 0xc4ceb9fe1a85ec53; + h ^= h >> 33; + + return h; +} + #endif /* HASHFN_H */ -- 2.30.1 --------------C6A03312D0F99F9183A0DF46-- ^ permalink raw reply [nested|flat] 48+ messages in thread
* [PATCH v6 3/3] Optimize hash function @ 2021-01-20 22:06 Heikki Linnakangas <[email protected]> 0 siblings, 0 replies; 48+ messages in thread From: Heikki Linnakangas @ 2021-01-20 22:06 UTC (permalink / raw) --- src/backend/utils/resowner/resowner.c | 24 ++++++++++++++++++------ src/include/common/hashfn.h | 15 +++++++++++++++ 2 files changed, 33 insertions(+), 6 deletions(-) diff --git a/src/backend/utils/resowner/resowner.c b/src/backend/utils/resowner/resowner.c index 1db1e7cf14b..72e4d8a784c 100644 --- a/src/backend/utils/resowner/resowner.c +++ b/src/backend/utils/resowner/resowner.c @@ -163,15 +163,27 @@ static void ReleaseAuxProcessResourcesCallback(int code, Datum arg); * INTERNAL ROUTINES * *****************************************************************************/ +/* + * Hash function for value+kind combination. + */ static inline uint32 hash_resource_elem(Datum value, ResourceOwnerFuncs *kind) { - Datum data[2]; - - data[0] = value; - data[1] = PointerGetDatum(kind); - - return hash_bytes((unsigned char *) &data, 2 * SIZEOF_DATUM); + /* + * Most resources types store a pointer in 'value', and pointers are + * unique all on their own. But some resources store plain integers (Files + * and Buffers as of this writing), so we want to incorporate the 'kind' + * in the hash too, otherwise those resources will collide a lot. But + * because there are only a few resource kinds like that - and only a few + * resource kinds to begin with - we don't need to work too hard to mix + * 'kind' into the hash. Just add it with hash_combine(), it perturbs the + * result enough for our purposes. + */ +#if SIZEOF_DATUM == 8 + return hash_combine64(murmurhash64((uint64) value), (uint64) kind); +#else + return hash_combine(murmurhash32((uint32) value), (uint32) kind); +#endif } static void diff --git a/src/include/common/hashfn.h b/src/include/common/hashfn.h index c634cc067a1..009ffbbdd38 100644 --- a/src/include/common/hashfn.h +++ b/src/include/common/hashfn.h @@ -101,4 +101,19 @@ murmurhash32(uint32 data) return h; } +/* 64-bit variant */ +static inline uint64 +murmurhash64(uint64 data) +{ + uint64 h = data; + + h ^= h >> 33; + h *= 0xff51afd7ed558ccd; + h ^= h >> 33; + h *= 0xc4ceb9fe1a85ec53; + h ^= h >> 33; + + return h; +} + #endif /* HASHFN_H */ -- 2.29.2 --------------F30A5443A5D524A5CBC51AE7 Content-Type: text/x-patch; charset=UTF-8; name="v2-0001-resownerbench.patch" Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename="v2-0001-resownerbench.patch" ^ permalink raw reply [nested|flat] 48+ messages in thread
* [PATCH v7 3/3] Optimize hash function @ 2021-01-20 22:06 Heikki Linnakangas <[email protected]> 0 siblings, 0 replies; 48+ messages in thread From: Heikki Linnakangas @ 2021-01-20 22:06 UTC (permalink / raw) --- src/backend/utils/resowner/resowner.c | 24 ++++++++++++++++++------ src/include/common/hashfn.h | 15 +++++++++++++++ 2 files changed, 33 insertions(+), 6 deletions(-) diff --git a/src/backend/utils/resowner/resowner.c b/src/backend/utils/resowner/resowner.c index d62b1495dd3..6189d051072 100644 --- a/src/backend/utils/resowner/resowner.c +++ b/src/backend/utils/resowner/resowner.c @@ -163,15 +163,27 @@ static void ReleaseAuxProcessResourcesCallback(int code, Datum arg); * INTERNAL ROUTINES * *****************************************************************************/ +/* + * Hash function for value+kind combination. + */ static inline uint32 hash_resource_elem(Datum value, ResourceOwnerFuncs *kind) { - Datum data[2]; - - data[0] = value; - data[1] = PointerGetDatum(kind); - - return hash_bytes((unsigned char *) &data, 2 * SIZEOF_DATUM); + /* + * Most resources types store a pointer in 'value', and pointers are + * unique all on their own. But some resources store plain integers (Files + * and Buffers as of this writing), so we want to incorporate the 'kind' + * in the hash too, otherwise those resources will collide a lot. But + * because there are only a few resource kinds like that - and only a few + * resource kinds to begin with - we don't need to work too hard to mix + * 'kind' into the hash. Just add it with hash_combine(), it perturbs the + * result enough for our purposes. + */ +#if SIZEOF_DATUM == 8 + return hash_combine64(murmurhash64((uint64) value), (uint64) kind); +#else + return hash_combine(murmurhash32((uint32) value), (uint32) kind); +#endif } static void diff --git a/src/include/common/hashfn.h b/src/include/common/hashfn.h index c634cc067a1..009ffbbdd38 100644 --- a/src/include/common/hashfn.h +++ b/src/include/common/hashfn.h @@ -101,4 +101,19 @@ murmurhash32(uint32 data) return h; } +/* 64-bit variant */ +static inline uint64 +murmurhash64(uint64 data) +{ + uint64 h = data; + + h ^= h >> 33; + h *= 0xff51afd7ed558ccd; + h ^= h >> 33; + h *= 0xc4ceb9fe1a85ec53; + h ^= h >> 33; + + return h; +} + #endif /* HASHFN_H */ -- 2.30.1 --------------C6A03312D0F99F9183A0DF46-- ^ permalink raw reply [nested|flat] 48+ messages in thread
* [PATCH v6 3/3] Optimize hash function @ 2021-01-20 22:06 Heikki Linnakangas <[email protected]> 0 siblings, 0 replies; 48+ messages in thread From: Heikki Linnakangas @ 2021-01-20 22:06 UTC (permalink / raw) --- src/backend/utils/resowner/resowner.c | 24 ++++++++++++++++++------ src/include/common/hashfn.h | 15 +++++++++++++++ 2 files changed, 33 insertions(+), 6 deletions(-) diff --git a/src/backend/utils/resowner/resowner.c b/src/backend/utils/resowner/resowner.c index 1db1e7cf14b..72e4d8a784c 100644 --- a/src/backend/utils/resowner/resowner.c +++ b/src/backend/utils/resowner/resowner.c @@ -163,15 +163,27 @@ static void ReleaseAuxProcessResourcesCallback(int code, Datum arg); * INTERNAL ROUTINES * *****************************************************************************/ +/* + * Hash function for value+kind combination. + */ static inline uint32 hash_resource_elem(Datum value, ResourceOwnerFuncs *kind) { - Datum data[2]; - - data[0] = value; - data[1] = PointerGetDatum(kind); - - return hash_bytes((unsigned char *) &data, 2 * SIZEOF_DATUM); + /* + * Most resources types store a pointer in 'value', and pointers are + * unique all on their own. But some resources store plain integers (Files + * and Buffers as of this writing), so we want to incorporate the 'kind' + * in the hash too, otherwise those resources will collide a lot. But + * because there are only a few resource kinds like that - and only a few + * resource kinds to begin with - we don't need to work too hard to mix + * 'kind' into the hash. Just add it with hash_combine(), it perturbs the + * result enough for our purposes. + */ +#if SIZEOF_DATUM == 8 + return hash_combine64(murmurhash64((uint64) value), (uint64) kind); +#else + return hash_combine(murmurhash32((uint32) value), (uint32) kind); +#endif } static void diff --git a/src/include/common/hashfn.h b/src/include/common/hashfn.h index c634cc067a1..009ffbbdd38 100644 --- a/src/include/common/hashfn.h +++ b/src/include/common/hashfn.h @@ -101,4 +101,19 @@ murmurhash32(uint32 data) return h; } +/* 64-bit variant */ +static inline uint64 +murmurhash64(uint64 data) +{ + uint64 h = data; + + h ^= h >> 33; + h *= 0xff51afd7ed558ccd; + h ^= h >> 33; + h *= 0xc4ceb9fe1a85ec53; + h ^= h >> 33; + + return h; +} + #endif /* HASHFN_H */ -- 2.29.2 --------------F30A5443A5D524A5CBC51AE7 Content-Type: text/x-patch; charset=UTF-8; name="v2-0001-resownerbench.patch" Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename="v2-0001-resownerbench.patch" ^ permalink raw reply [nested|flat] 48+ messages in thread
* [PATCH v7 3/3] Optimize hash function @ 2021-01-20 22:06 Heikki Linnakangas <[email protected]> 0 siblings, 0 replies; 48+ messages in thread From: Heikki Linnakangas @ 2021-01-20 22:06 UTC (permalink / raw) --- src/backend/utils/resowner/resowner.c | 24 ++++++++++++++++++------ src/include/common/hashfn.h | 15 +++++++++++++++ 2 files changed, 33 insertions(+), 6 deletions(-) diff --git a/src/backend/utils/resowner/resowner.c b/src/backend/utils/resowner/resowner.c index d62b1495dd3..6189d051072 100644 --- a/src/backend/utils/resowner/resowner.c +++ b/src/backend/utils/resowner/resowner.c @@ -163,15 +163,27 @@ static void ReleaseAuxProcessResourcesCallback(int code, Datum arg); * INTERNAL ROUTINES * *****************************************************************************/ +/* + * Hash function for value+kind combination. + */ static inline uint32 hash_resource_elem(Datum value, ResourceOwnerFuncs *kind) { - Datum data[2]; - - data[0] = value; - data[1] = PointerGetDatum(kind); - - return hash_bytes((unsigned char *) &data, 2 * SIZEOF_DATUM); + /* + * Most resources types store a pointer in 'value', and pointers are + * unique all on their own. But some resources store plain integers (Files + * and Buffers as of this writing), so we want to incorporate the 'kind' + * in the hash too, otherwise those resources will collide a lot. But + * because there are only a few resource kinds like that - and only a few + * resource kinds to begin with - we don't need to work too hard to mix + * 'kind' into the hash. Just add it with hash_combine(), it perturbs the + * result enough for our purposes. + */ +#if SIZEOF_DATUM == 8 + return hash_combine64(murmurhash64((uint64) value), (uint64) kind); +#else + return hash_combine(murmurhash32((uint32) value), (uint32) kind); +#endif } static void diff --git a/src/include/common/hashfn.h b/src/include/common/hashfn.h index c634cc067a1..009ffbbdd38 100644 --- a/src/include/common/hashfn.h +++ b/src/include/common/hashfn.h @@ -101,4 +101,19 @@ murmurhash32(uint32 data) return h; } +/* 64-bit variant */ +static inline uint64 +murmurhash64(uint64 data) +{ + uint64 h = data; + + h ^= h >> 33; + h *= 0xff51afd7ed558ccd; + h ^= h >> 33; + h *= 0xc4ceb9fe1a85ec53; + h ^= h >> 33; + + return h; +} + #endif /* HASHFN_H */ -- 2.30.1 --------------C6A03312D0F99F9183A0DF46-- ^ permalink raw reply [nested|flat] 48+ messages in thread
* [PATCH v6 3/3] Optimize hash function @ 2021-01-20 22:06 Heikki Linnakangas <[email protected]> 0 siblings, 0 replies; 48+ messages in thread From: Heikki Linnakangas @ 2021-01-20 22:06 UTC (permalink / raw) --- src/backend/utils/resowner/resowner.c | 24 ++++++++++++++++++------ src/include/common/hashfn.h | 15 +++++++++++++++ 2 files changed, 33 insertions(+), 6 deletions(-) diff --git a/src/backend/utils/resowner/resowner.c b/src/backend/utils/resowner/resowner.c index 1db1e7cf14b..72e4d8a784c 100644 --- a/src/backend/utils/resowner/resowner.c +++ b/src/backend/utils/resowner/resowner.c @@ -163,15 +163,27 @@ static void ReleaseAuxProcessResourcesCallback(int code, Datum arg); * INTERNAL ROUTINES * *****************************************************************************/ +/* + * Hash function for value+kind combination. + */ static inline uint32 hash_resource_elem(Datum value, ResourceOwnerFuncs *kind) { - Datum data[2]; - - data[0] = value; - data[1] = PointerGetDatum(kind); - - return hash_bytes((unsigned char *) &data, 2 * SIZEOF_DATUM); + /* + * Most resources types store a pointer in 'value', and pointers are + * unique all on their own. But some resources store plain integers (Files + * and Buffers as of this writing), so we want to incorporate the 'kind' + * in the hash too, otherwise those resources will collide a lot. But + * because there are only a few resource kinds like that - and only a few + * resource kinds to begin with - we don't need to work too hard to mix + * 'kind' into the hash. Just add it with hash_combine(), it perturbs the + * result enough for our purposes. + */ +#if SIZEOF_DATUM == 8 + return hash_combine64(murmurhash64((uint64) value), (uint64) kind); +#else + return hash_combine(murmurhash32((uint32) value), (uint32) kind); +#endif } static void diff --git a/src/include/common/hashfn.h b/src/include/common/hashfn.h index c634cc067a1..009ffbbdd38 100644 --- a/src/include/common/hashfn.h +++ b/src/include/common/hashfn.h @@ -101,4 +101,19 @@ murmurhash32(uint32 data) return h; } +/* 64-bit variant */ +static inline uint64 +murmurhash64(uint64 data) +{ + uint64 h = data; + + h ^= h >> 33; + h *= 0xff51afd7ed558ccd; + h ^= h >> 33; + h *= 0xc4ceb9fe1a85ec53; + h ^= h >> 33; + + return h; +} + #endif /* HASHFN_H */ -- 2.29.2 --------------F30A5443A5D524A5CBC51AE7 Content-Type: text/x-patch; charset=UTF-8; name="v2-0001-resownerbench.patch" Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename="v2-0001-resownerbench.patch" ^ permalink raw reply [nested|flat] 48+ messages in thread
* [PATCH v7 3/3] Optimize hash function @ 2021-01-20 22:06 Heikki Linnakangas <[email protected]> 0 siblings, 0 replies; 48+ messages in thread From: Heikki Linnakangas @ 2021-01-20 22:06 UTC (permalink / raw) --- src/backend/utils/resowner/resowner.c | 24 ++++++++++++++++++------ src/include/common/hashfn.h | 15 +++++++++++++++ 2 files changed, 33 insertions(+), 6 deletions(-) diff --git a/src/backend/utils/resowner/resowner.c b/src/backend/utils/resowner/resowner.c index d62b1495dd3..6189d051072 100644 --- a/src/backend/utils/resowner/resowner.c +++ b/src/backend/utils/resowner/resowner.c @@ -163,15 +163,27 @@ static void ReleaseAuxProcessResourcesCallback(int code, Datum arg); * INTERNAL ROUTINES * *****************************************************************************/ +/* + * Hash function for value+kind combination. + */ static inline uint32 hash_resource_elem(Datum value, ResourceOwnerFuncs *kind) { - Datum data[2]; - - data[0] = value; - data[1] = PointerGetDatum(kind); - - return hash_bytes((unsigned char *) &data, 2 * SIZEOF_DATUM); + /* + * Most resources types store a pointer in 'value', and pointers are + * unique all on their own. But some resources store plain integers (Files + * and Buffers as of this writing), so we want to incorporate the 'kind' + * in the hash too, otherwise those resources will collide a lot. But + * because there are only a few resource kinds like that - and only a few + * resource kinds to begin with - we don't need to work too hard to mix + * 'kind' into the hash. Just add it with hash_combine(), it perturbs the + * result enough for our purposes. + */ +#if SIZEOF_DATUM == 8 + return hash_combine64(murmurhash64((uint64) value), (uint64) kind); +#else + return hash_combine(murmurhash32((uint32) value), (uint32) kind); +#endif } static void diff --git a/src/include/common/hashfn.h b/src/include/common/hashfn.h index c634cc067a1..009ffbbdd38 100644 --- a/src/include/common/hashfn.h +++ b/src/include/common/hashfn.h @@ -101,4 +101,19 @@ murmurhash32(uint32 data) return h; } +/* 64-bit variant */ +static inline uint64 +murmurhash64(uint64 data) +{ + uint64 h = data; + + h ^= h >> 33; + h *= 0xff51afd7ed558ccd; + h ^= h >> 33; + h *= 0xc4ceb9fe1a85ec53; + h ^= h >> 33; + + return h; +} + #endif /* HASHFN_H */ -- 2.30.1 --------------C6A03312D0F99F9183A0DF46-- ^ permalink raw reply [nested|flat] 48+ messages in thread
* [PATCH v6 3/3] Optimize hash function @ 2021-01-20 22:06 Heikki Linnakangas <[email protected]> 0 siblings, 0 replies; 48+ messages in thread From: Heikki Linnakangas @ 2021-01-20 22:06 UTC (permalink / raw) --- src/backend/utils/resowner/resowner.c | 24 ++++++++++++++++++------ src/include/common/hashfn.h | 15 +++++++++++++++ 2 files changed, 33 insertions(+), 6 deletions(-) diff --git a/src/backend/utils/resowner/resowner.c b/src/backend/utils/resowner/resowner.c index 1db1e7cf14b..72e4d8a784c 100644 --- a/src/backend/utils/resowner/resowner.c +++ b/src/backend/utils/resowner/resowner.c @@ -163,15 +163,27 @@ static void ReleaseAuxProcessResourcesCallback(int code, Datum arg); * INTERNAL ROUTINES * *****************************************************************************/ +/* + * Hash function for value+kind combination. + */ static inline uint32 hash_resource_elem(Datum value, ResourceOwnerFuncs *kind) { - Datum data[2]; - - data[0] = value; - data[1] = PointerGetDatum(kind); - - return hash_bytes((unsigned char *) &data, 2 * SIZEOF_DATUM); + /* + * Most resources types store a pointer in 'value', and pointers are + * unique all on their own. But some resources store plain integers (Files + * and Buffers as of this writing), so we want to incorporate the 'kind' + * in the hash too, otherwise those resources will collide a lot. But + * because there are only a few resource kinds like that - and only a few + * resource kinds to begin with - we don't need to work too hard to mix + * 'kind' into the hash. Just add it with hash_combine(), it perturbs the + * result enough for our purposes. + */ +#if SIZEOF_DATUM == 8 + return hash_combine64(murmurhash64((uint64) value), (uint64) kind); +#else + return hash_combine(murmurhash32((uint32) value), (uint32) kind); +#endif } static void diff --git a/src/include/common/hashfn.h b/src/include/common/hashfn.h index c634cc067a1..009ffbbdd38 100644 --- a/src/include/common/hashfn.h +++ b/src/include/common/hashfn.h @@ -101,4 +101,19 @@ murmurhash32(uint32 data) return h; } +/* 64-bit variant */ +static inline uint64 +murmurhash64(uint64 data) +{ + uint64 h = data; + + h ^= h >> 33; + h *= 0xff51afd7ed558ccd; + h ^= h >> 33; + h *= 0xc4ceb9fe1a85ec53; + h ^= h >> 33; + + return h; +} + #endif /* HASHFN_H */ -- 2.29.2 --------------F30A5443A5D524A5CBC51AE7 Content-Type: text/x-patch; charset=UTF-8; name="v2-0001-resownerbench.patch" Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename="v2-0001-resownerbench.patch" ^ permalink raw reply [nested|flat] 48+ messages in thread
* [PATCH v7 3/3] Optimize hash function @ 2021-01-20 22:06 Heikki Linnakangas <[email protected]> 0 siblings, 0 replies; 48+ messages in thread From: Heikki Linnakangas @ 2021-01-20 22:06 UTC (permalink / raw) --- src/backend/utils/resowner/resowner.c | 24 ++++++++++++++++++------ src/include/common/hashfn.h | 15 +++++++++++++++ 2 files changed, 33 insertions(+), 6 deletions(-) diff --git a/src/backend/utils/resowner/resowner.c b/src/backend/utils/resowner/resowner.c index d62b1495dd3..6189d051072 100644 --- a/src/backend/utils/resowner/resowner.c +++ b/src/backend/utils/resowner/resowner.c @@ -163,15 +163,27 @@ static void ReleaseAuxProcessResourcesCallback(int code, Datum arg); * INTERNAL ROUTINES * *****************************************************************************/ +/* + * Hash function for value+kind combination. + */ static inline uint32 hash_resource_elem(Datum value, ResourceOwnerFuncs *kind) { - Datum data[2]; - - data[0] = value; - data[1] = PointerGetDatum(kind); - - return hash_bytes((unsigned char *) &data, 2 * SIZEOF_DATUM); + /* + * Most resources types store a pointer in 'value', and pointers are + * unique all on their own. But some resources store plain integers (Files + * and Buffers as of this writing), so we want to incorporate the 'kind' + * in the hash too, otherwise those resources will collide a lot. But + * because there are only a few resource kinds like that - and only a few + * resource kinds to begin with - we don't need to work too hard to mix + * 'kind' into the hash. Just add it with hash_combine(), it perturbs the + * result enough for our purposes. + */ +#if SIZEOF_DATUM == 8 + return hash_combine64(murmurhash64((uint64) value), (uint64) kind); +#else + return hash_combine(murmurhash32((uint32) value), (uint32) kind); +#endif } static void diff --git a/src/include/common/hashfn.h b/src/include/common/hashfn.h index c634cc067a1..009ffbbdd38 100644 --- a/src/include/common/hashfn.h +++ b/src/include/common/hashfn.h @@ -101,4 +101,19 @@ murmurhash32(uint32 data) return h; } +/* 64-bit variant */ +static inline uint64 +murmurhash64(uint64 data) +{ + uint64 h = data; + + h ^= h >> 33; + h *= 0xff51afd7ed558ccd; + h ^= h >> 33; + h *= 0xc4ceb9fe1a85ec53; + h ^= h >> 33; + + return h; +} + #endif /* HASHFN_H */ -- 2.30.1 --------------C6A03312D0F99F9183A0DF46-- ^ permalink raw reply [nested|flat] 48+ messages in thread
* [PATCH v6 3/3] Optimize hash function @ 2021-01-20 22:06 Heikki Linnakangas <[email protected]> 0 siblings, 0 replies; 48+ messages in thread From: Heikki Linnakangas @ 2021-01-20 22:06 UTC (permalink / raw) --- src/backend/utils/resowner/resowner.c | 24 ++++++++++++++++++------ src/include/common/hashfn.h | 15 +++++++++++++++ 2 files changed, 33 insertions(+), 6 deletions(-) diff --git a/src/backend/utils/resowner/resowner.c b/src/backend/utils/resowner/resowner.c index 1db1e7cf14b..72e4d8a784c 100644 --- a/src/backend/utils/resowner/resowner.c +++ b/src/backend/utils/resowner/resowner.c @@ -163,15 +163,27 @@ static void ReleaseAuxProcessResourcesCallback(int code, Datum arg); * INTERNAL ROUTINES * *****************************************************************************/ +/* + * Hash function for value+kind combination. + */ static inline uint32 hash_resource_elem(Datum value, ResourceOwnerFuncs *kind) { - Datum data[2]; - - data[0] = value; - data[1] = PointerGetDatum(kind); - - return hash_bytes((unsigned char *) &data, 2 * SIZEOF_DATUM); + /* + * Most resources types store a pointer in 'value', and pointers are + * unique all on their own. But some resources store plain integers (Files + * and Buffers as of this writing), so we want to incorporate the 'kind' + * in the hash too, otherwise those resources will collide a lot. But + * because there are only a few resource kinds like that - and only a few + * resource kinds to begin with - we don't need to work too hard to mix + * 'kind' into the hash. Just add it with hash_combine(), it perturbs the + * result enough for our purposes. + */ +#if SIZEOF_DATUM == 8 + return hash_combine64(murmurhash64((uint64) value), (uint64) kind); +#else + return hash_combine(murmurhash32((uint32) value), (uint32) kind); +#endif } static void diff --git a/src/include/common/hashfn.h b/src/include/common/hashfn.h index c634cc067a1..009ffbbdd38 100644 --- a/src/include/common/hashfn.h +++ b/src/include/common/hashfn.h @@ -101,4 +101,19 @@ murmurhash32(uint32 data) return h; } +/* 64-bit variant */ +static inline uint64 +murmurhash64(uint64 data) +{ + uint64 h = data; + + h ^= h >> 33; + h *= 0xff51afd7ed558ccd; + h ^= h >> 33; + h *= 0xc4ceb9fe1a85ec53; + h ^= h >> 33; + + return h; +} + #endif /* HASHFN_H */ -- 2.29.2 --------------F30A5443A5D524A5CBC51AE7 Content-Type: text/x-patch; charset=UTF-8; name="v2-0001-resownerbench.patch" Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename="v2-0001-resownerbench.patch" ^ permalink raw reply [nested|flat] 48+ messages in thread
* [PATCH v7 3/3] Optimize hash function @ 2021-01-20 22:06 Heikki Linnakangas <[email protected]> 0 siblings, 0 replies; 48+ messages in thread From: Heikki Linnakangas @ 2021-01-20 22:06 UTC (permalink / raw) --- src/backend/utils/resowner/resowner.c | 24 ++++++++++++++++++------ src/include/common/hashfn.h | 15 +++++++++++++++ 2 files changed, 33 insertions(+), 6 deletions(-) diff --git a/src/backend/utils/resowner/resowner.c b/src/backend/utils/resowner/resowner.c index d62b1495dd3..6189d051072 100644 --- a/src/backend/utils/resowner/resowner.c +++ b/src/backend/utils/resowner/resowner.c @@ -163,15 +163,27 @@ static void ReleaseAuxProcessResourcesCallback(int code, Datum arg); * INTERNAL ROUTINES * *****************************************************************************/ +/* + * Hash function for value+kind combination. + */ static inline uint32 hash_resource_elem(Datum value, ResourceOwnerFuncs *kind) { - Datum data[2]; - - data[0] = value; - data[1] = PointerGetDatum(kind); - - return hash_bytes((unsigned char *) &data, 2 * SIZEOF_DATUM); + /* + * Most resources types store a pointer in 'value', and pointers are + * unique all on their own. But some resources store plain integers (Files + * and Buffers as of this writing), so we want to incorporate the 'kind' + * in the hash too, otherwise those resources will collide a lot. But + * because there are only a few resource kinds like that - and only a few + * resource kinds to begin with - we don't need to work too hard to mix + * 'kind' into the hash. Just add it with hash_combine(), it perturbs the + * result enough for our purposes. + */ +#if SIZEOF_DATUM == 8 + return hash_combine64(murmurhash64((uint64) value), (uint64) kind); +#else + return hash_combine(murmurhash32((uint32) value), (uint32) kind); +#endif } static void diff --git a/src/include/common/hashfn.h b/src/include/common/hashfn.h index c634cc067a1..009ffbbdd38 100644 --- a/src/include/common/hashfn.h +++ b/src/include/common/hashfn.h @@ -101,4 +101,19 @@ murmurhash32(uint32 data) return h; } +/* 64-bit variant */ +static inline uint64 +murmurhash64(uint64 data) +{ + uint64 h = data; + + h ^= h >> 33; + h *= 0xff51afd7ed558ccd; + h ^= h >> 33; + h *= 0xc4ceb9fe1a85ec53; + h ^= h >> 33; + + return h; +} + #endif /* HASHFN_H */ -- 2.30.1 --------------C6A03312D0F99F9183A0DF46-- ^ permalink raw reply [nested|flat] 48+ messages in thread
* [PATCH v6 3/3] Optimize hash function @ 2021-01-20 22:06 Heikki Linnakangas <[email protected]> 0 siblings, 0 replies; 48+ messages in thread From: Heikki Linnakangas @ 2021-01-20 22:06 UTC (permalink / raw) --- src/backend/utils/resowner/resowner.c | 24 ++++++++++++++++++------ src/include/common/hashfn.h | 15 +++++++++++++++ 2 files changed, 33 insertions(+), 6 deletions(-) diff --git a/src/backend/utils/resowner/resowner.c b/src/backend/utils/resowner/resowner.c index 1db1e7cf14b..72e4d8a784c 100644 --- a/src/backend/utils/resowner/resowner.c +++ b/src/backend/utils/resowner/resowner.c @@ -163,15 +163,27 @@ static void ReleaseAuxProcessResourcesCallback(int code, Datum arg); * INTERNAL ROUTINES * *****************************************************************************/ +/* + * Hash function for value+kind combination. + */ static inline uint32 hash_resource_elem(Datum value, ResourceOwnerFuncs *kind) { - Datum data[2]; - - data[0] = value; - data[1] = PointerGetDatum(kind); - - return hash_bytes((unsigned char *) &data, 2 * SIZEOF_DATUM); + /* + * Most resources types store a pointer in 'value', and pointers are + * unique all on their own. But some resources store plain integers (Files + * and Buffers as of this writing), so we want to incorporate the 'kind' + * in the hash too, otherwise those resources will collide a lot. But + * because there are only a few resource kinds like that - and only a few + * resource kinds to begin with - we don't need to work too hard to mix + * 'kind' into the hash. Just add it with hash_combine(), it perturbs the + * result enough for our purposes. + */ +#if SIZEOF_DATUM == 8 + return hash_combine64(murmurhash64((uint64) value), (uint64) kind); +#else + return hash_combine(murmurhash32((uint32) value), (uint32) kind); +#endif } static void diff --git a/src/include/common/hashfn.h b/src/include/common/hashfn.h index c634cc067a1..009ffbbdd38 100644 --- a/src/include/common/hashfn.h +++ b/src/include/common/hashfn.h @@ -101,4 +101,19 @@ murmurhash32(uint32 data) return h; } +/* 64-bit variant */ +static inline uint64 +murmurhash64(uint64 data) +{ + uint64 h = data; + + h ^= h >> 33; + h *= 0xff51afd7ed558ccd; + h ^= h >> 33; + h *= 0xc4ceb9fe1a85ec53; + h ^= h >> 33; + + return h; +} + #endif /* HASHFN_H */ -- 2.29.2 --------------F30A5443A5D524A5CBC51AE7 Content-Type: text/x-patch; charset=UTF-8; name="v2-0001-resownerbench.patch" Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename="v2-0001-resownerbench.patch" ^ permalink raw reply [nested|flat] 48+ messages in thread
* [PATCH v7 3/3] Optimize hash function @ 2021-01-20 22:06 Heikki Linnakangas <[email protected]> 0 siblings, 0 replies; 48+ messages in thread From: Heikki Linnakangas @ 2021-01-20 22:06 UTC (permalink / raw) --- src/backend/utils/resowner/resowner.c | 24 ++++++++++++++++++------ src/include/common/hashfn.h | 15 +++++++++++++++ 2 files changed, 33 insertions(+), 6 deletions(-) diff --git a/src/backend/utils/resowner/resowner.c b/src/backend/utils/resowner/resowner.c index d62b1495dd3..6189d051072 100644 --- a/src/backend/utils/resowner/resowner.c +++ b/src/backend/utils/resowner/resowner.c @@ -163,15 +163,27 @@ static void ReleaseAuxProcessResourcesCallback(int code, Datum arg); * INTERNAL ROUTINES * *****************************************************************************/ +/* + * Hash function for value+kind combination. + */ static inline uint32 hash_resource_elem(Datum value, ResourceOwnerFuncs *kind) { - Datum data[2]; - - data[0] = value; - data[1] = PointerGetDatum(kind); - - return hash_bytes((unsigned char *) &data, 2 * SIZEOF_DATUM); + /* + * Most resources types store a pointer in 'value', and pointers are + * unique all on their own. But some resources store plain integers (Files + * and Buffers as of this writing), so we want to incorporate the 'kind' + * in the hash too, otherwise those resources will collide a lot. But + * because there are only a few resource kinds like that - and only a few + * resource kinds to begin with - we don't need to work too hard to mix + * 'kind' into the hash. Just add it with hash_combine(), it perturbs the + * result enough for our purposes. + */ +#if SIZEOF_DATUM == 8 + return hash_combine64(murmurhash64((uint64) value), (uint64) kind); +#else + return hash_combine(murmurhash32((uint32) value), (uint32) kind); +#endif } static void diff --git a/src/include/common/hashfn.h b/src/include/common/hashfn.h index c634cc067a1..009ffbbdd38 100644 --- a/src/include/common/hashfn.h +++ b/src/include/common/hashfn.h @@ -101,4 +101,19 @@ murmurhash32(uint32 data) return h; } +/* 64-bit variant */ +static inline uint64 +murmurhash64(uint64 data) +{ + uint64 h = data; + + h ^= h >> 33; + h *= 0xff51afd7ed558ccd; + h ^= h >> 33; + h *= 0xc4ceb9fe1a85ec53; + h ^= h >> 33; + + return h; +} + #endif /* HASHFN_H */ -- 2.30.1 --------------C6A03312D0F99F9183A0DF46-- ^ permalink raw reply [nested|flat] 48+ messages in thread
* [PATCH v10 3/3] Optimize hash function @ 2021-10-18 10:59 Heikki Linnakangas <[email protected]> 0 siblings, 0 replies; 48+ messages in thread From: Heikki Linnakangas @ 2021-10-18 10:59 UTC (permalink / raw) --- src/backend/utils/resowner/resowner.c | 24 ++++++++++++++++++------ src/include/common/hashfn.h | 15 +++++++++++++++ 2 files changed, 33 insertions(+), 6 deletions(-) diff --git a/src/backend/utils/resowner/resowner.c b/src/backend/utils/resowner/resowner.c index 35e824d4534..118bd2b34c9 100644 --- a/src/backend/utils/resowner/resowner.c +++ b/src/backend/utils/resowner/resowner.c @@ -169,15 +169,27 @@ static void ReleaseAuxProcessResourcesCallback(int code, Datum arg); * INTERNAL ROUTINES * *****************************************************************************/ +/* + * Hash function for value+kind combination. + */ static inline uint32 hash_resource_elem(Datum value, ResourceOwnerFuncs *kind) { - Datum data[2]; - - data[0] = value; - data[1] = PointerGetDatum(kind); - - return hash_bytes((unsigned char *) &data, 2 * SIZEOF_DATUM); + /* + * Most resources types store a pointer in 'value', and pointers are + * unique all on their own. But some resources store plain integers + * (Files and Buffers as of this writing), so we want to incorporate the + * 'kind' in the hash too, otherwise those resources will collide a lot. + * But because there are only a few resource kinds like that - and only a + * few resource kinds to begin with - we don't need to work too hard to + * mix 'kind' into the hash. Just add it with hash_combine(), it perturbs + * the result enough for our purposes. + */ +#if SIZEOF_DATUM == 8 + return hash_combine64(murmurhash64((uint64) value), (uint64) kind); +#else + return hash_combine(murmurhash32((uint32) value), (uint32) kind); +#endif } /* diff --git a/src/include/common/hashfn.h b/src/include/common/hashfn.h index c634cc067a1..009ffbbdd38 100644 --- a/src/include/common/hashfn.h +++ b/src/include/common/hashfn.h @@ -101,4 +101,19 @@ murmurhash32(uint32 data) return h; } +/* 64-bit variant */ +static inline uint64 +murmurhash64(uint64 data) +{ + uint64 h = data; + + h ^= h >> 33; + h *= 0xff51afd7ed558ccd; + h ^= h >> 33; + h *= 0xc4ceb9fe1a85ec53; + h ^= h >> 33; + + return h; +} + #endif /* HASHFN_H */ -- 2.30.2 --------------AF226508B1039DE40796DE51-- ^ permalink raw reply [nested|flat] 48+ messages in thread
* Use heap scan routines directly in vac_update_datfrozenxid() @ 2024-10-06 20:39 Soumyadeep Chakraborty <[email protected]> 0 siblings, 0 replies; 48+ messages in thread From: Soumyadeep Chakraborty @ 2024-10-06 20:39 UTC (permalink / raw) To: pgsql-hackers Hi hackers, Attached is a simple patch to directly use heap scan routines in vac_update_datfrozenxid(), avoiding the multilayer overhead from the sysscan infrastructure. The speedup can be noticeable in databases containing a large number of relations (perhaps due to heavy partition table usage). This was proposed in [1]. Experiment setup: * Use -O3 optimized build without asserts, with fsync and autovacuum off, on my laptop. Other gucs are all at defaults. * Create tables using pgbench to inflate pg_class's to a decent size. $ cat << EOF > bench.sql > select txid_current() AS txid \gset > CREATE TABLE t:txid(a int); > EOF $ pgbench -f ./bench.sql -t 200000 -c 100 -n bench select pg_size_pretty(pg_relation_size('pg_class')); pg_size_pretty ---------------- 3508 MB (1 row) * Use instr_time to record the scan time. See attached instr_vac.diff. * Run vacuum on any of the created empty tables in the database bench: Results: * main as of 68dfecbef2: bench=# vacuum t1624; NOTICE: scan took 796.862142 ms bench=# vacuum t1624; NOTICE: scan took 793.730688 ms bench=# vacuum t1624; NOTICE: scan took 793.963655 ms * patch: bench=# vacuum t1624; NOTICE: scan took 682.283366 ms bench=# vacuum t1624; NOTICE: scan took 670.816975 ms bench=# vacuum t1624; NOTICE: scan took 683.821717 ms Regards, Soumyadeep (Broadcom) [1] https://www.postgresql.org/message-id/20221229030329.fbpiitatmowzza6c%40awork3.anarazel.de Attachments: [text/x-patch] v1-0001-Use-heap_getnext-in-vac_update_datfrozenxid.patch (1.6K, ../../CAE-ML+8kNjRWO9YwWie2DdSbmRAgMPUO5mqtq0A15Uj3-MZXfA@mail.gmail.com/2-v1-0001-Use-heap_getnext-in-vac_update_datfrozenxid.patch) download | inline diff: From 320e54894a1ad45e2c25a4ee88a6409a9dc1a527 Mon Sep 17 00:00:00 2001 From: Soumyadeep Chakraborty <[email protected]> Date: Sat, 5 Oct 2024 16:22:56 -0700 Subject: [PATCH v1 1/1] Use heap_getnext() in vac_update_datfrozenxid() Since we are going to do a full sequential scan without a filter, we can avoid overhead from the extra layers of sysscan. --- src/backend/commands/vacuum.c | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/src/backend/commands/vacuum.c b/src/backend/commands/vacuum.c index ac8f5d9c25..717e310054 100644 --- a/src/backend/commands/vacuum.c +++ b/src/backend/commands/vacuum.c @@ -1588,7 +1588,7 @@ vac_update_datfrozenxid(void) HeapTuple tuple; Form_pg_database dbform; Relation relation; - SysScanDesc scan; + TableScanDesc scan; HeapTuple classTup; TransactionId newFrozenXid; MultiXactId newMinMulti; @@ -1638,10 +1638,9 @@ vac_update_datfrozenxid(void) */ relation = table_open(RelationRelationId, AccessShareLock); - scan = systable_beginscan(relation, InvalidOid, false, - NULL, 0, NULL); + scan = table_beginscan_catalog(relation, 0, NULL); - while ((classTup = systable_getnext(scan)) != NULL) + while ((classTup = heap_getnext(scan, ForwardScanDirection)) != NULL) { volatile FormData_pg_class *classForm = (Form_pg_class) GETSTRUCT(classTup); TransactionId relfrozenxid = classForm->relfrozenxid; @@ -1707,7 +1706,7 @@ vac_update_datfrozenxid(void) } /* we're done with pg_class */ - systable_endscan(scan); + table_endscan(scan); table_close(relation, AccessShareLock); /* chicken out if bogus data found */ -- 2.43.0 [text/x-patch] instr_vac.diff (1.1K, ../../CAE-ML+8kNjRWO9YwWie2DdSbmRAgMPUO5mqtq0A15Uj3-MZXfA@mail.gmail.com/3-instr_vac.diff) download | inline diff: diff --git a/src/backend/commands/vacuum.c b/src/backend/commands/vacuum.c index 717e310054..db3e8a4baf 100644 --- a/src/backend/commands/vacuum.c +++ b/src/backend/commands/vacuum.c @@ -1599,6 +1599,9 @@ vac_update_datfrozenxid(void) ScanKeyData key[1]; void *inplace_state; + instr_time before; + instr_time after; + /* * Restrict this task to one backend per database. This avoids race * conditions that would move datfrozenxid or datminmxid backward. It @@ -1636,6 +1639,7 @@ vac_update_datfrozenxid(void) * * See vac_truncate_clog() for the race condition to prevent. */ + INSTR_TIME_SET_CURRENT(before); relation = table_open(RelationRelationId, AccessShareLock); scan = table_beginscan_catalog(relation, 0, NULL); @@ -1708,7 +1712,9 @@ vac_update_datfrozenxid(void) /* we're done with pg_class */ table_endscan(scan); table_close(relation, AccessShareLock); - + INSTR_TIME_SET_CURRENT(after); + INSTR_TIME_SUBTRACT(after, before); + elog(NOTICE, "scan took %lf", INSTR_TIME_GET_MILLISEC(after)); /* chicken out if bogus data found */ if (bogus) return; ^ permalink raw reply [nested|flat] 48+ messages in thread
end of thread, other threads:[~2024-10-06 20:39 UTC | newest] Thread overview: 48+ messages (download: mbox mbox.gz follow: Atom feed) -- links below jump to the message on this page -- 2021-01-20 22:06 [PATCH v6 3/3] Optimize hash function Heikki Linnakangas <[email protected]> 2021-01-20 22:06 [PATCH v6 3/3] Optimize hash function Heikki Linnakangas <[email protected]> 2021-01-20 22:06 [PATCH v7 3/3] Optimize hash function Heikki Linnakangas <[email protected]> 2021-01-20 22:06 [PATCH v6 3/3] Optimize hash function Heikki Linnakangas <[email protected]> 2021-01-20 22:06 [PATCH v7 3/3] Optimize hash function Heikki Linnakangas <[email protected]> 2021-01-20 22:06 [PATCH v7 3/3] Optimize hash function Heikki Linnakangas <[email protected]> 2021-01-20 22:06 [PATCH v7 3/3] Optimize hash function Heikki Linnakangas <[email protected]> 2021-01-20 22:06 [PATCH v6 3/3] Optimize hash function Heikki Linnakangas <[email protected]> 2021-01-20 22:06 [PATCH v6 3/3] Optimize hash function Heikki Linnakangas <[email protected]> 2021-01-20 22:06 [PATCH v7 3/3] Optimize hash function Heikki Linnakangas <[email protected]> 2021-01-20 22:06 [PATCH v6 3/3] Optimize hash function Heikki Linnakangas <[email protected]> 2021-01-20 22:06 [PATCH v6 3/3] Optimize hash function Heikki Linnakangas <[email protected]> 2021-01-20 22:06 [PATCH v6 3/3] Optimize hash function Heikki Linnakangas <[email protected]> 2021-01-20 22:06 [PATCH v7 3/3] Optimize hash function Heikki Linnakangas <[email protected]> 2021-01-20 22:06 [PATCH v7 3/3] Optimize hash function Heikki Linnakangas <[email protected]> 2021-01-20 22:06 [PATCH v7 3/3] Optimize hash function Heikki Linnakangas <[email protected]> 2021-01-20 22:06 [PATCH v7 3/3] Optimize hash function Heikki Linnakangas <[email protected]> 2021-01-20 22:06 [PATCH v6 3/3] Optimize hash function Heikki Linnakangas <[email protected]> 2021-01-20 22:06 [PATCH v7 3/3] Optimize hash function Heikki Linnakangas <[email protected]> 2021-01-20 22:06 [PATCH v6 3/3] Optimize hash function Heikki Linnakangas <[email protected]> 2021-01-20 22:06 [PATCH v6 3/3] Optimize hash function Heikki Linnakangas <[email protected]> 2021-01-20 22:06 [PATCH v6 3/3] Optimize hash function Heikki Linnakangas <[email protected]> 2021-01-20 22:06 [PATCH v7 3/3] Optimize hash function Heikki Linnakangas <[email protected]> 2021-01-20 22:06 [PATCH v7 3/3] Optimize hash function Heikki Linnakangas <[email protected]> 2021-01-20 22:06 [PATCH v7 3/3] Optimize hash function Heikki Linnakangas <[email protected]> 2021-01-20 22:06 [PATCH v6 3/3] Optimize hash function Heikki Linnakangas <[email protected]> 2021-01-20 22:06 [PATCH v7 3/3] Optimize hash function Heikki Linnakangas <[email protected]> 2021-01-20 22:06 [PATCH v6 3/3] Optimize hash function Heikki Linnakangas <[email protected]> 2021-01-20 22:06 [PATCH v6 3/3] Optimize hash function Heikki Linnakangas <[email protected]> 2021-01-20 22:06 [PATCH v7 3/3] Optimize hash function Heikki Linnakangas <[email protected]> 2021-01-20 22:06 [PATCH v7 3/3] Optimize hash function Heikki Linnakangas <[email protected]> 2021-01-20 22:06 [PATCH v8 3/3] Optimize hash function Heikki Linnakangas <[email protected]> 2021-01-20 22:06 [PATCH v6 3/3] Optimize hash function Heikki Linnakangas <[email protected]> 2021-01-20 22:06 [PATCH v6 3/3] Optimize hash function Heikki Linnakangas <[email protected]> 2021-01-20 22:06 [PATCH v7 3/3] Optimize hash function Heikki Linnakangas <[email protected]> 2021-01-20 22:06 [PATCH v6 3/3] Optimize hash function Heikki Linnakangas <[email protected]> 2021-01-20 22:06 [PATCH v7 3/3] Optimize hash function Heikki Linnakangas <[email protected]> 2021-01-20 22:06 [PATCH v7 3/3] Optimize hash function Heikki Linnakangas <[email protected]> 2021-01-20 22:06 [PATCH v7 3/3] Optimize hash function Heikki Linnakangas <[email protected]> 2021-01-20 22:06 [PATCH v7 3/3] Optimize hash function Heikki Linnakangas <[email protected]> 2021-01-20 22:06 [PATCH v6 3/3] Optimize hash function Heikki Linnakangas <[email protected]> 2021-01-20 22:06 [PATCH v7 3/3] Optimize hash function Heikki Linnakangas <[email protected]> 2021-01-20 22:06 [PATCH v6 3/3] Optimize hash function Heikki Linnakangas <[email protected]> 2021-01-20 22:06 [PATCH v7 3/3] Optimize hash function Heikki Linnakangas <[email protected]> 2021-01-20 22:06 [PATCH v7 3/3] Optimize hash function Heikki Linnakangas <[email protected]> 2021-01-20 22:06 [PATCH v6 3/3] Optimize hash function Heikki Linnakangas <[email protected]> 2021-10-18 10:59 [PATCH v10 3/3] Optimize hash function Heikki Linnakangas <[email protected]> 2024-10-06 20:39 Use heap scan routines directly in vac_update_datfrozenxid() Soumyadeep Chakraborty <[email protected]>
This inbox is served by agora; see mirroring instructions for how to clone and mirror all data and code used for this inbox