diff options
| author | Scott Shawcroft <scott@tannewt.org> | 2018-07-23 18:17:08 -0700 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2018-07-23 18:17:08 -0700 |
| commit | 08def6b51ecb9853ccbe21fc6feecbb18cc3e387 (patch) | |
| tree | f40d5269a71138367f49f70fcdf39adeccb204a9 /py/gc_long_lived.c | |
| parent | 85a5276f36fb8b1be981d3155e4f0ef0c4cf72df (diff) | |
| parent | be1d882a8b5d2878c9b4402ae641abf9a88cd682 (diff) | |
Merge pull request #1052 from dhalbert/speed-up-dict-long-lived
Prevent repetitive recursive scanning of dicts when making them long-…
Diffstat (limited to 'py/gc_long_lived.c')
| -rw-r--r-- | py/gc_long_lived.c | 12 |
1 files changed, 11 insertions, 1 deletions
diff --git a/py/gc_long_lived.c b/py/gc_long_lived.c index d34fde5d9..dd5f0e8fd 100644 --- a/py/gc_long_lived.c +++ b/py/gc_long_lived.c @@ -91,6 +91,13 @@ mp_obj_dict_t *make_dict_long_lived(mp_obj_dict_t *dict, uint8_t max_depth) { if (dict == NULL || max_depth == 0) { return dict; } + // Don't recurse unnecessarily. Return immediately if we've already seen this dict. + if (dict->map.scanning) { + return dict; + } + // Mark that we're processing this dict. + dict->map.scanning = 1; + // Update all of the references first so that we reduce the chance of references to the old // copies. dict->map.table = gc_make_long_lived(dict->map.table); @@ -100,7 +107,10 @@ mp_obj_dict_t *make_dict_long_lived(mp_obj_dict_t *dict, uint8_t max_depth) { dict->map.table[i].value = make_obj_long_lived(value, max_depth - 1); } } - return gc_make_long_lived(dict); + dict = gc_make_long_lived(dict); + // Done recursing through this dict. + dict->map.scanning = 0; + return dict; } mp_obj_str_t *make_str_long_lived(mp_obj_str_t *str) { |
