Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 954e435

Browse files
committed
Use a faster hash function in resource owners.
This buys back some of the performance loss that we otherwise saw from the previous commit. Reviewed-by: Aleksander Alekseev, Michael Paquier, Julien Rouhaud Reviewed-by: Kyotaro Horiguchi, Hayato Kuroda, Álvaro Herrera, Zhihong Yu Reviewed-by: Peter Eisentraut, Andres Freund Discussion: https://www.postgresql.org/message-id/d746cead-a1ef-7efe-fb47-933311e876a3%40iki.fi
1 parent b8bff07 commit 954e435

File tree

2 files changed

+33
-6
lines changed

2 files changed

+33
-6
lines changed

src/backend/utils/resowner/resowner.c

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -206,15 +206,27 @@ static void ReleaseAuxProcessResourcesCallback(int code, Datum arg);
206206
* INTERNAL ROUTINES *
207207
*****************************************************************************/
208208

209+
/*
210+
* Hash function for value+kind combination.
211+
*/
209212
static inline uint32
210213
hash_resource_elem(Datum value, const ResourceOwnerDesc *kind)
211214
{
212-
Datum data[2];
213-
214-
data[0] = value;
215-
data[1] = PointerGetDatum(kind);
216-
217-
return hash_bytes((unsigned char *) &data, 2 * SIZEOF_DATUM);
215+
/*
216+
* Most resource kinds store a pointer in 'value', and pointers are unique
217+
* all on their own. But some resources store plain integers (Files and
218+
* Buffers as of this writing), so we want to incorporate the 'kind' in
219+
* the hash too, otherwise those resources will collide a lot. But
220+
* because there are only a few resource kinds like that - and only a few
221+
* resource kinds to begin with - we don't need to work too hard to mix
222+
* 'kind' into the hash. Just add it with hash_combine(), it perturbs the
223+
* result enough for our purposes.
224+
*/
225+
#if SIZEOF_DATUM == 8
226+
return hash_combine64(murmurhash64((uint64) value), (uint64) kind);
227+
#else
228+
return hash_combine(murmurhash32((uint32) value), (uint32) kind);
229+
#endif
218230
}
219231

220232
/*

src/include/common/hashfn.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,4 +101,19 @@ murmurhash32(uint32 data)
101101
return h;
102102
}
103103

104+
/* 64-bit variant */
105+
static inline uint64
106+
murmurhash64(uint64 data)
107+
{
108+
uint64 h = data;
109+
110+
h ^= h >> 33;
111+
h *= 0xff51afd7ed558ccd;
112+
h ^= h >> 33;
113+
h *= 0xc4ceb9fe1a85ec53;
114+
h ^= h >> 33;
115+
116+
return h;
117+
}
118+
104119
#endif /* HASHFN_H */

0 commit comments

Comments
 (0)