10000 py/gc: Speed up incremental GC cycles by tracking the high watermark. by damz · Pull Request #10235 · micropython/micropython · GitHub
[go: up one dir, main page]

Skip to content

py/gc: Speed up incremental GC cycles by tracking the high watermark. #10235

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 20 additions & 2 deletions py/gc.c
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,7 @@ STATIC void gc_setup_area(mp_state_mem_area_t *area, void *start, void *end) {
#endif

area->gc_last_free_atb_index = 0;
area->gc_last_used_block = 0;

#if MICROPY_GC_SPLIT_HEAP
area->next = NULL;
Expand Down Expand Up @@ -378,7 +379,14 @@ STATIC void gc_sweep(void) {
// free unmarked heads and their tails
int free_tail = 0;
for (mp_state_mem_area_t *area = &MP_STATE_MEM(area); area != NULL; area = NEXT_AREA(area)) {
for (size_t block = 0; block < area->gc_alloc_table_byte_len * BLOCKS_PER_ATB; block++) {
size_t end_block = area->gc_alloc_table_byte_len * BLOCKS_PER_ATB;
if (area->gc_last_used_block < end_block) {
end_block = area->gc_last_used_block + 1;
}

size_t last_used_block = 0;

for (size_t block = 0; block < end_block; block++) {
MICROPY_GC_HOOK_LOOP(block);
switch (ATB_GET_KIND(area, block)) {
case AT_HEAD:
Expand Down Expand Up @@ -418,15 +426,20 @@ STATIC void gc_sweep(void) {
#if CLEAR_ON_SWEEP
memset((void *)PTR_FROM_BLOCK(area, block), 0, BYTES_PER_BLOCK);
#endif
} else {
last_used_block = block;
}
break;

case AT_MARK:
ATB_MARK_TO_HEAD(area, block);
free_tail = 0;
last_used_block = block;
break;
}
}

area->gc_last_used_block = last_used_block;
}
}

Expand Down Expand Up @@ -680,6 +693,8 @@ void *gc_alloc(size_t n_bytes, unsigned int alloc_flags) {
area->gc_last_free_atb_index = (i + 1) / BLOCKS_PER_ATB;
}

area->gc_last_used_block = MAX(area->gc_last_used_block, end_block);

// mark first block as used head
ATB_FREE_TO_HEAD(area, start_block);

Expand Down Expand Up @@ -971,11 +986,14 @@ void *gc_realloc(void *ptr_in, size_t n_bytes, bool allow_move) {
// check if we can expand in place
if (new_blocks <= n_blocks + n_free) {
// mark few more blocks as used tail
for (size_t bl = block + n_blocks; bl < block + new_blocks; bl++) {
size_t end_block = block + new_blocks;
for (size_t bl = block + n_blocks; bl < end_block; bl++) {
assert(ATB_GET_KIND(area, bl) == AT_FREE);
ATB_FREE_TO_TAIL(area, bl);
}

area->gc_last_used_block = MAX(area->gc_last_used_block, end_block);

GC_EXIT();

#if MICROPY_GC_CONSERVATIVE_CLEAR
Expand Down
1 change: 1 addition & 0 deletions py/mpstate.h
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ typedef struct _mp_state_mem_area_t {
byte *gc_pool_end;

size_t gc_last_free_atb_index;
size_t gc_last_used_block; // The block ID of the highest block allocated in the area
} mp_state_mem_area_t;

// This structure hold information about the memory allocation system.
Expand Down
0