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
* Re: Materialized view in Postgres from the variables rather than SQL query results @ 2023-12-01 13:40 David G. Johnston <[email protected]> 0 siblings, 0 replies; 48+ messages in thread From: David G. Johnston @ 2023-12-01 13:40 UTC (permalink / raw) To: Nurul Karim Rafi <[email protected]>; +Cc: [email protected] <[email protected]> This mailing list is for discussing the development of patches to the PostgreSQL code base. Please send your request for help to a more appropriate list - specifically the -general list. David J. On Thursday, November 30, 2023, Nurul Karim Rafi <[email protected]> wrote: > I have a stored procedure in Postgres. I have generated some variables in > that procedure. These variables are generated inside a loop of query > result. Suppose if i have 10 rows from my query then for 10 times I am > generating those variables. > > Now I want to create a materialized view where these variables will be the > columns and every row will have values of those variables generated in > every iteration of that loop. > > What can be the better approach for this? > > I can create a temporary table with those variables and insert values in > every iteration of that loop but that will not serve my purpose. Because I > want to drop my existing table where all the values are available and > columns are those variables. My target is to make a materialized view with > those variables so that I can get rid of that parent table. > > Best Regards > > *Rafi* > ^ permalink raw reply [nested|flat] 48+ messages in thread
end of thread, other threads:[~2023-12-01 13:40 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 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 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 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 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 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 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 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 v8 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]> 2023-12-01 13:40 Re: Materialized view in Postgres from the variables rather than SQL query results David G. Johnston <[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