py/gc: Make GC stack pointer a local variable.

This saves a bit in code size, and saves some precious .bss RAM:

                 .text  .bss
minimal CROSS=1: -28    -4
unix (64-bit):   -64    -8
This commit is contained in:
Ayke van Laethem 2018-02-15 17:22:34 +01:00 committed by Damien George
parent 5c9e5618e0
commit 736faef223
2 changed files with 5 additions and 7 deletions

11
py/gc.c
View File

@ -209,6 +209,7 @@ bool gc_is_locked(void) {
// topmost block on the stack and repeat with that one.
STATIC void gc_mark_subtree(size_t block) {
// Start with the block passed in the argument.
size_t sp = 0;
for (;;) {
// work out number of consecutive blocks in the chain starting with this one
size_t n_blocks = 0;
@ -227,8 +228,8 @@ STATIC void gc_mark_subtree(size_t block) {
// an unmarked head, mark it, and push it on gc stack
TRACE_MARK(childblock, ptr);
ATB_HEAD_TO_MARK(childblock);
if (MP_STATE_MEM(gc_sp) < &MP_STATE_MEM(gc_stack)[MICROPY_ALLOC_GC_STACK_SIZE]) {
*MP_STATE_MEM(gc_sp)++ = childblock;
if (sp < MICROPY_ALLOC_GC_STACK_SIZE) {
MP_STATE_MEM(gc_stack)[sp++] = childblock;
} else {
MP_STATE_MEM(gc_stack_overflow) = 1;
}
@ -237,19 +238,18 @@ STATIC void gc_mark_subtree(size_t block) {
}
// Are there any blocks on the stack?
if (MP_STATE_MEM(gc_sp) <= MP_STATE_MEM(gc_stack)) {
if (sp == 0) {
break; // No, stack is empty, we're done.
}
// pop the next block off the stack
block = *--MP_STATE_MEM(gc_sp);
block = MP_STATE_MEM(gc_stack)[--sp];
}
}
STATIC void gc_deal_with_stack_overflow(void) {
while (MP_STATE_MEM(gc_stack_overflow)) {
MP_STATE_MEM(gc_stack_overflow) = 0;
MP_STATE_MEM(gc_sp) = MP_STATE_MEM(gc_stack);
// scan entire memory looking for blocks which have been marked but not their children
for (size_t block = 0; block < MP_STATE_MEM(gc_alloc_table_byte_len) * BLOCKS_PER_ATB; block++) {
@ -323,7 +323,6 @@ void gc_collect_start(void) {
MP_STATE_MEM(gc_alloc_amount) = 0;
#endif
MP_STATE_MEM(gc_stack_overflow) = 0;
MP_STATE_MEM(gc_sp) = MP_STATE_MEM(gc_stack);
// Trace root pointers. This relies on the root pointers being organised
// correctly in the mp_state_ctx structure. We scan nlr_top, dict_locals,

View File

@ -78,7 +78,6 @@ typedef struct _mp_state_mem_t {
int gc_stack_overflow;
size_t gc_stack[MICROPY_ALLOC_GC_STACK_SIZE];
size_t *gc_sp;
uint16_t gc_lock_depth;
// This variable controls auto garbage collection. If set to 0 then the