diff options
author | John Naylor | 2024-04-06 05:17:07 +0000 |
---|---|---|
committer | John Naylor | 2024-04-06 05:20:40 +0000 |
commit | f956ecd0353b2960f8322b2211142113fe2b6f67 (patch) | |
tree | 19ba604fb7f7cf34f3fcfbb4349810cb26a29030 | |
parent | db17594ad73a871a176a9bf96e0589c2cf57052c (diff) |
Convert uses of hash_string_pointer to fasthash equivalent
Remove duplicate hash_string_pointer() function definitions by creating
a new inline function hash_string() for this purpose.
This has the added advantage of avoiding strlen() calls when doing hash
lookup. It's not clear how many of these are perfomance-sensitive
enough to benefit from that, but the simplification is worth it on
its own.
Reviewed by Jeff Davis
Discussion: https://postgr.es/m/CANWCAZbg_XeSeY0a_PqWmWqeRATvzTzUNYRLeT%2Bbzs%2BYQdC92g%40mail.gmail.com
-rw-r--r-- | src/bin/pg_combinebackup/load_manifest.c | 16 | ||||
-rw-r--r-- | src/bin/pg_dump/pg_dumpall.c | 17 | ||||
-rw-r--r-- | src/bin/pg_rewind/filemap.c | 17 | ||||
-rw-r--r-- | src/bin/pg_verifybackup/pg_verifybackup.c | 16 | ||||
-rw-r--r-- | src/include/common/hashfn_unstable.h | 20 |
5 files changed, 28 insertions, 58 deletions
diff --git a/src/bin/pg_combinebackup/load_manifest.c b/src/bin/pg_combinebackup/load_manifest.c index 58677e5e3e7..9c9332cdd5c 100644 --- a/src/bin/pg_combinebackup/load_manifest.c +++ b/src/bin/pg_combinebackup/load_manifest.c @@ -15,7 +15,7 @@ #include <sys/stat.h> #include <unistd.h> -#include "common/hashfn.h" +#include "common/hashfn_unstable.h" #include "common/logging.h" #include "common/parse_manifest.h" #include "load_manifest.h" @@ -44,12 +44,11 @@ * Define a hash table which we can use to store information about the files * mentioned in the backup manifest. */ -static uint32 hash_string_pointer(char *s); #define SH_PREFIX manifest_files #define SH_ELEMENT_TYPE manifest_file #define SH_KEY_TYPE char * #define SH_KEY pathname -#define SH_HASH_KEY(tb, key) hash_string_pointer(key) +#define SH_HASH_KEY(tb, key) hash_string(key) #define SH_EQUAL(tb, a, b) (strcmp(a, b) == 0) #define SH_SCOPE extern #define SH_RAW_ALLOCATOR pg_malloc0 @@ -311,14 +310,3 @@ combinebackup_per_wal_range_cb(JsonManifestParseContext *context, manifest->last_wal_range->next = range; manifest->last_wal_range = range; } - -/* - * Helper function for manifest_files hash table. - */ -static uint32 -hash_string_pointer(char *s) -{ - unsigned char *ss = (unsigned char *) s; - - return hash_bytes(ss, strlen(s)); -} diff --git a/src/bin/pg_dump/pg_dumpall.c b/src/bin/pg_dump/pg_dumpall.c index 046c0dc3b36..73337f33923 100644 --- a/src/bin/pg_dump/pg_dumpall.c +++ b/src/bin/pg_dump/pg_dumpall.c @@ -21,7 +21,7 @@ #include "catalog/pg_authid_d.h" #include "common/connect.h" #include "common/file_utils.h" -#include "common/hashfn.h" +#include "common/hashfn_unstable.h" #include "common/logging.h" #include "common/string.h" #include "dumputils.h" @@ -33,8 +33,6 @@ /* version string we expect back from pg_dump */ #define PGDUMP_VERSIONSTR "pg_dump (PostgreSQL) " PG_VERSION "\n" -static uint32 hash_string_pointer(char *s); - typedef struct { uint32 status; @@ -46,7 +44,7 @@ typedef struct #define SH_ELEMENT_TYPE RoleNameEntry #define SH_KEY_TYPE char * #define SH_KEY rolename -#define SH_HASH_KEY(tb, key) hash_string_pointer(key) +#define SH_HASH_KEY(tb, key) hash_string(key) #define SH_EQUAL(tb, a, b) (strcmp(a, b) == 0) #define SH_STORE_HASH #define SH_GET_HASH(tb, a) (a)->hashval @@ -1939,17 +1937,6 @@ dumpTimestamp(const char *msg) } /* - * Helper function for rolename_hash hash table. - */ -static uint32 -hash_string_pointer(char *s) -{ - unsigned char *ss = (unsigned char *) s; - - return hash_bytes(ss, strlen(s)); -} - -/* * read_dumpall_filters - retrieve database identifier patterns from file * * Parse the specified filter file for include and exclude patterns, and add diff --git a/src/bin/pg_rewind/filemap.c b/src/bin/pg_rewind/filemap.c index 255ddf2ffaf..4458324c9d8 100644 --- a/src/bin/pg_rewind/filemap.c +++ b/src/bin/pg_rewind/filemap.c @@ -28,7 +28,7 @@ #include "catalog/pg_tablespace_d.h" #include "common/file_utils.h" -#include "common/hashfn.h" +#include "common/hashfn_unstable.h" #include "common/string.h" #include "datapagemap.h" #include "filemap.h" @@ -38,12 +38,11 @@ * Define a hash table which we can use to store information about the files * appearing in source and target systems. */ -static uint32 hash_string_pointer(const char *s); #define SH_PREFIX filehash #define SH_ELEMENT_TYPE file_entry_t #define SH_KEY_TYPE const char * #define SH_KEY path -#define SH_HASH_KEY(tb, key) hash_string_pointer(key) +#define SH_HASH_KEY(tb, key) hash_string(key) #define SH_EQUAL(tb, a, b) (strcmp(a, b) == 0) #define SH_SCOPE static inline #define SH_RAW_ALLOCATOR pg_malloc0 @@ -821,15 +820,3 @@ decide_file_actions(void) return filemap; } - - -/* - * Helper function for filemap hash table. - */ -static uint32 -hash_string_pointer(const char *s) -{ - unsigned char *ss = (unsigned char *) s; - - return hash_bytes(ss, strlen(s)); -} diff --git a/src/bin/pg_verifybackup/pg_verifybackup.c b/src/bin/pg_verifybackup/pg_verifybackup.c index 48f8b233262..90ef4b20379 100644 --- a/src/bin/pg_verifybackup/pg_verifybackup.c +++ b/src/bin/pg_verifybackup/pg_verifybackup.c @@ -19,7 +19,7 @@ #include <time.h> #include "common/controldata_utils.h" -#include "common/hashfn.h" +#include "common/hashfn_unstable.h" #include "common/logging.h" #include "common/parse_manifest.h" #include "fe_utils/simple_list.h" @@ -68,12 +68,11 @@ typedef struct manifest_file * Define a hash table which we can use to store information about the files * mentioned in the backup manifest. */ -static uint32 hash_string_pointer(char *s); #define SH_PREFIX manifest_files #define SH_ELEMENT_TYPE manifest_file #define SH_KEY_TYPE char * #define SH_KEY pathname -#define SH_HASH_KEY(tb, key) hash_string_pointer(key) +#define SH_HASH_KEY(tb, key) hash_string(key) #define SH_EQUAL(tb, a, b) (strcmp(a, b) == 0) #define SH_SCOPE static inline #define SH_RAW_ALLOCATOR pg_malloc0 @@ -1029,17 +1028,6 @@ should_ignore_relpath(verifier_context *context, char *relpath) } /* - * Helper function for manifest_files hash table. - */ -static uint32 -hash_string_pointer(char *s) -{ - unsigned char *ss = (unsigned char *) s; - - return hash_bytes(ss, strlen(s)); -} - -/* * Print a progress report based on the global variables. * * Progress report is written at maximum once per second, unless the finished diff --git a/src/include/common/hashfn_unstable.h b/src/include/common/hashfn_unstable.h index d7ab6eeefe7..c93c9d71792 100644 --- a/src/include/common/hashfn_unstable.h +++ b/src/include/common/hashfn_unstable.h @@ -370,4 +370,24 @@ fasthash32(const char *k, size_t len, uint64 seed) return fasthash_reduce32(fasthash64(k, len, seed)); } +/* + * Convenience function for hashing NUL-terminated strings + */ +static inline uint32 +hash_string(const char *s) +{ + fasthash_state hs; + size_t s_len; + + fasthash_init(&hs, 0); + + /* + * Combine string into the hash and save the length for tweaking the final + * mix. + */ + s_len = fasthash_accum_cstring(&hs, s); + + return fasthash_final32(&hs, s_len); +} + #endif /* HASHFN_UNSTABLE_H */ |