mirror of
https://github.com/postgres/postgres.git
synced 2025-08-31 17:02:12 +03:00
Improve hash_create's API for selecting simple-binary-key hash functions.
Previously, if you wanted anything besides C-string hash keys, you had to specify a custom hashing function to hash_create(). Nearly all such callers were specifying tag_hash or oid_hash; which is tedious, and rather error-prone, since a caller could easily miss the opportunity to optimize by using hash_uint32 when appropriate. Replace this with a design whereby callers using simple binary-data keys just specify HASH_BLOBS and don't need to mess with specific support functions. hash_create() itself will take care of optimizing when the key size is four bytes. This nets out saving a few hundred bytes of code space, and offers a measurable performance improvement in tidbitmap.c (which was not exploiting the opportunity to use hash_uint32 for its 4-byte keys). There might be some wins elsewhere too, I didn't analyze closely. In future we could look into offering a similar optimized hashing function for 8-byte keys. Under this design that could be done in a centralized and machine-independent fashion, whereas getting it right for keys of platform-dependent sizes would've been notationally painful before. For the moment, the old way still works fine, so as not to break source code compatibility for loadable modules. Eventually we might want to remove tag_hash and friends from the exported API altogether, since there's no real need for them to be explicitly referenced from outside dynahash.c. Teodor Sigaev and Tom Lane
This commit is contained in:
@@ -26,6 +26,20 @@
|
||||
* in local memory, we typically use palloc() which will throw error on
|
||||
* failure. The code in this file has to cope with both cases.
|
||||
*
|
||||
* dynahash.c provides support for these types of lookup keys:
|
||||
*
|
||||
* 1. Null-terminated C strings (truncated if necessary to fit in keysize),
|
||||
* compared as though by strcmp(). This is the default behavior.
|
||||
*
|
||||
* 2. Arbitrary binary data of size keysize, compared as though by memcmp().
|
||||
* (Caller must ensure there are no undefined padding bits in the keys!)
|
||||
* This is selected by specifying HASH_BLOBS flag to hash_create.
|
||||
*
|
||||
* 3. More complex key behavior can be selected by specifying user-supplied
|
||||
* hashing, comparison, and/or key-copying functions. At least a hashing
|
||||
* function must be supplied; comparison defaults to memcmp() and key copying
|
||||
* to memcpy() when a user-defined hashing function is selected.
|
||||
*
|
||||
* Portions Copyright (c) 1996-2014, PostgreSQL Global Development Group
|
||||
* Portions Copyright (c) 1994, Regents of the University of California
|
||||
*
|
||||
@@ -305,15 +319,32 @@ hash_create(const char *tabname, long nelem, HASHCTL *info, int flags)
|
||||
hashp->tabname = (char *) (hashp + 1);
|
||||
strcpy(hashp->tabname, tabname);
|
||||
|
||||
/*
|
||||
* Select the appropriate hash function (see comments at head of file).
|
||||
*/
|
||||
if (flags & HASH_FUNCTION)
|
||||
hashp->hash = info->hash;
|
||||
else if (flags & HASH_BLOBS)
|
||||
{
|
||||
/* We can optimize hashing for common key sizes */
|
||||
Assert(flags & HASH_ELEM);
|
||||
if (info->keysize == sizeof(uint32))
|
||||
hashp->hash = uint32_hash;
|
||||
else
|
||||
hashp->hash = tag_hash;
|
||||
}
|
||||
else
|
||||
hashp->hash = string_hash; /* default hash function */
|
||||
|
||||
/*
|
||||
* If you don't specify a match function, it defaults to string_compare if
|
||||
* you used string_hash (either explicitly or by default) and to memcmp
|
||||
* otherwise. (Prior to PostgreSQL 7.4, memcmp was always used.)
|
||||
* otherwise.
|
||||
*
|
||||
* Note: explicitly specifying string_hash is deprecated, because this
|
||||
* might not work for callers in loadable modules on some platforms due to
|
||||
* referencing a trampoline instead of the string_hash function proper.
|
||||
* Just let it default, eh?
|
||||
*/
|
||||
if (flags & HASH_COMPARE)
|
||||
hashp->match = info->match;
|
||||
@@ -332,6 +363,7 @@ hash_create(const char *tabname, long nelem, HASHCTL *info, int flags)
|
||||
else
|
||||
hashp->keycopy = memcpy;
|
||||
|
||||
/* And select the entry allocation function, too. */
|
||||
if (flags & HASH_ALLOC)
|
||||
hashp->alloc = info->alloc;
|
||||
else
|
||||
|
@@ -55,15 +55,15 @@ tag_hash(const void *key, Size keysize)
|
||||
}
|
||||
|
||||
/*
|
||||
* oid_hash: hash function for keys that are OIDs
|
||||
* uint32_hash: hash function for keys that are uint32 or int32
|
||||
*
|
||||
* (tag_hash works for this case too, but is slower)
|
||||
*/
|
||||
uint32
|
||||
oid_hash(const void *key, Size keysize)
|
||||
uint32_hash(const void *key, Size keysize)
|
||||
{
|
||||
Assert(keysize == sizeof(Oid));
|
||||
return DatumGetUInt32(hash_uint32((uint32) *((const Oid *) key)));
|
||||
Assert(keysize == sizeof(uint32));
|
||||
return DatumGetUInt32(hash_uint32(*((const uint32 *) key)));
|
||||
}
|
||||
|
||||
/*
|
||||
|
Reference in New Issue
Block a user