diff options
| author | Scott Shawcroft <scott.shawcroft@gmail.com> | 2018-01-23 16:22:05 -0800 |
|---|---|---|
| committer | Scott Shawcroft <scott.shawcroft@gmail.com> | 2018-01-24 10:33:46 -0800 |
| commit | 416abe33ed44e44d8f75342731080217e123278b (patch) | |
| tree | 73e35f752feccaeb8696112a13ba743cb041168d /py/builtinimport.c | |
| parent | 56bd0789af7b11bcc15dc901745f61b83b1f7ff3 (diff) | |
Introduce a long lived section of the heap.
This adapts the allocation process to start from either end of the heap
when searching for free space. The default behavior is identical to the
existing behavior where it starts with the lowest block and looks higher.
Now it can also look from the highest block and lower depending on the
long_lived parameter to gc_alloc. As the heap fills, the two sections may
overlap. When they overlap, a collect may be triggered in order to keep
the long lived section compact. However, free space is always eligable
for each type of allocation.
By starting from either of the end of the heap we have ability to separate
short lived objects from long lived ones. This separation reduces heap
fragmentation because long lived objects are easy to densely pack.
Most objects are short lived initially but may be made long lived when
they are referenced by a type or module. This involves copying the
memory and then letting the collect phase free the old portion.
QSTR pools and chunks are always long lived because they are never freed.
The reallocation, collection and free processes are largely unchanged. They
simply also maintain an index to the highest free block as well as the lowest.
These indices are used to speed up the allocation search until the next collect.
In practice, this change may slightly slow down import statements with the
benefit that memory is much less fragmented afterwards. For example, a test
import into a 20k heap that leaves ~6k free previously had the largest
continuous free space of ~400 bytes. After this change, the largest continuous
free space is over 3400 bytes.
Diffstat (limited to 'py/builtinimport.c')
| -rw-r--r-- | py/builtinimport.c | 8 |
1 files changed, 8 insertions, 0 deletions
diff --git a/py/builtinimport.c b/py/builtinimport.c index b76ea00bd..9f7d34dca 100644 --- a/py/builtinimport.c +++ b/py/builtinimport.c @@ -30,6 +30,7 @@ #include <assert.h> #include "py/compile.h" +#include "py/gc_long_lived.h" #include "py/objmodule.h" #include "py/persistentcode.h" #include "py/runtime.h" @@ -144,6 +145,7 @@ STATIC void do_load_from_lexer(mp_obj_t module_obj, mp_lexer_t *lex) { // parse, compile and execute the module in its context mp_obj_dict_t *mod_globals = mp_obj_module_get_globals(module_obj); mp_parse_compile_execute(lex, MP_PARSE_FILE_INPUT, mod_globals, mod_globals); + mp_obj_module_set_globals(module_obj, make_dict_long_lived(mod_globals, 10)); } #endif @@ -173,6 +175,8 @@ STATIC void do_execute_raw_code(mp_obj_t module_obj, mp_raw_code_t *raw_code) { // finish nlr block, restore context nlr_pop(); + mp_obj_module_set_globals(module_obj, + make_dict_long_lived(mp_obj_module_get_globals(module_obj), 10)); mp_globals_set(old_globals); mp_locals_set(old_locals); } else { @@ -468,6 +472,10 @@ mp_obj_t mp_builtin___import__(size_t n_args, const mp_obj_t *args) { if (outer_module_obj != MP_OBJ_NULL) { qstr s = qstr_from_strn(mod_str + last, i - last); mp_store_attr(outer_module_obj, s, module_obj); + // The above store can cause a dictionary rehash and new allocation. So, + // lets make sure the globals dictionary is still long lived. + mp_obj_module_set_globals(outer_module_obj, + make_dict_long_lived(mp_obj_module_get_globals(outer_module_obj), 10)); } outer_module_obj = module_obj; if (top_module_obj == MP_OBJ_NULL) { |
