diff options
author | John Naylor | 2024-01-16 09:32:48 +0000 |
---|---|---|
committer | John Naylor | 2024-01-19 05:56:15 +0000 |
commit | 0aba2554409ee3251d7558567edd114d8ed36dcc (patch) | |
tree | 286b5418f1988aee89bda055367f91234e164abd /src/backend | |
parent | e97b672c88f6e5938a2b81021bd4b590b013976f (diff) |
Add optimized C string hashing
Given an already-initialized hash state and a NUL-terminated string,
accumulate the hash of the string into the hash state and return the
length for the caller to (optionally) save for the finalizer. This
avoids a strlen call.
If the string pointer is aligned, we can use a word-at-a-time
algorithm for NUL lookahead. The aligned case is only used on 64-bit
platforms, since it's not worth the extra complexity for 32-bit.
Handling the tail of the string after finishing the word-wise loop
was inspired by NetBSD's strlen(), but no code was taken since that
is written in assembly language.
As demonstration, use this in the search path cache. This brings the
general case performance closer to the special case optimization done
in commit a86c61c9ee. There are other places that could benefit, but
that is left for future work.
Jeff Davis and John Naylor
Reviewed by Heikki Linnakangas, Jian He, Junwang Zhao
Discussion: https://postgr.es/m/3820f030fd008ff14134b3e9ce5cc6dd623ed479.camel%40j-davis.com
Discussion: https://postgr.es/m/b40292c99e623defe5eadedab1d438cf51a4107c.camel%40j-davis.com
Diffstat (limited to 'src/backend')
-rw-r--r-- | src/backend/catalog/namespace.c | 20 |
1 files changed, 15 insertions, 5 deletions
diff --git a/src/backend/catalog/namespace.c b/src/backend/catalog/namespace.c index eecc50a958d..b610aa62423 100644 --- a/src/backend/catalog/namespace.c +++ b/src/backend/catalog/namespace.c @@ -41,7 +41,7 @@ #include "catalog/pg_ts_template.h" #include "catalog/pg_type.h" #include "commands/dbcommands.h" -#include "common/hashfn.h" +#include "common/hashfn_unstable.h" #include "funcapi.h" #include "mb/pg_wchar.h" #include "miscadmin.h" @@ -253,11 +253,21 @@ static bool MatchNamedCall(HeapTuple proctup, int nargs, List *argnames, static inline uint32 spcachekey_hash(SearchPathCacheKey key) { - const unsigned char *bytes = (const unsigned char *) key.searchPath; - int blen = strlen(key.searchPath); + fasthash_state hs; + int sp_len; - return hash_combine(hash_bytes(bytes, blen), - hash_uint32(key.roleid)); + fasthash_init(&hs, FH_UNKNOWN_LENGTH, 0); + + hs.accum = key.roleid; + fasthash_combine(&hs); + + /* + * Combine search path into the hash and save the length for tweaking the + * final mix. + */ + sp_len = fasthash_accum_cstring(&hs, key.searchPath); + + return fasthash_final32(&hs, sp_len); } static inline bool |