Skip to content

Commit 1794d65

Browse files
authored
[mono] Reduce number of reallocs inside interp_create_var_explicit (#100801)
During startup we realloc the vars table a lot, so pre-reserve some spare space in it to minimize the number of reallocs
1 parent 74639e2 commit 1794d65

File tree

1 file changed

+9
-5
lines changed

1 file changed

+9
-5
lines changed

src/mono/mono/mini/interp/transform.c

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -419,11 +419,12 @@ interp_create_var_explicit (TransformData *td, MonoType *type, int size)
419419
if (td->vars_size == td->vars_capacity) {
420420
td->vars_capacity *= 2;
421421
if (td->vars_capacity == 0)
422-
td->vars_capacity = 2;
422+
td->vars_capacity = 16;
423423
td->vars = (InterpVar*) g_realloc (td->vars, td->vars_capacity * sizeof (InterpVar));
424424
}
425425
int mt = mono_mint_type (type);
426426
InterpVar *local = &td->vars [td->vars_size];
427+
// FIXME: We don't need to do this memset unless we realloc'd, since we malloc0 vars initially
427428
memset (local, 0, sizeof (InterpVar));
428429

429430
local->type = type;
@@ -4342,15 +4343,18 @@ interp_method_compute_offsets (TransformData *td, InterpMethod *imethod, MonoMet
43424343
int num_args = sig->hasthis + sig->param_count;
43434344
int num_il_locals = header->num_locals;
43444345
int num_locals = num_args + num_il_locals;
4346+
// HACK: Pre-reserve extra space to reduce the number of times we realloc during codegen, since it's expensive
4347+
// 64 vars * 72 bytes = 4608 bytes. Many methods need less than this
4348+
int target_vars_capacity = num_locals + 64;
43454349

43464350
imethod->local_offsets = (guint32*)g_malloc (num_il_locals * sizeof(guint32));
4347-
td->vars = (InterpVar*)g_malloc0 (num_locals * sizeof (InterpVar));
4351+
td->vars = (InterpVar*)g_malloc0 (target_vars_capacity * sizeof (InterpVar));
43484352
td->vars_size = num_locals;
4349-
td->vars_capacity = td->vars_size;
4353+
td->vars_capacity = target_vars_capacity;
43504354

4351-
td->renamable_vars = (InterpRenamableVar*)g_malloc (num_locals * sizeof (InterpRenamableVar));
4355+
td->renamable_vars = (InterpRenamableVar*)g_malloc (target_vars_capacity * sizeof (InterpRenamableVar));
43524356
td->renamable_vars_size = 0;
4353-
td->renamable_vars_capacity = num_locals;
4357+
td->renamable_vars_capacity = target_vars_capacity;
43544358
offset = 0;
43554359

43564360
/*

0 commit comments

Comments
 (0)