Skip to content
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

sort GC memory blocks by number of free slots #99

Closed
wants to merge 1 commit into from
Closed
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
21 changes: 20 additions & 1 deletion src/gc.c
Original file line number Diff line number Diff line change
Expand Up @@ -1674,6 +1674,11 @@ void gc_free_pages(void)
}
}

static int gc_pg_cmp(jl_gc_pagemeta_t *pg, jl_gc_pagemeta_t *pg2)
{
return (pg->nfree - pg2->nfree);
}

// setup the data-structures for a sweep over all memory pools
static void gc_sweep_pool(void)
{
Expand Down Expand Up @@ -1746,25 +1751,39 @@ static void gc_sweep_pool(void)
}
}

// create a temporary array of pages to sort them
ws_queue_t queue;
memset(&queue, 0, sizeof(ws_queue_t));
queue.array = create_ws_array((1 << 10), sizeof(jl_gc_pagemeta_t*));
// merge free lists
for (int t_i = 0; t_i < n_threads; t_i++) {
jl_ptls_t ptls2 = gc_all_tls_states[t_i];
if (ptls2 == NULL) {
continue;
}
jl_atomic_store_relaxed(&queue.bottom, 0);
jl_gc_pagemeta_t *pg = jl_atomic_load_relaxed(&ptls2->page_metadata_allocd.bottom);
while (pg != NULL) {
ws_queue_push(&queue, &pg, sizeof(jl_gc_pagemeta_t*));
jl_gc_pagemeta_t *pg2 = pg->next;
pg = pg2;
}
// sort by number of free objects in ascending order
int64_t b = jl_atomic_load_relaxed(&queue.bottom);
qsort(queue.array->buffer, b, sizeof(jl_gc_pagemeta_t*), (int (*)(const void *, const void *))gc_pg_cmp);
for (int i = 0; i < b; i++) {
jl_gc_pagemeta_t *pg = ((jl_gc_pagemeta_t**)queue.array->buffer)[i];
if (pg->fl_begin_offset != UINT16_MAX) {
char *cur_pg = pg->data;
jl_taggedvalue_t *fl_beg = (jl_taggedvalue_t*)(cur_pg + pg->fl_begin_offset);
jl_taggedvalue_t *fl_end = (jl_taggedvalue_t*)(cur_pg + pg->fl_end_offset);
*pfl[t_i * JL_GC_N_POOLS + pg->pool_n] = fl_beg;
pfl[t_i * JL_GC_N_POOLS + pg->pool_n] = &fl_end->next;
}
pg = pg2;
}
}
free(queue.array->buffer);
free(queue.array);

// null out terminal pointers of free lists
for (int t_i = 0; t_i < n_threads; t_i++) {
Expand Down