Skip to content

Commit ea7cf2b

Browse files
aykevldpgeorge
authored andcommitted
py/gc: Reduce code size by specialising VERIFY_MARK_AND_PUSH macro.
This macro is written out explicitly in the two locations that it is used and then the code is optimised, opening possibilities for further optimisations and reducing code size: unix: -48 minimal CROSS=1: -32 stm32: -32
1 parent a3e01d3 commit ea7cf2b

1 file changed

Lines changed: 25 additions & 21 deletions

File tree

py/gc.c

Lines changed: 25 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -203,24 +203,6 @@ bool gc_is_locked(void) {
203203
#endif
204204
#endif
205205

206-
// ptr should be of type void*
207-
#define VERIFY_MARK_AND_PUSH(ptr) \
208-
do { \
209-
if (VERIFY_PTR(ptr)) { \
210-
size_t _block = BLOCK_FROM_PTR(ptr); \
211-
if (ATB_GET_KIND(_block) == AT_HEAD) { \
212-
/* an unmarked head, mark it, and push it on gc stack */ \
213-
TRACE_MARK(_block, ptr); \
214-
ATB_HEAD_TO_MARK(_block); \
215-
if (MP_STATE_MEM(gc_sp) < &MP_STATE_MEM(gc_stack)[MICROPY_ALLOC_GC_STACK_SIZE]) { \
216-
*MP_STATE_MEM(gc_sp)++ = _block; \
217-
} else { \
218-
MP_STATE_MEM(gc_stack_overflow) = 1; \
219-
} \
220-
} \
221-
} \
222-
} while (0)
223-
224206
STATIC void gc_drain_stack(void) {
225207
while (MP_STATE_MEM(gc_sp) > MP_STATE_MEM(gc_stack)) {
226208
// pop the next block off the stack
@@ -236,7 +218,20 @@ STATIC void gc_drain_stack(void) {
236218
void **ptrs = (void**)PTR_FROM_BLOCK(block);
237219
for (size_t i = n_blocks * BYTES_PER_BLOCK / sizeof(void*); i > 0; i--, ptrs++) {
238220
void *ptr = *ptrs;
239-
VERIFY_MARK_AND_PUSH(ptr);
221+
if (VERIFY_PTR(ptr)) {
222+
// Mark and push this pointer
223+
size_t childblock = BLOCK_FROM_PTR(ptr);
224+
if (ATB_GET_KIND(childblock) == AT_HEAD) {
225+
// an unmarked head, mark it, and push it on gc stack
226+
TRACE_MARK(childblock, ptr);
227+
ATB_HEAD_TO_MARK(childblock);
228+
if (MP_STATE_MEM(gc_sp) < &MP_STATE_MEM(gc_stack)[MICROPY_ALLOC_GC_STACK_SIZE]) {
229+
*MP_STATE_MEM(gc_sp)++ = childblock;
230+
} else {
231+
MP_STATE_MEM(gc_stack_overflow) = 1;
232+
}
233+
}
234+
}
240235
}
241236
}
242237
}
@@ -337,8 +332,17 @@ void gc_collect_start(void) {
337332
void gc_collect_root(void **ptrs, size_t len) {
338333
for (size_t i = 0; i < len; i++) {
339334
void *ptr = ptrs[i];
340-
VERIFY_MARK_AND_PUSH(ptr);
341-
gc_drain_stack();
335+
if (VERIFY_PTR(ptr)) {
336+
// Mark and push this pointer
337+
size_t block = BLOCK_FROM_PTR(ptr);
338+
if (ATB_GET_KIND(block) == AT_HEAD) {
339+
// an unmarked head, mark it, and push it on gc stack
340+
TRACE_MARK(block, ptr);
341+
ATB_HEAD_TO_MARK(block);
342+
*MP_STATE_MEM(gc_sp)++ = block;
343+
gc_drain_stack();
344+
}
345+
}
342346
}
343347
}
344348

0 commit comments

Comments
 (0)