Skip to content

Commit 0513228

Browse files
authored
staticdata: gc the global_roots_table contents during saving (#52088)
A (very) small memory optimization to avoid saving objects that are essentially unreachable
1 parent d88d5cd commit 0513228

File tree

1 file changed

+15
-4
lines changed

1 file changed

+15
-4
lines changed

src/staticdata.c

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2594,6 +2594,7 @@ static void jl_save_system_image_to_stream(ios_t *f, jl_array_t *mod_array,
25942594
jl_docmeta_sym = (jl_sym_t*)jl_get_global((jl_module_t*)docs, jl_symbol("META"));
25952595
}
25962596
}
2597+
jl_genericmemory_t *global_roots_table = NULL;
25972598

25982599
{ // step 1: record values (recursively) that need to go in the image
25992600
size_t i;
@@ -2602,7 +2603,6 @@ static void jl_save_system_image_to_stream(ios_t *f, jl_array_t *mod_array,
26022603
jl_value_t *tag = *tags[i];
26032604
jl_queue_for_serialization(&s, tag);
26042605
}
2605-
jl_queue_for_serialization(&s, jl_global_roots_table);
26062606
jl_queue_for_serialization(&s, s.ptls->root_task->tls);
26072607
}
26082608
else {
@@ -2638,8 +2638,19 @@ static void jl_save_system_image_to_stream(ios_t *f, jl_array_t *mod_array,
26382638
record_gvars(&s, &gvars);
26392639
record_external_fns(&s, &external_fns);
26402640
jl_serialize_reachable(&s);
2641-
// step 1.3: prune (garbage collect) some special weak references from
2642-
// built-in type caches
2641+
// step 1.3: prune (garbage collect) special weak references from the jl_global_roots_table
2642+
if (worklist == NULL) {
2643+
global_roots_table = jl_alloc_memory_any(0);
2644+
for (size_t i = 0; i < jl_global_roots_table->length; i += 2) {
2645+
jl_value_t *val = jl_genericmemory_ptr_ref(jl_global_roots_table, i);
2646+
if (ptrhash_get(&serialization_order, val) != HT_NOTFOUND)
2647+
global_roots_table = jl_eqtable_put(global_roots_table, val, jl_nothing, NULL);
2648+
}
2649+
jl_queue_for_serialization(&s, global_roots_table);
2650+
jl_serialize_reachable(&s);
2651+
}
2652+
// step 1.4: prune (garbage collect) some special weak references from
2653+
// built-in type caches too
26432654
for (i = 0; i < serialization_queue.len; i++) {
26442655
jl_typename_t *tn = (jl_typename_t*)serialization_queue.items[i];
26452656
if (jl_is_typename(tn)) {
@@ -2747,7 +2758,7 @@ static void jl_save_system_image_to_stream(ios_t *f, jl_array_t *mod_array,
27472758
jl_value_t *tag = *tags[i];
27482759
jl_write_value(&s, tag);
27492760
}
2750-
jl_write_value(&s, jl_global_roots_table);
2761+
jl_write_value(&s, global_roots_table);
27512762
jl_write_value(&s, s.ptls->root_task->tls);
27522763
write_uint32(f, jl_get_gs_ctr());
27532764
write_uint(f, jl_atomic_load_acquire(&jl_world_counter));

0 commit comments

Comments
 (0)