From d5e7f6e37edc2fecbc0b3ceb19df425bafd45c90 Mon Sep 17 00:00:00 2001 From: Damien George Date: Fri, 22 Aug 2014 18:17:02 +0100 Subject: [PATCH] py: Speed up GC allocation. This simple patch gives a very significant speed up for memory allocation with the GC. Eg, on PYBv1.0: tests/basics/dict_del.py: 3.55 seconds -> 1.19 seconds tests/misc/rge_sm.py: 15.3 seconds -> 2.48 seconds --- py/gc.c | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/py/gc.c b/py/gc.c index 39dcd3c1d8..a8463fe9b0 100644 --- a/py/gc.c +++ b/py/gc.c @@ -62,6 +62,7 @@ STATIC int gc_stack_overflow; STATIC mp_uint_t gc_stack[STACK_SIZE]; STATIC mp_uint_t *gc_sp; STATIC mp_uint_t gc_lock_depth; +STATIC mp_uint_t gc_last_free_atb_index; // ATB = allocation table byte // 0b00 = FREE -- free block @@ -156,6 +157,9 @@ void gc_init(void *start, void *end) { gc_pool_start[i] = 0; } + // set last free ATB index to start of heap + gc_last_free_atb_index = 0; + // unlock the GC gc_lock_depth = 0; @@ -304,6 +308,7 @@ void gc_collect_root(void **ptrs, mp_uint_t len) { void gc_collect_end(void) { gc_deal_with_stack_overflow(); gc_sweep(); + gc_last_free_atb_index = 0; gc_unlock(); } @@ -374,7 +379,14 @@ void *gc_alloc(mp_uint_t n_bytes, bool has_finaliser) { for (;;) { // look for a run of n_blocks available blocks - for (i = 0; i < gc_alloc_table_byte_len; i++) { + for (i = gc_last_free_atb_index; i < gc_alloc_table_byte_len; i++) { + byte a = gc_alloc_table_start[i]; + if (ATB_0_IS_FREE(a)) { if (++n_free >= n_blocks) { i = i * BLOCKS_PER_ATB + 0; goto found; } } else { n_free = 0; } + if (ATB_1_IS_FREE(a)) { if (++n_free >= n_blocks) { i = i * BLOCKS_PER_ATB + 1; goto found; } } else { n_free = 0; } + if (ATB_2_IS_FREE(a)) { if (++n_free >= n_blocks) { i = i * BLOCKS_PER_ATB + 2; goto found; } } else { n_free = 0; } + if (ATB_3_IS_FREE(a)) { if (++n_free >= n_blocks) { i = i * BLOCKS_PER_ATB + 3; goto found; } } else { n_free = 0; } + } + for (i = 0; i < gc_last_free_atb_index; i++) { byte a = gc_alloc_table_start[i]; if (ATB_0_IS_FREE(a)) { if (++n_free >= n_blocks) { i = i * BLOCKS_PER_ATB + 0; goto found; } } else { n_free = 0; } if (ATB_1_IS_FREE(a)) { if (++n_free >= n_blocks) { i = i * BLOCKS_PER_ATB + 1; goto found; } } else { n_free = 0; } @@ -397,6 +409,9 @@ found: end_block = i; start_block = i - n_free + 1; + // set last free ATB index to last block we found, for start of next scan + gc_last_free_atb_index = i / BLOCKS_PER_ATB; + // mark first block as used head ATB_FREE_TO_HEAD(start_block);