Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit efb4609

Browse files
committed
Small lookmapping nits:
- remove bogus initialization using uninitialized i - derive initial incr from hash - copy mp->ma_table into a local variable
1 parent 5ed19dc commit efb4609

2 files changed

Lines changed: 12 additions & 10 deletions

File tree

Objects/dictobject.c

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -169,14 +169,15 @@ lookmapping(mp, key, hash)
169169
register unsigned long sum = (unsigned long) hash;
170170
register mappingentry *freeslot = NULL;
171171
register int mask = mp->ma_size-1;
172-
register mappingentry *ep = &mp->ma_table[i];
172+
mappingentry *ep0 = mp->ma_table;
173+
register mappingentry *ep;
173174
/* We must come up with (i, incr) such that 0 <= i < ma_size
174175
and 0 < incr < ma_size and both are a function of hash */
175176
i = (~sum) & mask;
176177
/* We use ~sum instead if sum, as degenerate hash functions, such
177178
as for ints <sigh>, can have lots of leading zeros. It's not
178179
really a performance risk, but better safe than sorry. */
179-
ep = &mp->ma_table[i];
180+
ep = &ep0[i];
180181
if (ep->me_key == NULL)
181182
return ep;
182183
if (ep->me_key == dummy)
@@ -185,16 +186,16 @@ lookmapping(mp, key, hash)
185186
(ep->me_hash == hash && cmpobject(ep->me_key, key) == 0)) {
186187
return ep;
187188
}
188-
/* Derive incr from i, just to make it more arbitrary. Note that
189+
/* Derive incr from sum, just to make it more arbitrary. Note that
189190
incr must not be 0, or we will get into an infinite loop.*/
190-
incr = i << 1;
191+
incr = (sum ^ (sum >> 3)) & mask;
191192
if (!incr)
192193
incr = mask;
193194
if (incr > mask) /* Cycle through GF(2^n)-{0} */
194195
incr ^= mp->ma_poly; /* This will implicitly clear the
195196
highest bit */
196197
for (;;) {
197-
ep = &mp->ma_table[(i+incr)&mask];
198+
ep = &ep0[(i+incr)&mask];
198199
if (ep->me_key == NULL) {
199200
if (freeslot != NULL)
200201
return freeslot;

Objects/mappingobject.c

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -169,14 +169,15 @@ lookmapping(mp, key, hash)
169169
register unsigned long sum = (unsigned long) hash;
170170
register mappingentry *freeslot = NULL;
171171
register int mask = mp->ma_size-1;
172-
register mappingentry *ep = &mp->ma_table[i];
172+
mappingentry *ep0 = mp->ma_table;
173+
register mappingentry *ep;
173174
/* We must come up with (i, incr) such that 0 <= i < ma_size
174175
and 0 < incr < ma_size and both are a function of hash */
175176
i = (~sum) & mask;
176177
/* We use ~sum instead if sum, as degenerate hash functions, such
177178
as for ints <sigh>, can have lots of leading zeros. It's not
178179
really a performance risk, but better safe than sorry. */
179-
ep = &mp->ma_table[i];
180+
ep = &ep0[i];
180181
if (ep->me_key == NULL)
181182
return ep;
182183
if (ep->me_key == dummy)
@@ -185,16 +186,16 @@ lookmapping(mp, key, hash)
185186
(ep->me_hash == hash && cmpobject(ep->me_key, key) == 0)) {
186187
return ep;
187188
}
188-
/* Derive incr from i, just to make it more arbitrary. Note that
189+
/* Derive incr from sum, just to make it more arbitrary. Note that
189190
incr must not be 0, or we will get into an infinite loop.*/
190-
incr = i << 1;
191+
incr = (sum ^ (sum >> 3)) & mask;
191192
if (!incr)
192193
incr = mask;
193194
if (incr > mask) /* Cycle through GF(2^n)-{0} */
194195
incr ^= mp->ma_poly; /* This will implicitly clear the
195196
highest bit */
196197
for (;;) {
197-
ep = &mp->ma_table[(i+incr)&mask];
198+
ep = &ep0[(i+incr)&mask];
198199
if (ep->me_key == NULL) {
199200
if (freeslot != NULL)
200201
return freeslot;

0 commit comments

Comments
 (0)