Skip to content

Commit a5ebbf6

Browse files
committed
Remove unneeded dummy test from the set search loop (when the hashes match we know the key is not a dummy).
1 parent 3037e84 commit a5ebbf6

File tree

2 files changed

+15
-5
lines changed

2 files changed

+15
-5
lines changed

Include/setobject.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,10 @@ extern "C" {
1414
2. Active: key != NULL and key != dummy
1515
3. Dummy: key == dummy
1616
17-
The hash field of Unused or Dummy slots have no meaning.
17+
The hash field of Unused slots have no meaning.
18+
The hash field of Dummny slots are set to -1
19+
meaning that dummy entries can be detected by
20+
either entry->key==dummy or by entry->hash==-1.
1821
*/
1922

2023
#define PySet_MINSIZE 8

Objects/setobject.c

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,10 @@ set_lookkey(PySetObject *so, PyObject *key, Py_hash_t hash)
6565
return entry;
6666

6767
while (1) {
68-
if (entry->hash == hash && entry->key != dummy) { /* dummy match unlikely */
68+
if (entry->hash == hash) {
6969
PyObject *startkey = entry->key;
70+
/* startkey cannot be a dummy because the dummy hash field is -1 */
71+
assert(startkey != dummy);
7072
if (startkey == key)
7173
return entry;
7274
if (PyUnicode_CheckExact(startkey)
@@ -83,15 +85,18 @@ set_lookkey(PySetObject *so, PyObject *key, Py_hash_t hash)
8385
if (cmp > 0) /* likely */
8486
return entry;
8587
}
86-
if (entry->key == dummy && freeslot == NULL)
88+
if (entry->hash == -1 && freeslot == NULL) {
89+
assert(entry->key == dummy);
8790
freeslot = entry;
91+
}
8892

8993
for (j = 1 ; j <= LINEAR_PROBES ; j++) {
9094
entry = &table[(i + j) & mask];
9195
if (entry->key == NULL)
9296
goto found_null;
93-
if (entry->hash == hash && entry->key != dummy) {
97+
if (entry->hash == hash) {
9498
PyObject *startkey = entry->key;
99+
assert(startkey != dummy);
95100
if (startkey == key)
96101
return entry;
97102
if (PyUnicode_CheckExact(startkey)
@@ -108,8 +113,10 @@ set_lookkey(PySetObject *so, PyObject *key, Py_hash_t hash)
108113
if (cmp > 0)
109114
return entry;
110115
}
111-
if (entry->key == dummy && freeslot == NULL)
116+
if (entry->hash == -1 && freeslot == NULL) {
117+
assert(entry->key == dummy);
112118
freeslot = entry;
119+
}
113120
}
114121

115122
perturb >>= PERTURB_SHIFT;

0 commit comments

Comments
 (0)