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

Commit f956ecd

Browse files
committed
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
1 parent db17594 commit f956ecd

File tree

5 files changed

+28
-58
lines changed

5 files changed

+28
-58
lines changed

src/bin/pg_combinebackup/load_manifest.c

+2-14
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
#include <sys/stat.h>
1616
#include <unistd.h>
1717

18-
#include "common/hashfn.h"
18+
#include "common/hashfn_unstable.h"
1919
#include "common/logging.h"
2020
#include "common/parse_manifest.h"
2121
#include "load_manifest.h"
@@ -44,12 +44,11 @@
4444
* Define a hash table which we can use to store information about the files
4545
* mentioned in the backup manifest.
4646
*/
47-
static uint32 hash_string_pointer(char *s);
4847
#define SH_PREFIX manifest_files
4948
#define SH_ELEMENT_TYPE manifest_file
5049
#define SH_KEY_TYPE char *
5150
#define SH_KEY pathname
52-
#define SH_HASH_KEY(tb, key) hash_string_pointer(key)
51+
#define SH_HASH_KEY(tb, key) hash_string(key)
5352
#define SH_EQUAL(tb, a, b) (strcmp(a, b) == 0)
5453
#define SH_SCOPE extern
5554
#define SH_RAW_ALLOCATOR pg_malloc0
@@ -311,14 +310,3 @@ combinebackup_per_wal_range_cb(JsonManifestParseContext *context,
311310
manifest->last_wal_range->next = range;
312311
manifest->last_wal_range = range;
313312
}
314-
315-
/*
316-
* Helper function for manifest_files hash table.
317-
*/
318-
static uint32
319-
hash_string_pointer(char *s)
320-
{
321-
unsigned char *ss = (unsigned char *) s;
322-
323-
return hash_bytes(ss, strlen(s));
324-
}

src/bin/pg_dump/pg_dumpall.c

+2-15
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
#include "catalog/pg_authid_d.h"
2222
#include "common/connect.h"
2323
#include "common/file_utils.h"
24-
#include "common/hashfn.h"
24+
#include "common/hashfn_unstable.h"
2525
#include "common/logging.h"
2626
#include "common/string.h"
2727
#include "dumputils.h"
@@ -33,8 +33,6 @@
3333
/* version string we expect back from pg_dump */
3434
#define PGDUMP_VERSIONSTR "pg_dump (PostgreSQL) " PG_VERSION "\n"
3535

36-
static uint32 hash_string_pointer(char *s);
37-
3836
typedef struct
3937
{
4038
uint32 status;
@@ -46,7 +44,7 @@ typedef struct
4644
#define SH_ELEMENT_TYPE RoleNameEntry
4745
#define SH_KEY_TYPE char *
4846
#define SH_KEY rolename
49-
#define SH_HASH_KEY(tb, key) hash_string_pointer(key)
47+
#define SH_HASH_KEY(tb, key) hash_string(key)
5048
#define SH_EQUAL(tb, a, b) (strcmp(a, b) == 0)
5149
#define SH_STORE_HASH
5250
#define SH_GET_HASH(tb, a) (a)->hashval
@@ -1938,17 +1936,6 @@ dumpTimestamp(const char *msg)
19381936
fprintf(OPF, "-- %s %s\n\n", msg, buf);
19391937
}
19401938

