Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitcc5ef90

Browse files
committed
Refactor initial hash lookup in dynahash.c
The same pattern is used three times in dynahash.c to retrieve a bucketnumber and a hash bucket from a hash value. This has popped up whilediscussing improvements for the type cache, where this piece ofrefactoring would become useful.Note that hash_search_with_hash_value() does not need the bucket number,just the hash bucket.Author: Teodor SigaevReviewed-by: Aleksander Alekseev, Michael PaquierDiscussion:https://postgr.es/m/5812a6e5-68ae-4d84-9d85-b443176966a1@sigaev.ru
1 parent4169850 commitcc5ef90

File tree

1 file changed

+33
-42
lines changed

1 file changed

+33
-42
lines changed

‎src/backend/utils/hash/dynahash.c

Lines changed: 33 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,8 @@ static void hdefault(HTAB *hashp);
273273
staticintchoose_nelem_alloc(Sizeentrysize);
274274
staticboolinit_htab(HTAB*hashp,longnelem);
275275
staticvoidhash_corrupted(HTAB*hashp);
276+
staticuint32hash_initial_lookup(HTAB*hashp,uint32hashvalue,
277+
HASHBUCKET**bucketptr);
276278
staticlongnext_pow2_long(longnum);
277279
staticintnext_pow2_int(longnum);
278280
staticvoidregister_seq_scan(HTAB*hashp);
@@ -972,10 +974,6 @@ hash_search_with_hash_value(HTAB *hashp,
972974
HASHHDR*hctl=hashp->hctl;
973975
intfreelist_idx=FREELIST_IDX(hctl,hashvalue);
974976
Sizekeysize;
975-
uint32bucket;
976-
longsegment_num;
977-
longsegment_ndx;
978-
HASHSEGMENTsegp;
979977
HASHBUCKETcurrBucket;
980978
HASHBUCKET*prevBucketPtr;
981979
HashCompareFuncmatch;
@@ -1008,17 +1006,7 @@ hash_search_with_hash_value(HTAB *hashp,
10081006
/*
10091007
* Do the initial lookup
10101008
*/
1011-
bucket=calc_bucket(hctl,hashvalue);
1012-
1013-
segment_num=bucket >>hashp->sshift;
1014-
segment_ndx=MOD(bucket,hashp->ssize);
1015-
1016-
segp=hashp->dir[segment_num];
1017-
1018-
if (segp==NULL)
1019-
hash_corrupted(hashp);
1020-
1021-
prevBucketPtr=&segp[segment_ndx];
1009+
(void)hash_initial_lookup(hashp,hashvalue,&prevBucketPtr);
10221010
currBucket=*prevBucketPtr;
10231011

10241012
/*
@@ -1159,14 +1147,10 @@ hash_update_hash_key(HTAB *hashp,
11591147
constvoid*newKeyPtr)
11601148
{
11611149
HASHELEMENT*existingElement=ELEMENT_FROM_KEY(existingEntry);
1162-
HASHHDR*hctl=hashp->hctl;
11631150
uint32newhashvalue;
11641151
Sizekeysize;
11651152
uint32bucket;
11661153
uint32newbucket;
1167-
longsegment_num;
1168-
longsegment_ndx;
1169-
HASHSEGMENTsegp;
11701154
HASHBUCKETcurrBucket;
11711155
HASHBUCKET*prevBucketPtr;
11721156
HASHBUCKET*oldPrevPtr;
@@ -1187,17 +1171,8 @@ hash_update_hash_key(HTAB *hashp,
11871171
* this to be able to unlink it from its hash chain, but as a side benefit
11881172
* we can verify the validity of the passed existingEntry pointer.
11891173
*/
1190-
bucket=calc_bucket(hctl,existingElement->hashvalue);
1191-
1192-
segment_num=bucket >>hashp->sshift;
1193-
segment_ndx=MOD(bucket,hashp->ssize);
1194-
1195-
segp=hashp->dir[segment_num];
1196-
1197-
if (segp==NULL)
1198-
hash_corrupted(hashp);
1199-
1200-
prevBucketPtr=&segp[segment_ndx];
1174+
bucket=hash_initial_lookup(hashp,existingElement->hashvalue,
1175+
&prevBucketPtr);
12011176
currBucket=*prevBucketPtr;
12021177

12031178
while (currBucket!=NULL)
@@ -1219,18 +1194,7 @@ hash_update_hash_key(HTAB *hashp,
12191194
* chain we want to put the entry into.
12201195
*/
12211196
newhashvalue=hashp->hash(newKeyPtr,hashp->keysize);
1222-
1223-
newbucket=calc_bucket(hctl,newhashvalue);
1224-
1225-
segment_num=newbucket >>hashp->sshift;
1226-
segment_ndx=MOD(newbucket,hashp->ssize);
1227-
1228-
segp=hashp->dir[segment_num];
1229-
1230-
if (segp==NULL)
1231-
hash_corrupted(hashp);
1232-
1233-
prevBucketPtr=&segp[segment_ndx];
1197+
newbucket=hash_initial_lookup(hashp,newhashvalue,&prevBucketPtr);
12341198
currBucket=*prevBucketPtr;
12351199

12361200
/*
@@ -1741,6 +1705,33 @@ element_alloc(HTAB *hashp, int nelem, int freelist_idx)
17411705
return true;
17421706
}
17431707

1708+
/*
1709+
* Do initial lookup of a bucket for the given hash value, retrieving its
1710+
* bucket number and its hash bucket.
1711+
*/
1712+
staticinlineuint32
1713+
hash_initial_lookup(HTAB*hashp,uint32hashvalue,HASHBUCKET**bucketptr)
1714+
{
1715+
HASHHDR*hctl=hashp->hctl;
1716+
HASHSEGMENTsegp;
1717+
longsegment_num;
1718+
longsegment_ndx;
1719+
uint32bucket;
1720+
1721+
bucket=calc_bucket(hctl,hashvalue);
1722+
1723+
segment_num=bucket >>hashp->sshift;
1724+
segment_ndx=MOD(bucket,hashp->ssize);
1725+
1726+
segp=hashp->dir[segment_num];
1727+
1728+
if (segp==NULL)
1729+
hash_corrupted(hashp);
1730+
1731+
*bucketptr=&segp[segment_ndx];
1732+
returnbucket;
1733+
}
1734+
17441735
/* complain when we have detected a corrupted hashtable */
17451736
staticvoid
17461737
hash_corrupted(HTAB*hashp)

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp