2014-05-03 18:27:38 -04:00
|
|
|
/*
|
2017-06-30 03:22:17 -04:00
|
|
|
* This file is part of the MicroPython project, http://micropython.org/
|
2014-05-03 18:27:38 -04:00
|
|
|
*
|
|
|
|
* The MIT License (MIT)
|
|
|
|
*
|
|
|
|
* Copyright (c) 2013, 2014 Damien P. George
|
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
* THE SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
2013-10-04 14:53:11 -04:00
|
|
|
#include <assert.h>
|
|
|
|
#include <string.h>
|
2015-02-10 06:02:28 -05:00
|
|
|
#include <stdio.h>
|
2013-10-04 14:53:11 -04:00
|
|
|
|
2015-01-01 18:30:53 -05:00
|
|
|
#include "py/mpstate.h"
|
2015-01-01 15:27:54 -05:00
|
|
|
#include "py/qstr.h"
|
|
|
|
#include "py/gc.h"
|
2021-04-23 15:26:42 -04:00
|
|
|
#include "py/runtime.h"
|
2013-10-04 14:53:11 -04:00
|
|
|
|
2023-10-19 16:42:36 -04:00
|
|
|
// CIRCUITPY-CHANGE: changes for TRANSLATION
|
2023-10-18 17:49:14 -04:00
|
|
|
|
2014-01-04 10:57:35 -05:00
|
|
|
// NOTE: we are using linear arrays to store and search for qstr's (unique strings, interned strings)
|
|
|
|
// ultimately we will replace this with a static hash table of some kind
|
|
|
|
|
2017-07-24 12:55:14 -04:00
|
|
|
#if MICROPY_DEBUG_VERBOSE // print debugging info
|
2014-02-16 11:11:42 -05:00
|
|
|
#define DEBUG_printf DEBUG_printf
|
2014-01-04 10:57:35 -05:00
|
|
|
#else // don't print debugging info
|
2014-02-26 11:57:08 -05:00
|
|
|
#define DEBUG_printf(...) (void)0
|
2014-01-04 10:57:35 -05:00
|
|
|
#endif
|
|
|
|
|
2014-01-21 16:40:13 -05:00
|
|
|
// A qstr is an index into the qstr pool.
|
2021-04-06 12:33:03 -04:00
|
|
|
// The data for a qstr is \0 terminated (so they can be printed using printf)
|
|
|
|
|
|
|
|
#define Q_HASH_MASK ((1 << (8 * MICROPY_QSTR_BYTES_IN_HASH)) - 1)
|
2014-01-21 16:40:13 -05:00
|
|
|
|
2016-05-26 06:53:34 -04:00
|
|
|
#if MICROPY_PY_THREAD && !MICROPY_PY_THREAD_GIL
|
2016-05-26 05:06:46 -04:00
|
|
|
#define QSTR_ENTER() mp_thread_mutex_lock(&MP_STATE_VM(qstr_mutex), 1)
|
|
|
|
#define QSTR_EXIT() mp_thread_mutex_unlock(&MP_STATE_VM(qstr_mutex))
|
|
|
|
#else
|
|
|
|
#define QSTR_ENTER()
|
|
|
|
#define QSTR_EXIT()
|
|
|
|
#endif
|
|
|
|
|
2018-12-13 01:08:26 -05:00
|
|
|
// Initial number of entries for qstr pool, set so that the first dynamically
|
|
|
|
// allocated pool is twice this size. The value here must be <= MP_QSTRnumber_of.
|
|
|
|
#define MICROPY_ALLOC_QSTR_ENTRIES_INIT (10)
|
|
|
|
|
2014-03-25 11:27:15 -04:00
|
|
|
// this must match the equivalent function in makeqstrdata.py
|
2022-08-11 02:34:02 -04:00
|
|
|
size_t qstr_compute_hash(const byte *data, size_t len) {
|
2014-03-25 11:27:15 -04:00
|
|
|
// djb2 algorithm; see http://www.cse.yorku.ca/~oz/hash.html
|
2022-08-11 02:34:02 -04:00
|
|
|
size_t hash = 5381;
|
2014-01-21 16:40:13 -05:00
|
|
|
for (const byte *top = data + len; data < top; data++) {
|
2014-03-25 11:27:15 -04:00
|
|
|
hash = ((hash << 5) + hash) ^ (*data); // hash * 33 ^ data
|
2014-01-21 16:40:13 -05:00
|
|
|
}
|
2015-07-20 07:03:13 -04:00
|
|
|
hash &= Q_HASH_MASK;
|
2014-04-13 18:43:01 -04:00
|
|
|
// Make sure that valid hash is never zero, zero means "hash not computed"
|
|
|
|
if (hash == 0) {
|
|
|
|
hash++;
|
|
|
|
}
|
|
|
|
return hash;
|
2014-01-21 16:40:13 -05:00
|
|
|
}
|
|
|
|
|
2021-05-03 14:17:36 -04:00
|
|
|
const qstr_hash_t mp_qstr_const_hashes[] = {
|
2021-04-30 16:30:13 -04:00
|
|
|
#ifndef NO_QSTR
|
2021-05-03 14:17:36 -04:00
|
|
|
#define QDEF(id, hash, len, str) hash,
|
2023-08-07 20:45:57 -04:00
|
|
|
#define TRANSLATION(id, length, compressed ...)
|
2021-04-30 16:30:13 -04:00
|
|
|
#include "genhdr/qstrdefs.generated.h"
|
2023-08-07 20:45:57 -04:00
|
|
|
#undef TRANSLATION
|
2021-04-06 12:33:03 -04:00
|
|
|
#undef QDEF
|
2021-04-30 16:30:13 -04:00
|
|
|
#endif
|
2021-04-06 12:33:03 -04:00
|
|
|
};
|
|
|
|
|
2021-05-03 14:17:36 -04:00
|
|
|
const qstr_len_t mp_qstr_const_lengths[] = {
|
|
|
|
#ifndef NO_QSTR
|
|
|
|
#define QDEF(id, hash, len, str) len,
|
2023-08-07 20:45:57 -04:00
|
|
|
#define TRANSLATION(id, length, compressed ...)
|
2021-05-03 14:17:36 -04:00
|
|
|
#include "genhdr/qstrdefs.generated.h"
|
2023-08-07 20:45:57 -04:00
|
|
|
#undef TRANSLATION
|
2021-05-03 14:17:36 -04:00
|
|
|
#undef QDEF
|
|
|
|
#endif
|
|
|
|
};
|
|
|
|
|
2016-01-31 17:24:16 -05:00
|
|
|
const qstr_pool_t mp_qstr_const_pool = {
|
2014-01-04 10:57:35 -05:00
|
|
|
NULL, // no previous pool
|
|
|
|
0, // no previous pool
|
2018-12-13 01:08:26 -05:00
|
|
|
MICROPY_ALLOC_QSTR_ENTRIES_INIT,
|
2016-01-31 17:24:16 -05:00
|
|
|
MP_QSTRnumber_of, // corresponds to number of strings in array just below
|
2021-05-03 14:17:36 -04:00
|
|
|
(qstr_hash_t *)mp_qstr_const_hashes,
|
|
|
|
(qstr_len_t *)mp_qstr_const_lengths,
|
2014-01-04 10:57:35 -05:00
|
|
|
{
|
2021-03-15 09:57:36 -04:00
|
|
|
#ifndef NO_QSTR
|
2021-04-06 12:33:03 -04:00
|
|
|
#define QDEF(id, hash, len, str) str,
|
2021-03-15 09:57:36 -04:00
|
|
|
#define TRANSLATION(id, length, compressed ...)
|
|
|
|
#include "genhdr/qstrdefs.generated.h"
|
2018-07-31 19:53:54 -04:00
|
|
|
#undef TRANSLATION
|
2015-01-11 12:52:45 -05:00
|
|
|
#undef QDEF
|
2021-03-15 09:57:36 -04:00
|
|
|
#endif
|
2014-01-04 10:57:35 -05:00
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2016-01-31 17:24:16 -05:00
|
|
|
#ifdef MICROPY_QSTR_EXTRA_POOL
|
|
|
|
extern const qstr_pool_t MICROPY_QSTR_EXTRA_POOL;
|
|
|
|
#define CONST_POOL MICROPY_QSTR_EXTRA_POOL
|
|
|
|
#else
|
|
|
|
#define CONST_POOL mp_qstr_const_pool
|
|
|
|
#endif
|
|
|
|
|
2023-10-19 16:42:36 -04:00
|
|
|
// CIRCUITPY-CHANGE
|
2023-09-29 17:51:13 -04:00
|
|
|
void qstr_reset(void) {
|
2021-03-15 09:57:36 -04:00
|
|
|
MP_STATE_VM(last_pool) = (qstr_pool_t *)&CONST_POOL; // we won't modify the const_pool since it has no allocated room left
|
2015-06-13 16:53:22 -04:00
|
|
|
MP_STATE_VM(qstr_last_chunk) = NULL;
|
2023-09-29 17:51:13 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void qstr_init(void) {
|
|
|
|
qstr_reset();
|
2016-05-26 05:06:46 -04:00
|
|
|
|
2020-01-22 12:19:37 -05:00
|
|
|
#if MICROPY_PY_THREAD && !MICROPY_PY_THREAD_GIL
|
2016-05-26 05:06:46 -04:00
|
|
|
mp_thread_mutex_init(&MP_STATE_VM(qstr_mutex));
|
|
|
|
#endif
|
2013-10-04 14:53:11 -04:00
|
|
|
}
|
|
|
|
|
2023-08-01 13:50:05 -04:00
|
|
|
STATIC const qstr_pool_t *PLACE_IN_ITCM(find_qstr)(qstr *q) {
|
2014-01-21 16:40:13 -05:00
|
|
|
// search pool for this qstr
|
2017-11-29 00:58:27 -05:00
|
|
|
// total_prev_len==0 in the final pool, so the loop will always terminate
|
2021-05-04 03:35:45 -04:00
|
|
|
const qstr_pool_t *pool = MP_STATE_VM(last_pool);
|
2021-05-03 14:17:36 -04:00
|
|
|
while (*q < pool->total_prev_len) {
|
2017-11-29 00:58:27 -05:00
|
|
|
pool = pool->prev;
|
2014-01-21 16:40:13 -05:00
|
|
|
}
|
2021-05-03 14:17:36 -04:00
|
|
|
*q -= pool->total_prev_len;
|
|
|
|
assert(*q < pool->len);
|
|
|
|
return pool;
|
2014-01-21 16:40:13 -05:00
|
|
|
}
|
|
|
|
|
2016-05-26 05:06:46 -04:00
|
|
|
// qstr_mutex must be taken while in this function
|
2021-04-06 12:33:03 -04:00
|
|
|
STATIC qstr qstr_add(mp_uint_t hash, mp_uint_t len, const char *q_ptr) {
|
|
|
|
DEBUG_printf("QSTR: add hash=%d len=%d data=%.*s\n", hash, len, len, q_ptr);
|
2014-01-04 10:57:35 -05:00
|
|
|
|
|
|
|
// make sure we have room in the pool for a new qstr
|
2015-01-01 18:30:53 -05:00
|
|
|
if (MP_STATE_VM(last_pool)->len >= MP_STATE_VM(last_pool)->alloc) {
|
2018-12-13 01:08:26 -05:00
|
|
|
size_t new_alloc = MP_STATE_VM(last_pool)->alloc * 2;
|
|
|
|
#ifdef MICROPY_QSTR_EXTRA_POOL
|
|
|
|
// Put a lower bound on the allocation size in case the extra qstr pool has few entries
|
|
|
|
new_alloc = MAX(MICROPY_ALLOC_QSTR_ENTRIES_INIT, new_alloc);
|
|
|
|
#endif
|
2021-05-05 02:01:47 -04:00
|
|
|
mp_uint_t pool_size = sizeof(qstr_pool_t)
|
2021-05-03 14:17:36 -04:00
|
|
|
+ (sizeof(const char *) + sizeof(qstr_hash_t) + sizeof(qstr_len_t)) * new_alloc;
|
2023-08-08 20:41:17 -04:00
|
|
|
qstr_pool_t *pool = (qstr_pool_t *)m_malloc_maybe(pool_size);
|
2021-05-05 02:01:47 -04:00
|
|
|
if (pool == NULL) {
|
2021-12-27 09:39:34 -05:00
|
|
|
// Keep qstr_last_chunk consistent with qstr_pool_t: qstr_last_chunk is not scanned
|
|
|
|
// at garbage collection since it's reachable from a qstr_pool_t. And the caller of
|
|
|
|
// this function expects q_ptr to be stored in a qstr_pool_t so it can be reached
|
|
|
|
// by the collector. If qstr_pool_t allocation failed, qstr_last_chunk needs to be
|
|
|
|
// NULL'd. Otherwise it may become a dangling pointer at the next garbage collection.
|
|
|
|
MP_STATE_VM(qstr_last_chunk) = NULL;
|
2016-05-26 05:06:46 -04:00
|
|
|
QSTR_EXIT();
|
2018-12-13 01:08:26 -05:00
|
|
|
m_malloc_fail(new_alloc);
|
2016-05-26 05:06:46 -04:00
|
|
|
}
|
2021-05-03 14:17:36 -04:00
|
|
|
pool->hashes = (qstr_hash_t *)(pool->qstrs + new_alloc);
|
|
|
|
pool->lengths = (qstr_len_t *)(pool->hashes + new_alloc);
|
2015-01-01 18:30:53 -05:00
|
|
|
pool->prev = MP_STATE_VM(last_pool);
|
|
|
|
pool->total_prev_len = MP_STATE_VM(last_pool)->total_prev_len + MP_STATE_VM(last_pool)->len;
|
2018-12-13 01:08:26 -05:00
|
|
|
pool->alloc = new_alloc;
|
2014-01-04 10:57:35 -05:00
|
|
|
pool->len = 0;
|
2015-01-01 18:30:53 -05:00
|
|
|
MP_STATE_VM(last_pool) = pool;
|
|
|
|
DEBUG_printf("QSTR: allocate new pool of size %d\n", MP_STATE_VM(last_pool)->alloc);
|
2013-10-04 14:53:11 -04:00
|
|
|
}
|
2014-01-04 10:57:35 -05:00
|
|
|
|
|
|
|
// add the new qstr
|
2021-04-06 12:33:03 -04:00
|
|
|
mp_uint_t at = MP_STATE_VM(last_pool)->len;
|
2021-05-03 14:17:36 -04:00
|
|
|
MP_STATE_VM(last_pool)->hashes[at] = hash;
|
|
|
|
MP_STATE_VM(last_pool)->lengths[at] = len;
|
2021-04-06 12:33:03 -04:00
|
|
|
MP_STATE_VM(last_pool)->qstrs[at] = q_ptr;
|
|
|
|
MP_STATE_VM(last_pool)->len++;
|
2014-01-04 10:57:35 -05:00
|
|
|
|
|
|
|
// return id for the newly-added qstr
|
2021-04-06 12:33:03 -04:00
|
|
|
return MP_STATE_VM(last_pool)->total_prev_len + at;
|
2013-10-04 14:53:11 -04:00
|
|
|
}
|
|
|
|
|
2015-11-27 07:23:18 -05:00
|
|
|
qstr qstr_find_strn(const char *str, size_t str_len) {
|
2014-01-21 16:40:13 -05:00
|
|
|
// work out hash of str
|
2022-08-11 02:34:02 -04:00
|
|
|
size_t str_hash = qstr_compute_hash((const byte *)str, str_len);
|
2014-01-21 16:40:13 -05:00
|
|
|
|
|
|
|
// search pools for the data
|
2021-05-04 03:35:45 -04:00
|
|
|
for (const qstr_pool_t *pool = MP_STATE_VM(last_pool); pool != NULL; pool = pool->prev) {
|
2021-04-06 12:33:03 -04:00
|
|
|
for (mp_uint_t at = 0, top = pool->len; at < top; at++) {
|
2021-05-03 14:17:36 -04:00
|
|
|
if (pool->hashes[at] == str_hash && pool->lengths[at] == str_len
|
|
|
|
&& memcmp(pool->qstrs[at], str, str_len) == 0) {
|
2021-04-06 12:33:03 -04:00
|
|
|
return pool->total_prev_len + at;
|
2014-01-04 10:57:35 -05:00
|
|
|
}
|
2013-10-04 14:53:11 -04:00
|
|
|
}
|
|
|
|
}
|
2014-01-21 16:40:13 -05:00
|
|
|
|
|
|
|
// not found; return null qstr
|
|
|
|
return 0;
|
2013-10-04 14:53:11 -04:00
|
|
|
}
|
|
|
|
|
2014-01-21 16:40:13 -05:00
|
|
|
qstr qstr_from_str(const char *str) {
|
|
|
|
return qstr_from_strn(str, strlen(str));
|
2013-10-04 14:53:11 -04:00
|
|
|
}
|
|
|
|
|
2015-11-27 07:23:18 -05:00
|
|
|
qstr qstr_from_strn(const char *str, size_t len) {
|
2016-05-26 05:06:46 -04:00
|
|
|
QSTR_ENTER();
|
2014-05-25 17:27:57 -04:00
|
|
|
qstr q = qstr_find_strn(str, len);
|
2014-01-21 16:40:13 -05:00
|
|
|
if (q == 0) {
|
2015-06-13 16:53:22 -04:00
|
|
|
// qstr does not exist in interned pool so need to add it
|
|
|
|
|
2021-04-23 15:26:42 -04:00
|
|
|
// check that len is not too big
|
|
|
|
if (len >= (1 << (8 * MICROPY_QSTR_BYTES_IN_LEN))) {
|
|
|
|
QSTR_EXIT();
|
2021-05-04 14:40:55 -04:00
|
|
|
mp_raise_msg(&mp_type_RuntimeError, MP_ERROR_TEXT("Name too long"));
|
2021-04-23 15:26:42 -04:00
|
|
|
}
|
|
|
|
|
2015-06-13 16:53:22 -04:00
|
|
|
// compute number of bytes needed to intern this string
|
2021-04-06 12:33:03 -04:00
|
|
|
size_t n_bytes = len + 1;
|
2015-06-13 16:53:22 -04:00
|
|
|
|
|
|
|
if (MP_STATE_VM(qstr_last_chunk) != NULL && MP_STATE_VM(qstr_last_used) + n_bytes > MP_STATE_VM(qstr_last_alloc)) {
|
|
|
|
// not enough room at end of previously interned string so try to grow
|
2021-04-06 12:33:03 -04:00
|
|
|
char *new_p = m_renew_maybe(char, MP_STATE_VM(qstr_last_chunk), MP_STATE_VM(qstr_last_alloc), MP_STATE_VM(qstr_last_alloc) + n_bytes, false);
|
2015-06-13 16:53:22 -04:00
|
|
|
if (new_p == NULL) {
|
|
|
|
// could not grow existing memory; shrink it to fit previous
|
2021-04-06 12:33:03 -04:00
|
|
|
(void)m_renew_maybe(char, MP_STATE_VM(qstr_last_chunk), MP_STATE_VM(qstr_last_alloc), MP_STATE_VM(qstr_last_used), false);
|
2015-06-13 16:53:22 -04:00
|
|
|
MP_STATE_VM(qstr_last_chunk) = NULL;
|
|
|
|
} else {
|
|
|
|
// could grow existing memory
|
|
|
|
MP_STATE_VM(qstr_last_alloc) += n_bytes;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (MP_STATE_VM(qstr_last_chunk) == NULL) {
|
|
|
|
// no existing memory for the interned string so allocate a new chunk
|
2015-11-27 07:23:18 -05:00
|
|
|
size_t al = n_bytes;
|
2015-06-13 16:53:22 -04:00
|
|
|
if (al < MICROPY_ALLOC_QSTR_CHUNK_INIT) {
|
|
|
|
al = MICROPY_ALLOC_QSTR_CHUNK_INIT;
|
|
|
|
}
|
2023-08-08 20:41:17 -04:00
|
|
|
MP_STATE_VM(qstr_last_chunk) = m_new_maybe(char, al);
|
2015-06-13 16:53:22 -04:00
|
|
|
if (MP_STATE_VM(qstr_last_chunk) == NULL) {
|
|
|
|
// failed to allocate a large chunk so try with exact size
|
2023-08-08 20:41:17 -04:00
|
|
|
MP_STATE_VM(qstr_last_chunk) = m_new_maybe(char, n_bytes);
|
2016-05-26 05:06:46 -04:00
|
|
|
if (MP_STATE_VM(qstr_last_chunk) == NULL) {
|
|
|
|
QSTR_EXIT();
|
|
|
|
m_malloc_fail(n_bytes);
|
|
|
|
}
|
2015-06-13 16:53:22 -04:00
|
|
|
al = n_bytes;
|
|
|
|
}
|
|
|
|
MP_STATE_VM(qstr_last_alloc) = al;
|
|
|
|
MP_STATE_VM(qstr_last_used) = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// allocate memory from the chunk for this new interned string's data
|
2021-04-06 12:33:03 -04:00
|
|
|
char *q_ptr = MP_STATE_VM(qstr_last_chunk) + MP_STATE_VM(qstr_last_used);
|
2015-06-13 16:53:22 -04:00
|
|
|
MP_STATE_VM(qstr_last_used) += n_bytes;
|
|
|
|
|
|
|
|
// store the interned strings' data
|
2022-08-11 02:34:02 -04:00
|
|
|
size_t hash = qstr_compute_hash((const byte *)str, len);
|
2021-04-06 12:33:03 -04:00
|
|
|
memcpy(q_ptr, str, len);
|
|
|
|
q_ptr[len] = '\0';
|
|
|
|
q = qstr_add(hash, len, q_ptr);
|
2013-10-04 14:53:11 -04:00
|
|
|
}
|
2016-05-26 05:06:46 -04:00
|
|
|
QSTR_EXIT();
|
2014-01-21 16:40:13 -05:00
|
|
|
return q;
|
2013-10-04 14:53:11 -04:00
|
|
|
}
|
|
|
|
|
2014-07-03 08:25:24 -04:00
|
|
|
mp_uint_t qstr_hash(qstr q) {
|
2021-05-04 03:35:45 -04:00
|
|
|
const qstr_pool_t *pool = find_qstr(&q);
|
2021-05-03 14:17:36 -04:00
|
|
|
return pool->hashes[q];
|
2014-01-21 16:40:13 -05:00
|
|
|
}
|
|
|
|
|
2015-11-27 07:23:18 -05:00
|
|
|
size_t qstr_len(qstr q) {
|
2021-05-04 03:35:45 -04:00
|
|
|
const qstr_pool_t *pool = find_qstr(&q);
|
2021-05-03 14:17:36 -04:00
|
|
|
return pool->lengths[q];
|
2014-01-21 16:40:13 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
const char *qstr_str(qstr q) {
|
2021-05-04 03:35:45 -04:00
|
|
|
const qstr_pool_t *pool = find_qstr(&q);
|
2021-05-03 14:17:36 -04:00
|
|
|
return pool->qstrs[q];
|
2014-01-21 16:40:13 -05:00
|
|
|
}
|
2014-01-04 10:57:35 -05:00
|
|
|
|
2015-11-27 07:23:18 -05:00
|
|
|
const byte *qstr_data(qstr q, size_t *len) {
|
2021-05-04 03:35:45 -04:00
|
|
|
const qstr_pool_t *pool = find_qstr(&q);
|
2021-05-03 14:17:36 -04:00
|
|
|
*len = pool->lengths[q];
|
|
|
|
return (byte *)pool->qstrs[q];
|
2013-10-04 14:53:11 -04:00
|
|
|
}
|
2014-01-29 13:56:46 -05:00
|
|
|
|
2015-12-17 07:41:40 -05:00
|
|
|
void qstr_pool_info(size_t *n_pool, size_t *n_qstr, size_t *n_str_data_bytes, size_t *n_total_bytes) {
|
2016-05-26 05:06:46 -04:00
|
|
|
QSTR_ENTER();
|
2014-01-29 13:56:46 -05:00
|
|
|
*n_pool = 0;
|
|
|
|
*n_qstr = 0;
|
|
|
|
*n_str_data_bytes = 0;
|
|
|
|
*n_total_bytes = 0;
|
2021-05-04 03:35:45 -04:00
|
|
|
for (const qstr_pool_t *pool = MP_STATE_VM(last_pool); pool != NULL && pool != &CONST_POOL; pool = pool->prev) {
|
2014-01-29 13:56:46 -05:00
|
|
|
*n_pool += 1;
|
|
|
|
*n_qstr += pool->len;
|
2021-05-03 14:17:36 -04:00
|
|
|
for (qstr_len_t *l = pool->lengths, *l_top = pool->lengths + pool->len; l < l_top; l++) {
|
|
|
|
*n_str_data_bytes += *l + 1;
|
2014-01-29 13:56:46 -05:00
|
|
|
}
|
2014-10-24 18:12:25 -04:00
|
|
|
#if MICROPY_ENABLE_GC
|
|
|
|
*n_total_bytes += gc_nbytes(pool); // this counts actual bytes used in heap
|
|
|
|
#else
|
2021-05-03 14:17:36 -04:00
|
|
|
*n_total_bytes += sizeof(qstr_pool_t)
|
|
|
|
+ (sizeof(const char *) + sizeof(qstr_hash_t) + sizeof(qstr_len_t)) * pool->alloc;
|
2014-10-24 18:12:25 -04:00
|
|
|
#endif
|
2014-01-29 13:56:46 -05:00
|
|
|
}
|
|
|
|
*n_total_bytes += *n_str_data_bytes;
|
2016-05-26 05:06:46 -04:00
|
|
|
QSTR_EXIT();
|
2014-01-29 13:56:46 -05:00
|
|
|
}
|
2015-02-10 06:02:28 -05:00
|
|
|
|
|
|
|
#if MICROPY_PY_MICROPYTHON_MEM_INFO
|
|
|
|
void qstr_dump_data(void) {
|
2016-05-26 05:06:46 -04:00
|
|
|
QSTR_ENTER();
|
2021-05-04 03:35:45 -04:00
|
|
|
for (const qstr_pool_t *pool = MP_STATE_VM(last_pool); pool != NULL && pool != &CONST_POOL; pool = pool->prev) {
|
|
|
|
for (const char *const *q = pool->qstrs, *const *q_top = pool->qstrs + pool->len; q < q_top; q++) {
|
2021-04-06 12:33:03 -04:00
|
|
|
mp_printf(&mp_plat_print, "Q(%s)\n", *q);
|
2015-02-10 06:02:28 -05:00
|
|
|
}
|
|
|
|
}
|
2016-05-26 05:06:46 -04:00
|
|
|
QSTR_EXIT();
|
2015-02-10 06:02:28 -05:00
|
|
|
}
|
|
|
|
#endif
|
py: Implement "common word" compression scheme for error messages.
The idea here is that there's a moderate amount of ROM used up by exception
text. Obviously we try to keep the messages short, and the code can enable
terse errors, but it still adds up. Listed below is the total string data
size for various ports:
bare-arm 2860
minimal 2876
stm32 8926 (PYBV11)
cc3200 3751
esp32 5721
This commit implements compression of these strings. It takes advantage of
the fact that these strings are all 7-bit ascii and extracts the top 128
frequently used words from the messages and stores them packed (dropping
their null-terminator), then uses (0x80 | index) inside strings to refer to
these common words. Spaces are automatically added around words, saving
more bytes. This happens transparently in the build process, mirroring the
steps that are used to generate the QSTR data. The MP_COMPRESSED_ROM_TEXT
macro wraps any literal string that should compressed, and it's
automatically decompressed in mp_decompress_rom_string.
There are many schemes that could be used for the compression, and some are
included in py/makecompresseddata.py for reference (space, Huffman, ngram,
common word). Results showed that the common-word compression gets better
results. This is before counting the increased cost of the Huffman
decoder. This might be slightly counter-intuitive, but this data is
extremely repetitive at a word-level, and the byte-level entropy coder
can't quite exploit that as efficiently. Ideally one would combine both
approaches, but for now the common-word approach is the one that is used.
For additional comparison, the size of the raw data compressed with gzip
and zlib is calculated, as a sort of proxy for a lower entropy bound. With
this scheme we come within 15% on stm32, and 30% on bare-arm (i.e. we use
x% more bytes than the data compressed with gzip -- not counting the code
overhead of a decoder, and how this would be hypothetically implemented).
The feature is disabled by default and can be enabled by setting
MICROPY_ROM_TEXT_COMPRESSION at the Makefile-level.
2019-09-26 08:19:29 -04:00
|
|
|
|
|
|
|
#if MICROPY_ROM_TEXT_COMPRESSION
|
|
|
|
|
|
|
|
#ifdef NO_QSTR
|
|
|
|
|
|
|
|
// If NO_QSTR is set, it means we're doing QSTR extraction.
|
|
|
|
// So we won't yet have "genhdr/compressed.data.h"
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
|
|
|
// Emit the compressed_string_data string.
|
|
|
|
#define MP_COMPRESSED_DATA(x) STATIC const char *compressed_string_data = x;
|
|
|
|
#define MP_MATCH_COMPRESSED(a, b)
|
|
|
|
#include "genhdr/compressed.data.h"
|
|
|
|
#undef MP_COMPRESSED_DATA
|
|
|
|
#undef MP_MATCH_COMPRESSED
|
|
|
|
|
|
|
|
#endif // NO_QSTR
|
|
|
|
|
|
|
|
// This implements the "common word" compression scheme (see makecompresseddata.py) where the most
|
|
|
|
// common 128 words in error messages are replaced by their index into the list of common words.
|
|
|
|
|
|
|
|
// The compressed string data is delimited by setting high bit in the final char of each word.
|
|
|
|
// e.g. aaaa<0x80|a>bbbbbb<0x80|b>....
|
|
|
|
// This method finds the n'th string.
|
|
|
|
STATIC const byte *find_uncompressed_string(uint8_t n) {
|
|
|
|
const byte *c = (byte *)compressed_string_data;
|
|
|
|
while (n > 0) {
|
|
|
|
while ((*c & 0x80) == 0) {
|
|
|
|
++c;
|
|
|
|
}
|
|
|
|
++c;
|
|
|
|
--n;
|
|
|
|
}
|
|
|
|
return c;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Given a compressed string in src, decompresses it into dst.
|
|
|
|
// dst must be large enough (use MP_MAX_UNCOMPRESSED_TEXT_LEN+1).
|
2023-10-25 15:40:11 -04:00
|
|
|
void mp_decompress_rom_string(byte *dst, mp_rom_error_text_t src_chr) {
|
py: Implement "common word" compression scheme for error messages.
The idea here is that there's a moderate amount of ROM used up by exception
text. Obviously we try to keep the messages short, and the code can enable
terse errors, but it still adds up. Listed below is the total string data
size for various ports:
bare-arm 2860
minimal 2876
stm32 8926 (PYBV11)
cc3200 3751
esp32 5721
This commit implements compression of these strings. It takes advantage of
the fact that these strings are all 7-bit ascii and extracts the top 128
frequently used words from the messages and stores them packed (dropping
their null-terminator), then uses (0x80 | index) inside strings to refer to
these common words. Spaces are automatically added around words, saving
more bytes. This happens transparently in the build process, mirroring the
steps that are used to generate the QSTR data. The MP_COMPRESSED_ROM_TEXT
macro wraps any literal string that should compressed, and it's
automatically decompressed in mp_decompress_rom_string.
There are many schemes that could be used for the compression, and some are
included in py/makecompresseddata.py for reference (space, Huffman, ngram,
common word). Results showed that the common-word compression gets better
results. This is before counting the increased cost of the Huffman
decoder. This might be slightly counter-intuitive, but this data is
extremely repetitive at a word-level, and the byte-level entropy coder
can't quite exploit that as efficiently. Ideally one would combine both
approaches, but for now the common-word approach is the one that is used.
For additional comparison, the size of the raw data compressed with gzip
and zlib is calculated, as a sort of proxy for a lower entropy bound. With
this scheme we come within 15% on stm32, and 30% on bare-arm (i.e. we use
x% more bytes than the data compressed with gzip -- not counting the code
overhead of a decoder, and how this would be hypothetically implemented).
The feature is disabled by default and can be enabled by setting
MICROPY_ROM_TEXT_COMPRESSION at the Makefile-level.
2019-09-26 08:19:29 -04:00
|
|
|
// Skip past the 0xff marker.
|
|
|
|
const byte *src = (byte *)src_chr + 1;
|
|
|
|
// Need to add spaces around compressed words, except for the first (i.e. transition from 1<->2).
|
|
|
|
// 0 = start, 1 = compressed, 2 = regular.
|
|
|
|
int state = 0;
|
|
|
|
while (*src) {
|
|
|
|
if ((byte) * src >= 128) {
|
|
|
|
if (state != 0) {
|
|
|
|
*dst++ = ' ';
|
|
|
|
}
|
|
|
|
state = 1;
|
|
|
|
|
|
|
|
// High bit set, replace with common word.
|
|
|
|
const byte *word = find_uncompressed_string(*src & 0x7f);
|
|
|
|
// The word is terminated by the final char having its high bit set.
|
|
|
|
while ((*word & 0x80) == 0) {
|
|
|
|
*dst++ = *word++;
|
|
|
|
}
|
|
|
|
*dst++ = (*word & 0x7f);
|
|
|
|
} else {
|
|
|
|
// Otherwise just copy one char.
|
|
|
|
if (state == 1) {
|
|
|
|
*dst++ = ' ';
|
|
|
|
}
|
|
|
|
state = 2;
|
|
|
|
|
|
|
|
*dst++ = *src;
|
|
|
|
}
|
|
|
|
++src;
|
|
|
|
}
|
|
|
|
// Add null-terminator.
|
|
|
|
*dst = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif // MICROPY_ROM_TEXT_COMPRESSION
|