Skip to content

Commit

Permalink
[mono] Reduce number of reallocs inside interp_create_var_explicit (#…
Browse files Browse the repository at this point in the history
…100801)

During startup we realloc the vars table a lot, so pre-reserve some spare space in it to minimize the number of reallocs
  • Loading branch information
kg authored Apr 11, 2024
1 parent 74639e2 commit 1794d65
Showing 1 changed file with 9 additions and 5 deletions.
14 changes: 9 additions & 5 deletions src/mono/mono/mini/interp/transform.c
Original file line number Diff line number Diff line change
Expand Up @@ -419,11 +419,12 @@ interp_create_var_explicit (TransformData *td, MonoType *type, int size)
if (td->vars_size == td->vars_capacity) {
td->vars_capacity *= 2;
if (td->vars_capacity == 0)
td->vars_capacity = 2;
td->vars_capacity = 16;
td->vars = (InterpVar*) g_realloc (td->vars, td->vars_capacity * sizeof (InterpVar));
}
int mt = mono_mint_type (type);
InterpVar *local = &td->vars [td->vars_size];
// FIXME: We don't need to do this memset unless we realloc'd, since we malloc0 vars initially
memset (local, 0, sizeof (InterpVar));

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

imethod->local_offsets = (guint32*)g_malloc (num_il_locals * sizeof(guint32));
td->vars = (InterpVar*)g_malloc0 (num_locals * sizeof (InterpVar));
td->vars = (InterpVar*)g_malloc0 (target_vars_capacity * sizeof (InterpVar));
td->vars_size = num_locals;
td->vars_capacity = td->vars_size;
td->vars_capacity = target_vars_capacity;

td->renamable_vars = (InterpRenamableVar*)g_malloc (num_locals * sizeof (InterpRenamableVar));
td->renamable_vars = (InterpRenamableVar*)g_malloc (target_vars_capacity * sizeof (InterpRenamableVar));
td->renamable_vars_size = 0;
td->renamable_vars_capacity = num_locals;
td->renamable_vars_capacity = target_vars_capacity;
offset = 0;

/*
Expand Down

0 comments on commit 1794d65

Please sign in to comment.