From 82b3500724206f2baa342a3559bbe716e9819426 Mon Sep 17 00:00:00 2001 From: Damien George Date: Thu, 11 Aug 2022 16:34:02 +1000 Subject: [PATCH] py/qstr: Change qstr hash type from mp_uint_t to size_t. The hash is either 8 or 16 bits (depending on MICROPY_QSTR_BYTES_IN_HASH) so will fit in a size_t. This saves 268 bytes on the unix nanbox build. Non-nanbox configurations are unchanged because mp_uint_t is the same size as size_t. Signed-off-by: Damien George --- py/objstr.h | 4 ++-- py/qstr.c | 8 ++++---- py/qstr.h | 2 +- 3 files changed, 7 insertions(+), 7 deletions(-) diff --git a/py/objstr.h b/py/objstr.h index 8f87c3018..92b065d86 100644 --- a/py/objstr.h +++ b/py/objstr.h @@ -30,7 +30,7 @@ typedef struct _mp_obj_str_t { mp_obj_base_t base; - mp_uint_t hash; + size_t hash; // len == number of bytes used in data, alloc = len + 1 because (at the moment) we also append a null byte size_t len; const byte *data; @@ -41,7 +41,7 @@ typedef struct _mp_obj_str_t { // use this macro to extract the string hash // warning: the hash can be 0, meaning invalid, and must then be explicitly computed from the data #define GET_STR_HASH(str_obj_in, str_hash) \ - mp_uint_t str_hash; \ + size_t str_hash; \ if (mp_obj_is_qstr(str_obj_in)) { \ str_hash = qstr_hash(MP_OBJ_QSTR_VALUE(str_obj_in)); \ } else { \ diff --git a/py/qstr.c b/py/qstr.c index f9ca10683..ea700566f 100644 --- a/py/qstr.c +++ b/py/qstr.c @@ -60,9 +60,9 @@ #define MICROPY_ALLOC_QSTR_ENTRIES_INIT (10) // this must match the equivalent function in makeqstrdata.py -mp_uint_t qstr_compute_hash(const byte *data, size_t len) { +size_t qstr_compute_hash(const byte *data, size_t len) { // djb2 algorithm; see http://www.cse.yorku.ca/~oz/hash.html - mp_uint_t hash = 5381; + size_t hash = 5381; for (const byte *top = data + len; data < top; data++) { hash = ((hash << 5) + hash) ^ (*data); // hash * 33 ^ data } @@ -181,7 +181,7 @@ STATIC qstr qstr_add(mp_uint_t hash, mp_uint_t len, const char *q_ptr) { qstr qstr_find_strn(const char *str, size_t str_len) { // work out hash of str - mp_uint_t str_hash = qstr_compute_hash((const byte *)str, str_len); + size_t str_hash = qstr_compute_hash((const byte *)str, str_len); // search pools for the data for (const qstr_pool_t *pool = MP_STATE_VM(last_pool); pool != NULL; pool = pool->prev) { @@ -254,7 +254,7 @@ qstr qstr_from_strn(const char *str, size_t len) { MP_STATE_VM(qstr_last_used) += n_bytes; // store the interned strings' data - mp_uint_t hash = qstr_compute_hash((const byte *)str, len); + size_t hash = qstr_compute_hash((const byte *)str, len); memcpy(q_ptr, str, len); q_ptr[len] = '\0'; q = qstr_add(hash, len, q_ptr); diff --git a/py/qstr.h b/py/qstr.h index a463b67a1..0ef861f33 100644 --- a/py/qstr.h +++ b/py/qstr.h @@ -78,7 +78,7 @@ typedef struct _qstr_pool_t { void qstr_init(void); -mp_uint_t qstr_compute_hash(const byte *data, size_t len); +size_t qstr_compute_hash(const byte *data, size_t len); qstr qstr_find_strn(const char *str, size_t str_len); // returns MP_QSTRnull if not found qstr qstr_from_str(const char *str);