diff options
author | Damien George <damien.p.george@gmail.com> | 2015-01-11 22:27:30 +0000 |
---|---|---|
committer | Damien George <damien.p.george@gmail.com> | 2015-01-11 22:27:30 +0000 |
commit | 95836f8439b9f1ca0b6ff20f56a03253aa9ba836 (patch) | |
tree | c9fa6b9d1c2aab8d9db9cf1884176a7b4b67c3d5 /py/qstr.c | |
parent | 6942f80a8feb521ff4dc2b457fa5cb3d039bee54 (diff) |
py: Add MICROPY_QSTR_BYTES_IN_LEN config option, defaulting to 1.
This new config option sets how many fixed-number-of-bytes to use to
store the length of each qstr. Previously this was hard coded to 2,
but, as per issue #1056, this is considered overkill since no-one
needs identifiers longer than 255 bytes.
With this patch the number of bytes for the length is configurable, and
defaults to 1 byte. The configuration option filters through to the
makeqstrdata.py script.
Code size savings going from 2 to 1 byte:
- unix x64 down by 592 bytes
- stmhal down by 1148 bytes
- bare-arm down by 284 bytes
Also has RAM savings, and will be slightly more efficient in execution.
Diffstat (limited to 'py/qstr.c')
-rw-r--r-- | py/qstr.c | 32 |
1 files changed, 19 insertions, 13 deletions
@@ -50,9 +50,17 @@ // - \0 terminated (for now, so they can be printed using printf) #define Q_GET_HASH(q) ((q)[0] | ((q)[1] << 8)) -#define Q_GET_ALLOC(q) (4 + Q_GET_LENGTH(q) + 1) -#define Q_GET_LENGTH(q) ((q)[2] | ((q)[3] << 8)) -#define Q_GET_DATA(q) ((q) + 4) +#define Q_GET_ALLOC(q) (2 + MICROPY_QSTR_BYTES_IN_LEN + Q_GET_LENGTH(q) + 1) +#define Q_GET_DATA(q) ((q) + 2 + MICROPY_QSTR_BYTES_IN_LEN) +#if MICROPY_QSTR_BYTES_IN_LEN == 1 + #define Q_GET_LENGTH(q) ((q)[2]) + #define Q_SET_LENGTH(q, len) do { (q)[2] = (len); } while (0) +#elif MICROPY_QSTR_BYTES_IN_LEN == 2 + #define Q_GET_LENGTH(q) ((q)[2] | ((q)[3] << 8)) + #define Q_SET_LENGTH(q, len) do { (q)[2] = (len); (q)[3] = (len) >> 8; } while (0) +#else + #error unimplemented qstr length decoding +#endif // this must match the equivalent function in makeqstrdata.py mp_uint_t qstr_compute_hash(const byte *data, mp_uint_t len) { @@ -143,23 +151,21 @@ qstr qstr_from_strn(const char *str, mp_uint_t len) { qstr q = qstr_find_strn(str, len); if (q == 0) { mp_uint_t hash = qstr_compute_hash((const byte*)str, len); - byte *q_ptr = m_new(byte, 4 + len + 1); + byte *q_ptr = m_new(byte, 2 + MICROPY_QSTR_BYTES_IN_LEN + len + 1); q_ptr[0] = hash; q_ptr[1] = hash >> 8; - q_ptr[2] = len; - q_ptr[3] = len >> 8; - memcpy(q_ptr + 4, str, len); - q_ptr[4 + len] = '\0'; + Q_SET_LENGTH(q_ptr, len); + memcpy(q_ptr + 2 + MICROPY_QSTR_BYTES_IN_LEN, str, len); + q_ptr[2 + MICROPY_QSTR_BYTES_IN_LEN + len] = '\0'; q = qstr_add(q_ptr); } return q; } byte *qstr_build_start(mp_uint_t len, byte **q_ptr) { - assert(len <= 65535); - *q_ptr = m_new(byte, 4 + len + 1); - (*q_ptr)[2] = len; - (*q_ptr)[3] = len >> 8; + assert(len < (1 << (8 * MICROPY_QSTR_BYTES_IN_LEN))); + *q_ptr = m_new(byte, 2 + MICROPY_QSTR_BYTES_IN_LEN + len + 1); + Q_SET_LENGTH(*q_ptr, len); return Q_GET_DATA(*q_ptr); } @@ -170,7 +176,7 @@ qstr qstr_build_end(byte *q_ptr) { mp_uint_t hash = qstr_compute_hash(Q_GET_DATA(q_ptr), len); q_ptr[0] = hash; q_ptr[1] = hash >> 8; - q_ptr[4 + len] = '\0'; + q_ptr[2 + MICROPY_QSTR_BYTES_IN_LEN + len] = '\0'; q = qstr_add(q_ptr); } else { m_del(byte, q_ptr, Q_GET_ALLOC(q_ptr)); |