Skip to content

Commit b4c05c2

Browse files
committed
py: Move pointer to next free block earlier in alloc.
1 parent 788f191 commit b4c05c2

File tree

1 file changed

+12
-9
lines changed

1 file changed

+12
-9
lines changed

py/gc.c

+12-9
Original file line numberDiff line numberDiff line change
@@ -480,9 +480,20 @@ void *gc_alloc(mp_uint_t n_bytes, bool has_finaliser) {
480480
end_block = i;
481481
start_block = i - n_free + 1;
482482

483-
// mark first block as used head
484483
#if MICROPY_REENTRANT_GC
485484
mp_uint_t atomic_state = MICROPY_BEGIN_ATOMIC_SECTION();
485+
#endif
486+
487+
// Set last free ATB index to block after last block we found, for start of
488+
// next scan. To reduce fragmentation, we only do this if there are no free
489+
// blocks before this one. Also, whenever we free or shink a block we must
490+
// check if this index needs adjusting (see gc_realloc and gc_free).
491+
if ((i + 1) / BLOCKS_PER_ATB < gc_last_free_atb_index) {
492+
gc_last_free_atb_index = (i + 1) / BLOCKS_PER_ATB;
493+
}
494+
495+
// mark first block as used head
496+
#if MICROPY_REENTRANT_GC
486497
if (ATB_GET_KIND(start_block) == AT_FREE) {
487498
// TODO might need to make MARK
488499
ATB_FREE_TO_HEAD(start_block);
@@ -518,14 +529,6 @@ void *gc_alloc(mp_uint_t n_bytes, bool has_finaliser) {
518529
#endif
519530
}
520531

521-
// Set last free ATB index to block after last block we found, for start of
522-
// next scan. To reduce fragmentation, we only do this if there are no free
523-
// blocks before this one. Also, whenever we free or shink a block we must
524-
// check if this index needs adjusting (see gc_realloc and gc_free).
525-
if ((i + 1) / BLOCKS_PER_ATB < gc_last_free_atb_index) {
526-
gc_last_free_atb_index = (i + 1) / BLOCKS_PER_ATB;
527-
}
528-
529532
#if MICROPY_REENTRANT_GC
530533
MICROPY_END_ATOMIC_SECTION(atomic_state);
531534
#endif

0 commit comments

Comments
 (0)