py/qstr: Add support for MICROPY_QSTR_BYTES_IN_HASH=0.

This disables using qstr hashes altogether, which saves RAM and flash
(two bytes per interned string on a typical build) as well as code size.
On PYBV11 this is worth over 3k flash.

qstr comparison will now be done just by length then data. This affects
qstr_find_strn although this has a negligible performance impact as, for a
given comparison, the length and first character will ~usually be
different anyway.

String hashing (e.g. builtin `hash()` and map.c) now need to compute the
hash dynamically, and for the map case this does come at a performance
cost.

This work was funded through GitHub Sponsors.

Signed-off-by: Jim Mussared <jim.mussared@gmail.com>
This commit is contained in:
Jim Mussared
2023-02-03 16:42:03 +11:00
parent 307ecc5707
commit 7ea503929a
4 changed files with 66 additions and 23 deletions

View File

@@ -295,7 +295,8 @@ def compute_hash(qstr, bytes_hash):
for b in qstr:
hash = (hash * 33) ^ b
# Make sure that valid hash is never zero, zero means "hash not computed"
return (hash & ((1 << (8 * bytes_hash)) - 1)) or 1
# if bytes_hash is zero, assume a 16-bit mask (to match qstr.c)
return (hash & ((1 << (8 * (bytes_hash or 2))) - 1)) or 1
def qstr_escape(qst):