diff options
author | Damien George <damien.p.george@gmail.com> | 2015-06-13 21:53:22 +0100 |
---|---|---|
committer | Damien George <damien.p.george@gmail.com> | 2015-07-14 22:56:32 +0100 |
commit | ade9a052365247be5ed4ce8b53c2164a576a4a05 (patch) | |
tree | 505cc643084f47a018b52b7a12df98aaf653ee29 /py/gc.c | |
parent | c48740e20b5bfee6cfee271f67f8e99e9df102d7 (diff) | |
download | micropython-ade9a052365247be5ed4ce8b53c2164a576a4a05.tar.gz micropython-ade9a052365247be5ed4ce8b53c2164a576a4a05.zip |
py: Improve allocation policy of qstr data.
Previous to this patch all interned strings lived in their own malloc'd
chunk. On average this wastes N/2 bytes per interned string, where N is
the number-of-bytes for a quanta of the memory allocator (16 bytes on 32
bit archs).
With this patch interned strings are concatenated into the same malloc'd
chunk when possible. Such chunks are enlarged inplace when possible,
and shrunk to fit when a new chunk is needed.
RAM savings with this patch are highly varied, but should always show an
improvement (unless only 3 or 4 strings are interned). New version
typically uses about 70% of previous memory for the qstr data, and can
lead to savings of around 10% of total memory footprint of a running
script.
Costs about 120 bytes code size on Thumb2 archs (depends on how many
calls to gc_realloc are made).
Diffstat (limited to 'py/gc.c')
-rw-r--r-- | py/gc.c | 7 |
1 files changed, 6 insertions, 1 deletions
@@ -522,7 +522,7 @@ void *gc_realloc(void *ptr, mp_uint_t n_bytes) { #else // Alternative gc_realloc impl -void *gc_realloc(void *ptr_in, mp_uint_t n_bytes) { +void *gc_realloc(void *ptr_in, mp_uint_t n_bytes, bool allow_move) { if (MP_STATE_MEM(gc_lock_depth) > 0) { return NULL; } @@ -624,6 +624,11 @@ void *gc_realloc(void *ptr_in, mp_uint_t n_bytes) { return ptr_in; } + if (!allow_move) { + // not allowed to move memory block so return failure + return NULL; + } + // can't resize inplace; try to find a new contiguous chain void *ptr_out = gc_alloc(n_bytes, #if MICROPY_ENABLE_FINALISER |