diff options
| author | Robert Haas | 2020-02-27 03:55:41 +0000 |
|---|---|---|
| committer | Robert Haas | 2020-02-27 03:55:41 +0000 |
| commit | 05d8449e73694585b59f8b03aaa087f04cc4679a (patch) | |
| tree | 0198e4d1dfc2774dbc58d24f148af4dd8fb2cd47 /src/backend | |
| parent | 008cf040962c98c7c55d54c28dcb43c3c1d83c92 (diff) | |
Move src/backend/utils/hash/hashfn.c to src/common
This also involves renaming src/include/utils/hashutils.h, which
becomes src/include/common/hashfn.h. Perhaps an argument can be
made for keeping the hashutils.h name, but it seemed more
consistent to make it match the name of the file, and also more
descriptive of what is actually going on here.
Patch by me, reviewed by Suraj Kharage and Mark Dilger. Off-list
advice on how not to break the Windows build from Davinder Singh
and Amit Kapila.
Discussion: http://postgr.es/m/CA+TgmoaRiG4TXND8QuM6JXFRkM_1wL2ZNhzaUKsuec9-4yrkgw@mail.gmail.com
Diffstat (limited to 'src/backend')
31 files changed, 29 insertions, 722 deletions
diff --git a/src/backend/access/common/tupdesc.c b/src/backend/access/common/tupdesc.c index 00bb4cb53d..28835512f0 100644 --- a/src/backend/access/common/tupdesc.c +++ b/src/backend/access/common/tupdesc.c @@ -23,12 +23,12 @@ #include "access/tupdesc_details.h" #include "catalog/pg_collation.h" #include "catalog/pg_type.h" +#include "common/hashfn.h" #include "miscadmin.h" #include "parser/parse_type.h" #include "utils/acl.h" #include "utils/builtins.h" #include "utils/datum.h" -#include "utils/hashutils.h" #include "utils/resowner_private.h" #include "utils/syscache.h" diff --git a/src/backend/access/hash/hashfunc.c b/src/backend/access/hash/hashfunc.c index 5b517106ff..a8498226e3 100644 --- a/src/backend/access/hash/hashfunc.c +++ b/src/backend/access/hash/hashfunc.c @@ -28,8 +28,8 @@ #include "access/hash.h" #include "catalog/pg_collation.h" +#include "common/hashfn.h" #include "utils/builtins.h" -#include "utils/hashutils.h" #include "utils/pg_locale.h" /* diff --git a/src/backend/access/tablesample/bernoulli.c b/src/backend/access/tablesample/bernoulli.c index 46016fb5de..606730d6cb 100644 --- a/src/backend/access/tablesample/bernoulli.c +++ b/src/backend/access/tablesample/bernoulli.c @@ -28,9 +28,9 @@ #include "access/tsmapi.h" #include "catalog/pg_type.h" +#include "common/hashfn.h" #include "optimizer/optimizer.h" #include "utils/builtins.h" -#include "utils/hashutils.h" /* Private state */ diff --git a/src/backend/access/tablesample/system.c b/src/backend/access/tablesample/system.c index 8a5f03bfd3..29b7c0d3c2 100644 --- a/src/backend/access/tablesample/system.c +++ b/src/backend/access/tablesample/system.c @@ -29,9 +29,9 @@ #include "access/relscan.h" #include "access/tsmapi.h" #include "catalog/pg_type.h" +#include "common/hashfn.h" #include "optimizer/optimizer.h" #include "utils/builtins.h" -#include "utils/hashutils.h" /* Private state */ diff --git a/src/backend/commands/async.c b/src/backend/commands/async.c index 9aa2b61600..dae939a4ab 100644 --- a/src/backend/commands/async.c +++ b/src/backend/commands/async.c @@ -126,6 +126,7 @@ #include "access/xact.h" #include "catalog/pg_database.h" #include "commands/async.h" +#include "common/hashfn.h" #include "funcapi.h" #include "libpq/libpq.h" #include "libpq/pqformat.h" @@ -138,7 +139,6 @@ #include "storage/sinval.h" #include "tcop/tcopprot.h" #include "utils/builtins.h" -#include "utils/hashutils.h" #include "utils/memutils.h" #include "utils/ps_status.h" #include "utils/snapmgr.h" diff --git a/src/backend/executor/execGrouping.c b/src/backend/executor/execGrouping.c index de0205f4fc..f6c3e4cbde 100644 --- a/src/backend/executor/execGrouping.c +++ b/src/backend/executor/execGrouping.c @@ -19,9 +19,9 @@ #include "postgres.h" #include "access/parallel.h" +#include "common/hashfn.h" #include "executor/executor.h" #include "miscadmin.h" -#include "utils/hashutils.h" #include "utils/lsyscache.h" #include "utils/memutils.h" diff --git a/src/backend/lib/bloomfilter.c b/src/backend/lib/bloomfilter.c index 29b62e70e4..f040e83c01 100644 --- a/src/backend/lib/bloomfilter.c +++ b/src/backend/lib/bloomfilter.c @@ -35,9 +35,9 @@ #include <math.h> +#include "common/hashfn.h" #include "lib/bloomfilter.h" #include "port/pg_bitutils.h" -#include "utils/hashutils.h" #define MAX_HASH_FUNCS 10 diff --git a/src/backend/lib/dshash.c b/src/backend/lib/dshash.c index 5e0167d9b0..78ccf03217 100644 --- a/src/backend/lib/dshash.c +++ b/src/backend/lib/dshash.c @@ -31,11 +31,11 @@ #include "postgres.h" +#include "common/hashfn.h" #include "lib/dshash.h" #include "storage/ipc.h" #include "storage/lwlock.h" #include "utils/dsa.h" -#include "utils/hashutils.h" #include "utils/memutils.h" /* diff --git a/src/backend/nodes/bitmapset.c b/src/backend/nodes/bitmapset.c index f711e6c699..2719ea45a3 100644 --- a/src/backend/nodes/bitmapset.c +++ b/src/backend/nodes/bitmapset.c @@ -20,10 +20,10 @@ */ #include "postgres.h" +#include "common/hashfn.h" #include "nodes/bitmapset.h" #include "nodes/pg_list.h" #include "port/pg_bitutils.h" -#include "utils/hashutils.h" #define WORDNUM(x) ((x) / BITS_PER_BITMAPWORD) diff --git a/src/backend/nodes/tidbitmap.c b/src/backend/nodes/tidbitmap.c index e102589e74..ad4e071ca3 100644 --- a/src/backend/nodes/tidbitmap.c +++ b/src/backend/nodes/tidbitmap.c @@ -41,11 +41,11 @@ #include <limits.h> #include "access/htup_details.h" +#include "common/hashfn.h" #include "nodes/bitmapset.h" #include "nodes/tidbitmap.h" #include "storage/lwlock.h" #include "utils/dsa.h" -#include "utils/hashutils.h" /* * The maximum number of tuples per page is not large (typically 256 with diff --git a/src/backend/partitioning/partbounds.c b/src/backend/partitioning/partbounds.c index 54eb83a0d1..35953f23fa 100644 --- a/src/backend/partitioning/partbounds.c +++ b/src/backend/partitioning/partbounds.c @@ -21,6 +21,7 @@ #include "catalog/pg_inherits.h" #include "catalog/pg_type.h" #include "commands/tablecmds.h" +#include "common/hashfn.h" #include "executor/executor.h" #include "miscadmin.h" #include "nodes/makefuncs.h" @@ -32,7 +33,6 @@ #include "utils/builtins.h" #include "utils/datum.h" #include "utils/fmgroids.h" -#include "utils/hashutils.h" #include "utils/lsyscache.h" #include "utils/partcache.h" #include "utils/ruleutils.h" diff --git a/src/backend/storage/file/sharedfileset.c b/src/backend/storage/file/sharedfileset.c index 6666a25521..f7206c9175 100644 --- a/src/backend/storage/file/sharedfileset.c +++ b/src/backend/storage/file/sharedfileset.c @@ -22,11 +22,11 @@ #include "catalog/pg_tablespace.h" #include "commands/tablespace.h" +#include "common/hashfn.h" #include "miscadmin.h" #include "storage/dsm.h" #include "storage/sharedfileset.h" #include "utils/builtins.h" -#include "utils/hashutils.h" static void SharedFileSetOnDetach(dsm_segment *segment, Datum datum); static void SharedFileSetPath(char *path, SharedFileSet *fileset, Oid tablespace); diff --git a/src/backend/tsearch/ts_typanalyze.c b/src/backend/tsearch/ts_typanalyze.c index 24c6479f61..2eed0cd137 100644 --- a/src/backend/tsearch/ts_typanalyze.c +++ b/src/backend/tsearch/ts_typanalyze.c @@ -16,9 +16,9 @@ #include "catalog/pg_collation.h" #include "catalog/pg_operator.h" #include "commands/vacuum.h" +#include "common/hashfn.h" #include "tsearch/ts_type.h" #include "utils/builtins.h" -#include "utils/hashutils.h" /* A hash key for lexemes */ diff --git a/src/backend/utils/adt/acl.c b/src/backend/utils/adt/acl.c index cc4170efbf..bce1f1e0b1 100644 --- a/src/backend/utils/adt/acl.c +++ b/src/backend/utils/adt/acl.c @@ -26,6 +26,7 @@ #include "commands/dbcommands.h" #include "commands/proclang.h" #include "commands/tablespace.h" +#include "common/hashfn.h" #include "foreign/foreign.h" #include "funcapi.h" #include "lib/qunique.h" @@ -34,7 +35,6 @@ #include "utils/array.h" #include "utils/builtins.h" #include "utils/catcache.h" -#include "utils/hashutils.h" #include "utils/inval.h" #include "utils/lsyscache.h" #include "utils/memutils.h" diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 393ee991b0..0c55b68fbf 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -21,6 +21,7 @@ #include <time.h> #include "access/xact.h" +#include "common/hashfn.h" #include "libpq/pqformat.h" #include "miscadmin.h" #include "nodes/supportnodes.h" @@ -29,7 +30,6 @@ #include "utils/builtins.h" #include "utils/date.h" #include "utils/datetime.h" -#include "utils/hashutils.h" #include "utils/sortsupport.h" /* diff --git a/src/backend/utils/adt/jsonb_gin.c b/src/backend/utils/adt/jsonb_gin.c index 72a88121d5..63122edf2e 100644 --- a/src/backend/utils/adt/jsonb_gin.c +++ b/src/backend/utils/adt/jsonb_gin.c @@ -63,9 +63,9 @@ #include "access/stratnum.h" #include "catalog/pg_collation.h" #include "catalog/pg_type.h" +#include "common/hashfn.h" #include "miscadmin.h" #include "utils/builtins.h" -#include "utils/hashutils.h" #include "utils/jsonb.h" #include "utils/jsonpath.h" #include "utils/varlena.h" diff --git a/src/backend/utils/adt/jsonb_util.c b/src/backend/utils/adt/jsonb_util.c index edec657cd3..04b70c805b 100644 --- a/src/backend/utils/adt/jsonb_util.c +++ b/src/backend/utils/adt/jsonb_util.c @@ -15,11 +15,11 @@ #include "catalog/pg_collation.h" #include "catalog/pg_type.h" +#include "common/hashfn.h" #include "common/jsonapi.h" #include "miscadmin.h" #include "utils/builtins.h" #include "utils/datetime.h" -#include "utils/hashutils.h" #include "utils/json.h" #include "utils/jsonb.h" #include "utils/memutils.h" diff --git a/src/backend/utils/adt/mac.c b/src/backend/utils/adt/mac.c index f9eb0b57d8..8aeddc6863 100644 --- a/src/backend/utils/adt/mac.c +++ b/src/backend/utils/adt/mac.c @@ -13,12 +13,12 @@ #include "postgres.h" +#include "common/hashfn.h" #include "lib/hyperloglog.h" #include "libpq/pqformat.h" #include "port/pg_bswap.h" #include "utils/builtins.h" #include "utils/guc.h" -#include "utils/hashutils.h" #include "utils/inet.h" #include "utils/sortsupport.h" diff --git a/src/backend/utils/adt/mac8.c b/src/backend/utils/adt/mac8.c index 571eee920f..b7b2968b92 100644 --- a/src/backend/utils/adt/mac8.c +++ b/src/backend/utils/adt/mac8.c @@ -21,9 +21,9 @@ #include "postgres.h" +#include "common/hashfn.h" #include "libpq/pqformat.h" #include "utils/builtins.h" -#include "utils/hashutils.h" #include "utils/inet.h" /* diff --git a/src/backend/utils/adt/network.c b/src/backend/utils/adt/network.c index a6dd8b75aa..0ab54316f8 100644 --- a/src/backend/utils/adt/network.c +++ b/src/backend/utils/adt/network.c @@ -15,6 +15,7 @@ #include "access/stratnum.h" #include "catalog/pg_opfamily.h" #include "catalog/pg_type.h" +#include "common/hashfn.h" #include "common/ip.h" #include "lib/hyperloglog.h" #include "libpq/libpq-be.h" @@ -26,7 +27,6 @@ #include "utils/builtins.h" #include "utils/fmgroids.h" #include "utils/guc.h" -#include "utils/hashutils.h" #include "utils/inet.h" #include "utils/lsyscache.h" #include "utils/sortsupport.h" diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index c92ad5a4fe..bd00f23b94 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -27,6 +27,7 @@ #include <math.h> #include "catalog/pg_type.h" +#include "common/hashfn.h" #include "common/int.h" #include "funcapi.h" #include "lib/hyperloglog.h" @@ -38,7 +39,6 @@ #include "utils/builtins.h" #include "utils/float.h" #include "utils/guc.h" -#include "utils/hashutils.h" #include "utils/int8.h" #include "utils/numeric.h" #include "utils/sortsupport.h" diff --git a/src/backend/utils/adt/rangetypes.c b/src/backend/utils/adt/rangetypes.c index 639e1dad6c..b95132b714 100644 --- a/src/backend/utils/adt/rangetypes.c +++ b/src/backend/utils/adt/rangetypes.c @@ -31,12 +31,12 @@ #include "postgres.h" #include "access/tupmacs.h" +#include "common/hashfn.h" #include "lib/stringinfo.h" #include "libpq/pqformat.h" #include "miscadmin.h" #include "utils/builtins.h" #include "utils/date.h" -#include "utils/hashutils.h" #include "utils/int8.h" #include "utils/lsyscache.h" #include "utils/rangetypes.h" diff --git a/src/backend/utils/adt/tid.c b/src/backend/utils/adt/tid.c index fad2057754..4ce8375eab 100644 --- a/src/backend/utils/adt/tid.c +++ b/src/backend/utils/adt/tid.c @@ -25,12 +25,12 @@ #include "access/tableam.h" #include "catalog/namespace.h" #include "catalog/pg_type.h" +#include "common/hashfn.h" #include "libpq/pqformat.h" #include "miscadmin.h" #include "parser/parsetree.h" #include "utils/acl.h" #include "utils/builtins.h" -#include "utils/hashutils.h" #include "utils/rel.h" #include "utils/snapmgr.h" #include "utils/varlena.h" diff --git a/src/backend/utils/adt/uuid.c b/src/backend/utils/adt/uuid.c index 4abd861dd7..c906ee789d 100644 --- a/src/backend/utils/adt/uuid.c +++ b/src/backend/utils/adt/uuid.c @@ -13,12 +13,12 @@ #include "postgres.h" +#include "common/hashfn.h" #include "lib/hyperloglog.h" #include "libpq/pqformat.h" #include "port/pg_bswap.h" #include "utils/builtins.h" #include "utils/guc.h" -#include "utils/hashutils.h" #include "utils/sortsupport.h" #include "utils/uuid.h" diff --git a/src/backend/utils/adt/varchar.c b/src/backend/utils/adt/varchar.c index 1e1239a1ba..39acfdff6c 100644 --- a/src/backend/utils/adt/varchar.c +++ b/src/backend/utils/adt/varchar.c @@ -17,13 +17,13 @@ #include "access/detoast.h" #include "catalog/pg_collation.h" #include "catalog/pg_type.h" +#include "common/hashfn.h" #include "libpq/pqformat.h" #include "mb/pg_wchar.h" #include "nodes/nodeFuncs.h" #include "nodes/supportnodes.h" #include "utils/array.h" #include "utils/builtins.h" -#include "utils/hashutils.h" #include "utils/lsyscache.h" #include "utils/pg_locale.h" #include "utils/varlena.h" diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 875b02d643..18900e0f1e 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -20,6 +20,7 @@ #include "access/detoast.h" #include "catalog/pg_collation.h" #include "catalog/pg_type.h" +#include "common/hashfn.h" #include "common/int.h" #include "lib/hyperloglog.h" #include "libpq/pqformat.h" @@ -29,7 +30,6 @@ #include "regex/regex.h" #include "utils/builtins.h" #include "utils/bytea.h" -#include "utils/hashutils.h" #include "utils/lsyscache.h" #include "utils/memutils.h" #include "utils/pg_locale.h" diff --git a/src/backend/utils/cache/catcache.c b/src/backend/utils/cache/catcache.c index 64776e3209..3613ae5f44 100644 --- a/src/backend/utils/cache/catcache.c +++ b/src/backend/utils/cache/catcache.c @@ -24,6 +24,7 @@ #include "catalog/pg_collation.h" #include "catalog/pg_operator.h" #include "catalog/pg_type.h" +#include "common/hashfn.h" #include "miscadmin.h" #ifdef CATCACHE_STATS #include "storage/ipc.h" /* for on_proc_exit */ @@ -32,7 +33,6 @@ #include "utils/builtins.h" #include "utils/datum.h" #include "utils/fmgroids.h" -#include "utils/hashutils.h" #include "utils/inval.h" #include "utils/memutils.h" #include "utils/rel.h" diff --git a/src/backend/utils/hash/Makefile b/src/backend/utils/hash/Makefile index fc7b165f7f..d4c1210e36 100644 --- a/src/backend/utils/hash/Makefile +++ b/src/backend/utils/hash/Makefile @@ -14,7 +14,6 @@ include $(top_builddir)/src/Makefile.global OBJS = \ dynahash.o \ - hashfn.o \ pg_crc.o include $(top_srcdir)/src/backend/common.mk diff --git a/src/backend/utils/hash/dynahash.c b/src/backend/utils/hash/dynahash.c index d245e1aa12..b5381958e7 100644 --- a/src/backend/utils/hash/dynahash.c +++ b/src/backend/utils/hash/dynahash.c @@ -86,10 +86,10 @@ #include <limits.h> #include "access/xact.h" +#include "common/hashfn.h" #include "storage/shmem.h" #include "storage/spin.h" #include "utils/dynahash.h" -#include "utils/hashutils.h" #include "utils/memutils.h" diff --git a/src/backend/utils/hash/hashfn.c b/src/backend/utils/hash/hashfn.c deleted file mode 100644 index ecc52014af..0000000000 --- a/src/backend/utils/hash/hashfn.c +++ /dev/null @@ -1,692 +0,0 @@ -/*------------------------------------------------------------------------- - * - * hashfn.c - * Generic hashing functions, and hash functions for use in dynahash.c - * hashtables - * - * - * Portions Copyright (c) 1996-2020, PostgreSQL Global Development Group - * Portions Copyright (c) 1994, Regents of the University of California - * - * - * IDENTIFICATION - * src/backend/utils/hash/hashfn.c - * - * NOTES - * It is expected that every bit of a hash function's 32-bit result is - * as random as every other; failure to ensure this is likely to lead - * to poor performance of hash tables. In most cases a hash - * function should use hash_bytes() or its variant hash_bytes_uint32(), - * or the wrappers hash_any() and hash_uint32 defined in hashfn.h. - * - *------------------------------------------------------------------------- - */ -#include "postgres.h" - -#include "utils/hashutils.h" - - -/* - * This hash function was written by Bob Jenkins - * (bob_jenkins@burtleburtle.net), and superficially adapted - * for PostgreSQL by Neil Conway. For more information on this - * hash function, see http://burtleburtle.net/bob/hash/doobs.html, - * or Bob's article in Dr. Dobb's Journal, Sept. 1997. - * - * In the current code, we have adopted Bob's 2006 update of his hash - * function to fetch the data a word at a time when it is suitably aligned. - * This makes for a useful speedup, at the cost of having to maintain - * four code paths (aligned vs unaligned, and little-endian vs big-endian). - * It also uses two separate mixing functions mix() and final(), instead - * of a slower multi-purpose function. - */ - -/* Get a bit mask of the bits set in non-uint32 aligned addresses */ -#define UINT32_ALIGN_MASK (sizeof(uint32) - 1) - -/* Rotate a uint32 value left by k bits - note multiple evaluation! */ -#define rot(x,k) (((x)<<(k)) | ((x)>>(32-(k)))) - -/*---------- - * mix -- mix 3 32-bit values reversibly. - * - * This is reversible, so any information in (a,b,c) before mix() is - * still in (a,b,c) after mix(). - * - * If four pairs of (a,b,c) inputs are run through mix(), or through - * mix() in reverse, there are at least 32 bits of the output that - * are sometimes the same for one pair and different for another pair. - * This was tested for: - * * pairs that differed by one bit, by two bits, in any combination - * of top bits of (a,b,c), or in any combination of bottom bits of - * (a,b,c). - * * "differ" is defined as +, -, ^, or ~^. For + and -, I transformed - * the output delta to a Gray code (a^(a>>1)) so a string of 1's (as - * is commonly produced by subtraction) look like a single 1-bit - * difference. - * * the base values were pseudorandom, all zero but one bit set, or - * all zero plus a counter that starts at zero. - * - * This does not achieve avalanche. There are input bits of (a,b,c) - * that fail to affect some output bits of (a,b,c), especially of a. The - * most thoroughly mixed value is c, but it doesn't really even achieve - * avalanche in c. - * - * This allows some parallelism. Read-after-writes are good at doubling - * the number of bits affected, so the goal of mixing pulls in the opposite - * direction from the goal of parallelism. I did what I could. Rotates - * seem to cost as much as shifts on every machine I could lay my hands on, - * and rotates are much kinder to the top and bottom bits, so I used rotates. - *---------- - */ -#define mix(a,b,c) \ -{ \ - a -= c; a ^= rot(c, 4); c += b; \ - b -= a; b ^= rot(a, 6); a += c; \ - c -= b; c ^= rot(b, 8); b += a; \ - a -= c; a ^= rot(c,16); c += b; \ - b -= a; b ^= rot(a,19); a += c; \ - c -= b; c ^= rot(b, 4); b += a; \ -} - -/*---------- - * final -- final mixing of 3 32-bit values (a,b,c) into c - * - * Pairs of (a,b,c) values differing in only a few bits will usually - * produce values of c that look totally different. This was tested for - * * pairs that differed by one bit, by two bits, in any combination - * of top bits of (a,b,c), or in any combination of bottom bits of - * (a,b,c). - * * "differ" is defined as +, -, ^, or ~^. For + and -, I transformed - * the output delta to a Gray code (a^(a>>1)) so a string of 1's (as - * is commonly produced by subtraction) look like a single 1-bit - * difference. - * * the base values were pseudorandom, all zero but one bit set, or - * all zero plus a counter that starts at zero. - * - * The use of separate functions for mix() and final() allow for a - * substantial performance increase since final() does not need to - * do well in reverse, but is does need to affect all output bits. - * mix(), on the other hand, does not need to affect all output - * bits (affecting 32 bits is enough). The original hash function had - * a single mixing operation that had to satisfy both sets of requirements - * and was slower as a result. - *---------- - */ -#define final(a,b,c) \ -{ \ - c ^= b; c -= rot(b,14); \ - a ^= c; a -= rot(c,11); \ - b ^= a; b -= rot(a,25); \ - c ^= b; c -= rot(b,16); \ - a ^= c; a -= rot(c, 4); \ - b ^= a; b -= rot(a,14); \ - c ^= b; c -= rot(b,24); \ -} - -/* - * hash_bytes() -- hash a variable-length key into a 32-bit value - * k : the key (the unaligned variable-length array of bytes) - * len : the length of the key, counting by bytes - * - * Returns a uint32 value. Every bit of the key affects every bit of - * the return value. Every 1-bit and 2-bit delta achieves avalanche. - * About 6*len+35 instructions. The best hash table sizes are powers - * of 2. There is no need to do mod a prime (mod is sooo slow!). - * If you need less than 32 bits, use a bitmask. - * - * This procedure must never throw elog(ERROR); the ResourceOwner code - * relies on this not to fail. - * - * Note: we could easily change this function to return a 64-bit hash value - * by using the final values of both b and c. b is perhaps a little less - * well mixed than c, however. - */ -uint32 -hash_bytes(const unsigned char *k, int keylen) -{ - uint32 a, - b, - c, - len; - - /* Set up the internal state */ - len = keylen; - a = b = c = 0x9e3779b9 + len + 3923095; - - /* If the source pointer is word-aligned, we use word-wide fetches */ - if (((uintptr_t) k & UINT32_ALIGN_MASK) == 0) - { - /* Code path for aligned source data */ - const uint32 *ka = (const uint32 *) k; - - /* handle most of the key */ - while (len >= 12) - { - a += ka[0]; - b += ka[1]; - c += ka[2]; - mix(a, b, c); - ka += 3; - len -= 12; - } - - /* handle the last 11 bytes */ - k = (const unsigned char *) ka; -#ifdef WORDS_BIGENDIAN - switch (len) - { - case 11: - c += ((uint32) k[10] << 8); - /* fall through */ - case 10: - c += ((uint32) k[9] << 16); - /* fall through */ - case 9: - c += ((uint32) k[8] << 24); - /* fall through */ - case 8: - /* the lowest byte of c is reserved for the length */ - b += ka[1]; - a += ka[0]; - break; - case 7: - b += ((uint32) k[6] << 8); - /* fall through */ - case 6: - b += ((uint32) k[5] << 16); - /* fall through */ - case 5: - b += ((uint32) k[4] << 24); - /* fall through */ - case 4: - a += ka[0]; - break; - case 3: - a += ((uint32) k[2] << 8); - /* fall through */ - case 2: - a += ((uint32) k[1] << 16); - /* fall through */ - case 1: - a += ((uint32) k[0] << 24); - /* case 0: nothing left to add */ - } -#else /* !WORDS_BIGENDIAN */ - switch (len) - { - case 11: - c += ((uint32) k[10] << 24); - /* fall through */ - case 10: - c += ((uint32) k[9] << 16); - /* fall through */ - case 9: - c += ((uint32) k[8] << 8); - /* fall through */ - case 8: - /* the lowest byte of c is reserved for the length */ - b += ka[1]; - a += ka[0]; - break; - case 7: - b += ((uint32) k[6] << 16); - /* fall through */ - case 6: - b += ((uint32) k[5] << 8); - /* fall through */ - case 5: - b += k[4]; - /* fall through */ - case 4: - a += ka[0]; - break; - case 3: - a += ((uint32) k[2] << 16); - /* fall through */ - case 2: - a += ((uint32) k[1] << 8); - /* fall through */ - case 1: - a += k[0]; - /* case 0: nothing left to add */ - } -#endif /* WORDS_BIGENDIAN */ - } - else - { - /* Code path for non-aligned source data */ - - /* handle most of the key */ - while (len >= 12) - { -#ifdef WORDS_BIGENDIAN - a += (k[3] + ((uint32) k[2] << 8) + ((uint32) k[1] << 16) + ((uint32) k[0] << 24)); - b += (k[7] + ((uint32) k[6] << 8) + ((uint32) k[5] << 16) + ((uint32) k[4] << 24)); - c += (k[11] + ((uint32) k[10] << 8) + ((uint32) k[9] << 16) + ((uint32) k[8] << 24)); -#else /* !WORDS_BIGENDIAN */ - a += (k[0] + ((uint32) k[1] << 8) + ((uint32) k[2] << 16) + ((uint32) k[3] << 24)); - b += (k[4] + ((uint32) k[5] << 8) + ((uint32) k[6] << 16) + ((uint32) k[7] << 24)); - c += (k[8] + ((uint32) k[9] << 8) + ((uint32) k[10] << 16) + ((uint32) k[11] << 24)); -#endif /* WORDS_BIGENDIAN */ - mix(a, b, c); - k += 12; - len -= 12; - } - - /* handle the last 11 bytes */ -#ifdef WORDS_BIGENDIAN - switch (len) - { - case 11: - c += ((uint32) k[10] << 8); - /* fall through */ - case 10: - c += ((uint32) k[9] << 16); - /* fall through */ - case 9: - c += ((uint32) k[8] << 24); - /* fall through */ - case 8: - /* the lowest byte of c is reserved for the length */ - b += k[7]; - /* fall through */ - case 7: - b += ((uint32) k[6] << 8); - /* fall through */ - case 6: - b += ((uint32) k[5] << 16); - /* fall through */ - case 5: - b += ((uint32) k[4] << 24); - /* fall through */ - case 4: - a += k[3]; - /* fall through */ - case 3: - a += ((uint32) k[2] << 8); - /* fall through */ - case 2: - a += ((uint32) k[1] << 16); - /* fall through */ - case 1: - a += ((uint32) k[0] << 24); - /* case 0: nothing left to add */ - } -#else /* !WORDS_BIGENDIAN */ - switch (len) - { - case 11: - c += ((uint32) k[10] << 24); - /* fall through */ - case 10: - c += ((uint32) k[9] << 16); - /* fall through */ - case 9: - c += ((uint32) k[8] << 8); - /* fall through */ - case 8: - /* the lowest byte of c is reserved for the length */ - b += ((uint32) k[7] << 24); - /* fall through */ - case 7: - b += ((uint32) k[6] << 16); - /* fall through */ - case 6: - b += ((uint32) k[5] << 8); - /* fall through */ - case 5: - b += k[4]; - /* fall through */ - case 4: - a += ((uint32) k[3] << 24); - /* fall through */ - case 3: - a += ((uint32) k[2] << 16); - /* fall through */ - case 2: - a += ((uint32) k[1] << 8); - /* fall through */ - case 1: - a += k[0]; - /* case 0: nothing left to add */ - } -#endif /* WORDS_BIGENDIAN */ - } - - final(a, b, c); - - /* report the result */ - return c; -} - -/* - * hash_bytes_extended() -- hash into a 64-bit value, using an optional seed - * k : the key (the unaligned variable-length array of bytes) - * len : the length of the key, counting by bytes - * seed : a 64-bit seed (0 means no seed) - * - * Returns a uint64 value. Otherwise similar to hash_bytes. - */ -uint64 -hash_bytes_extended(const unsigned char *k, int keylen, uint64 seed) -{ - uint32 a, - b, - c, - len; - - /* Set up the internal state */ - len = keylen; - a = b = c = 0x9e3779b9 + len + 3923095; - - /* If the seed is non-zero, use it to perturb the internal state. */ - if (seed != 0) - { - /* - * In essence, the seed is treated as part of the data being hashed, - * but for simplicity, we pretend that it's padded with four bytes of - * zeroes so that the seed constitutes a 12-byte chunk. - */ - a += (uint32) (seed >> 32); - b += (uint32) seed; - mix(a, b, c); - } - - /* If the source pointer is word-aligned, we use word-wide fetches */ - if (((uintptr_t) k & UINT32_ALIGN_MASK) == 0) - { - /* Code path for aligned source data */ - const uint32 *ka = (const uint32 *) k; - - /* handle most of the key */ - while (len >= 12) - { - a += ka[0]; - b += ka[1]; - c += ka[2]; - mix(a, b, c); - ka += 3; - len -= 12; - } - - /* handle the last 11 bytes */ - k = (const unsigned char *) ka; -#ifdef WORDS_BIGENDIAN - switch (len) - { - case 11: - c += ((uint32) k[10] << 8); - /* fall through */ - case 10: - c += ((uint32) k[9] << 16); - /* fall through */ - case 9: - c += ((uint32) k[8] << 24); - /* fall through */ - case 8: - /* the lowest byte of c is reserved for the length */ - b += ka[1]; - a += ka[0]; - break; - case 7: - b += ((uint32) k[6] << 8); - /* fall through */ - case 6: - b += ((uint32) k[5] << 16); - /* fall through */ - case 5: - b += ((uint32) k[4] << 24); - /* fall through */ - case 4: - a += ka[0]; - break; - case 3: - a += ((uint32) k[2] << 8); - /* fall through */ - case 2: - a += ((uint32) k[1] << 16); - /* fall through */ - case 1: - a += ((uint32) k[0] << 24); - /* case 0: nothing left to add */ - } -#else /* !WORDS_BIGENDIAN */ - switch (len) - { - case 11: - c += ((uint32) k[10] << 24); - /* fall through */ - case 10: - c += ((uint32) k[9] << 16); - /* fall through */ - case 9: - c += ((uint32) k[8] << 8); - /* fall through */ - case 8: - /* the lowest byte of c is reserved for the length */ - b += ka[1]; - a += ka[0]; - break; - case 7: - b += ((uint32) k[6] << 16); - /* fall through */ - case 6: - b += ((uint32) k[5] << 8); - /* fall through */ - case 5: - b += k[4]; - /* fall through */ - case 4: - a += ka[0]; - break; - case 3: - a += ((uint32) k[2] << 16); - /* fall through */ - case 2: - a += ((uint32) k[1] << 8); - /* fall through */ - case 1: - a += k[0]; - /* case 0: nothing left to add */ - } -#endif /* WORDS_BIGENDIAN */ - } - else - { - /* Code path for non-aligned source data */ - - /* handle most of the key */ - while (len >= 12) - { -#ifdef WORDS_BIGENDIAN - a += (k[3] + ((uint32) k[2] << 8) + ((uint32) k[1] << 16) + ((uint32) k[0] << 24)); - b += (k[7] + ((uint32) k[6] << 8) + ((uint32) k[5] << 16) + ((uint32) k[4] << 24)); - c += (k[11] + ((uint32) k[10] << 8) + ((uint32) k[9] << 16) + ((uint32) k[8] << 24)); -#else /* !WORDS_BIGENDIAN */ - a += (k[0] + ((uint32) k[1] << 8) + ((uint32) k[2] << 16) + ((uint32) k[3] << 24)); - b += (k[4] + ((uint32) k[5] << 8) + ((uint32) k[6] << 16) + ((uint32) k[7] << 24)); - c += (k[8] + ((uint32) k[9] << 8) + ((uint32) k[10] << 16) + ((uint32) k[11] << 24)); -#endif /* WORDS_BIGENDIAN */ - mix(a, b, c); - k += 12; - len -= 12; - } - - /* handle the last 11 bytes */ -#ifdef WORDS_BIGENDIAN - switch (len) - { - case 11: - c += ((uint32) k[10] << 8); - /* fall through */ - case 10: - c += ((uint32) k[9] << 16); - /* fall through */ - case 9: - c += ((uint32) k[8] << 24); - /* fall through */ - case 8: - /* the lowest byte of c is reserved for the length */ - b += k[7]; - /* fall through */ - case 7: - b += ((uint32) k[6] << 8); - /* fall through */ - case 6: - b += ((uint32) k[5] << 16); - /* fall through */ - case 5: - b += ((uint32) k[4] << 24); - /* fall through */ - case 4: - a += k[3]; - /* fall through */ - case 3: - a += ((uint32) k[2] << 8); - /* fall through */ - case 2: - a += ((uint32) k[1] << 16); - /* fall through */ - case 1: - a += ((uint32) k[0] << 24); - /* case 0: nothing left to add */ - } -#else /* !WORDS_BIGENDIAN */ - switch (len) - { - case 11: - c += ((uint32) k[10] << 24); - /* fall through */ - case 10: - c += ((uint32) k[9] << 16); - /* fall through */ - case 9: - c += ((uint32) k[8] << 8); - /* fall through */ - case 8: - /* the lowest byte of c is reserved for the length */ - b += ((uint32) k[7] << 24); - /* fall through */ - case 7: - b += ((uint32) k[6] << 16); - /* fall through */ - case 6: - b += ((uint32) k[5] << 8); - /* fall through */ - case 5: - b += k[4]; - /* fall through */ - case 4: - a += ((uint32) k[3] << 24); - /* fall through */ - case 3: - a += ((uint32) k[2] << 16); - /* fall through */ - case 2: - a += ((uint32) k[1] << 8); - /* fall through */ - case 1: - a += k[0]; - /* case 0: nothing left to add */ - } -#endif /* WORDS_BIGENDIAN */ - } - - final(a, b, c); - - /* report the result */ - return ((uint64) b << 32) | c; -} - -/* - * hash_bytes_uint32() -- hash a 32-bit value to a 32-bit value - * - * This has the same result as - * hash_bytes(&k, sizeof(uint32)) - * but is faster and doesn't force the caller to store k into memory. - */ -uint32 -hash_bytes_uint32(uint32 k) -{ - uint32 a, - b, - c; - - a = b = c = 0x9e3779b9 + (uint32) sizeof(uint32) + 3923095; - a += k; - - final(a, b, c); - - /* report the result */ - return c; -} - -/* - * hash_bytes_uint32_extended() -- hash 32-bit value to 64-bit value, with seed - * - * Like hash_bytes_uint32, this is a convenience function. - */ -uint64 -hash_bytes_uint32_extended(uint32 k, uint64 seed) -{ - uint32 a, - b, - c; - - a = b = c = 0x9e3779b9 + (uint32) sizeof(uint32) + 3923095; - - if (seed != 0) - { - a += (uint32) (seed >> 32); - b += (uint32) seed; - mix(a, b, c); - } - - a += k; - - final(a, b, c); - - /* report the result */ - return ((uint64) b << 32) | c; -} - -/* - * string_hash: hash function for keys that are NUL-terminated strings. - * - * NOTE: this is the default hash function if none is specified. - */ -uint32 -string_hash(const void *key, Size keysize) -{ - /* - * If the string exceeds keysize-1 bytes, we want to hash only that many, - * because when it is copied into the hash table it will be truncated at - * that length. - */ - Size s_len = strlen((const char *) key); - - s_len = Min(s_len, keysize - 1); - return hash_bytes((const unsigned char *) key, (int) s_len); -} - -/* - * tag_hash: hash function for fixed-size tag values - */ -uint32 -tag_hash(const void *key, Size keysize) -{ - return hash_bytes((const unsigned char *) key, (int) keysize); -} - -/* - * uint32_hash: hash function for keys that are uint32 or int32 - * - * (tag_hash works for this case too, but is slower) - */ -uint32 -uint32_hash(const void *key, Size keysize) -{ - Assert(keysize == sizeof(uint32)); - return hash_bytes_uint32(*((const uint32 *) key)); -} diff --git a/src/backend/utils/resowner/resowner.c b/src/backend/utils/resowner/resowner.c index ac6f226f59..3c39e48825 100644 --- a/src/backend/utils/resowner/resowner.c +++ b/src/backend/utils/resowner/resowner.c @@ -20,12 +20,12 @@ */ #include "postgres.h" +#include "common/hashfn.h" #include "jit/jit.h" #include "storage/bufmgr.h" #include "storage/ipc.h" #include "storage/predicate.h" #include "storage/proc.h" -#include "utils/hashutils.h" #include "utils/memutils.h" #include "utils/rel.h" #include "utils/resowner_private.h" |
