8000 py/qstr: Optimize hash sort. · micropython/micropython@3bc257c · GitHub
[go: up one dir, main page]

Skip to content

Commit 3bc257c

Browse files
committed
py/qstr: Optimize hash sort.
In case cmp == 0 there could be a hash collision. To save program size Instead of checking for hash collisions, simply revert to linear search which also compares strings.
1 parent 660c810 commit 3bc257c

File tree

1 file changed

+8
-16
lines changed

1 file changed

+8
-16
lines changed

py/qstr.c

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -243,24 +243,16 @@ qstr qstr_find_strn(const char *str, size_t str_len) {
243243
while (high - low > MP_QSTR_SEARCH_THRESHOLD) {
244244
size_t mid = (low + high + 1) / 2;
245245
int cmp = pool->hashes[mid] - str_hash;
246-
if (cmp == 0) cmp = pool->lengths[mid] - str_len;
247-
if (cmp < 0) {
248-
low = mid;
249-
} else if (cmp > 0) {
246+
if (cmp == 0) {
247+
cmp = pool->lengths[mid] - str_len;
248+
}
249+
if (cmp > 0) {
250250
high = mid;
251251
} else {
252-
// avoid a rare (hash,len) collisions
253-
while (MP_UNLIKELY(
254-
pool->lengths[mid] != str_len ||
255-
memcmp(pool->qstrs[mid], str, str_len) != 0)) {
256-
mid++;
257-
if (mid > high ||
258-
pool->hashes[mid] != str_hash ||
259-
pool->lengths[mid] != str_len) {
260-
return 0;
261-
}
262-
}
263-
return pool->total_prev_len + mid;
252+
low = mid;
253+
if (cmp == 0) {
254+
break;
255+
}
264256
}
265257
}
266258
}

0 commit comments

Comments
 (0)
0