1941-
/*
1942-
* Helper function for rolename_hash hash table.
1943-
*/
1944-
static uint32
1945-
hash_string_pointer(char *s)
1946-
{
1947-
unsigned char *ss = (unsigned char *) s;
1948-
1949-
return hash_bytes(ss, strlen(s));
1950-
}
1951-
19521939
/*
19531940
* read_dumpall_filters - retrieve database identifier patterns from file
19541941
*

src/bin/pg_rewind/filemap.c

+2-15
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828

2929
#include "catalog/pg_tablespace_d.h"
3030
#include "common/file_utils.h"
31-
#include "common/hashfn.h"
31+
#include "common/hashfn_unstable.h"
3232
#include "common/string.h"
3333
#include "datapagemap.h"
3434
#include "filemap.h"
@@ -38,12 +38,11 @@
3838
* Define a hash table which we can use to store information about the files
3939
* appearing in source and target systems.
4040
*/
41-
static uint32 hash_string_pointer(const char *s);
4241
#define SH_PREFIX filehash
4342
#define SH_ELEMENT_TYPE file_entry_t
4443
#define SH_KEY_TYPE const char *
4544
#define SH_KEY path
46-
#define SH_HASH_KEY(tb, key) hash_string_pointer(key)
45+
#define SH_HASH_KEY(tb, key) hash_string(key)
4746
#define SH_EQUAL(tb, a, b) (strcmp(a, b) == 0)
4847
#define SH_SCOPE static inline
4948
#define SH_RAW_ALLOCATOR pg_malloc0
@@ -821,15 +820,3 @@ decide_file_actions(void)
821820

822821
return filemap;
823822
}
824-
825-
826-
/*
827-
* Helper function for filemap hash table.
828-
*/
829-
static uint32
830-
hash_string_pointer(const char *s)
831-
{
832-
unsigned char *ss = (unsigned char *) s;
833-
834-
return hash_bytes(ss, strlen(s));
835-
}

src/bin/pg_verifybackup/pg_verifybackup.c

+2-14
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
#include <time.h>
2020

2121
#include "common/controldata_utils.h"
22-
#include "common/hashfn.h"
22+
#include "common/hashfn_unstable.h"
2323
#include "common/logging.h"
2424
#include "common/parse_manifest.h"
2525
#include "fe_utils/simple_list.h"
@@ -68,12 +68,11 @@ typedef struct manifest_file
6868
* Define a hash table which we can use to store information about the files
6969
* mentioned in the backup manifest.
7070
*/
71-
static uint32 hash_string_pointer(char *s);
7271
#define SH_PREFIX manifest_files
7372
#define SH_ELEMENT_TYPE manifest_file
7473
#define SH_KEY_TYPE char *
7574
#define SH_KEY pathname
76-
#define SH_HASH_KEY(tb, key) hash_string_pointer(key)
75+
#define SH_HASH_KEY(tb, key) hash_string(key)
7776
#define SH_EQUAL(tb, a, b) (strcmp(a, b) == 0)
7877
#define SH_SCOPE static inline
7978
#define SH_RAW_ALLOCATOR pg_malloc0
@@ -1028,17 +1027,6 @@ should_ignore_relpath(verifier_context *context, char *relpath)
10281027
return false;
10291028
}
10301029

1031-
/*
1032-
* Helper function for manifest_files hash table.
1033-
*/
1034-
static uint32
1035-
hash_string_pointer(char *s)
1036-
{
1037-
unsigned char *ss = (unsigned char *) s;
1038-
1039-
return hash_bytes(ss, strlen(s));
1040-
}
1041-
10421030
/*
10431031
* Print a progress report based on the global variables.
10441032
*

src/include/common/hashfn_unstable.h

+20
Original file line numberDiff line numberDiff line change
@@ -370,4 +370,24 @@ fasthash32(const char *k, size_t len, uint64 seed)
370370
return fasthash_reduce32(fasthash64(k, len, seed));
371371
}
372372

373+
/*
374+
* Convenience function for hashing NUL-terminated strings
375+
*/
376+
static inline uint32
377+
hash_string(const char *s)
378+
{
379+
fasthash_state hs;
380+
size_t s_len;
381+
382+
fasthash_init(&hs, 0);
383+
384+
/*
385+
* Combine string into the hash and save the length for tweaking the final
386+
* mix.
387+
*/
388+
s_len = fasthash_accum_cstring(&hs, s);
389+
390+
return fasthash_final32(&hs, s_len);
391+
}
392+
373393
#endif /* HASHFN_UNSTABLE_H */

0 commit comments

Comments
 (0)