Skip to content

Commit 736faef

Browse files
aykevldpgeorge
authored andcommitted
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
1 parent 5c9e561 commit 736faef

2 files changed

Lines changed: 5 additions & 7 deletions

File tree

py/gc.c

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,7 @@ bool gc_is_locked(void) {
209209
// topmost block on the stack and repeat with that one.
210210
STATIC void gc_mark_subtree(size_t block) {
211211
// Start with the block passed in the argument.
212+
size_t sp = 0;
212213
for (;;) {
213214
// work out number of consecutive blocks in the chain starting with this one
214215
size_t n_blocks = 0;
@@ -227,8 +228,8 @@ STATIC void gc_mark_subtree(size_t block) {
227228
// an unmarked head, mark it, and push it on gc stack
228229
TRACE_MARK(childblock, ptr);
229230
ATB_HEAD_TO_MARK(childblock);
230-
if (MP_STATE_MEM(gc_sp) < &MP_STATE_MEM(gc_stack)[MICROPY_ALLOC_GC_STACK_SIZE]) {
231-
*MP_STATE_MEM(gc_sp)++ = childblock;
231+
if (sp < MICROPY_ALLOC_GC_STACK_SIZE) {
232+
MP_STATE_MEM(gc_stack)[sp++] = childblock;
232233
} else {
233234
MP_STATE_MEM(gc_stack_overflow) = 1;
234235
}
@@ -237,19 +238,18 @@ STATIC void gc_mark_subtree(size_t block) {
237238
}
238239

239240
// Are there any blocks on the stack?
240-
if (MP_STATE_MEM(gc_sp) <= MP_STATE_MEM(gc_stack)) {
241+
if (sp == 0) {
241242
break; // No, stack is empty, we're done.
242243
}
243244

244245
// pop the next block off the stack
245-
block = *--MP_STATE_MEM(gc_sp);
246+
block = MP_STATE_MEM(gc_stack)[--sp];
246247
}
247248
}
248249

249250
STATIC void gc_deal_with_stack_overflow(void) {
250251
while (MP_STATE_MEM(gc_stack_overflow)) {
251252
MP_STATE_MEM(gc_stack_overflow) = 0;
252-
MP_STATE_MEM(gc_sp) = MP_STATE_MEM(gc_stack);
253253

254254
// scan entire memory looking for blocks which have been marked but not their children
255255
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) {
323323
MP_STATE_MEM(gc_alloc_amount) = 0;
324324
#endif
325325
MP_STATE_MEM(gc_stack_overflow) = 0;
326-
MP_STATE_MEM(gc_sp) = MP_STATE_MEM(gc_stack);
327326

328327
// Trace root pointers. This relies on the root pointers being organised
329328
// correctly in the mp_state_ctx structure. We scan nlr_top, dict_locals,

py/mpstate.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,6 @@ typedef struct _mp_state_mem_t {
7878

7979
int gc_stack_overflow;
8080
size_t gc_stack[MICROPY_ALLOC_GC_STACK_SIZE];
81-
size_t *gc_sp;
8281
uint16_t gc_lock_depth;
8382

8483
// This variable controls auto garbage collection. If set to 0 then the

0 commit comments

Comments
 (0)