Skip to content

Commit

Permalink
[mono][aot] Optimize the layout of the extra_method_table. (#80682)
Browse files Browse the repository at this point in the history
  • Loading branch information
vargaz authored Jan 18, 2023
1 parent efc07a0 commit 6e773f2
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 27 deletions.
51 changes: 38 additions & 13 deletions src/mono/mono/mini/aot-compiler.c
Original file line number Diff line number Diff line change
Expand Up @@ -10957,6 +10957,17 @@ typedef struct HashEntry {
struct HashEntry *next;
} HashEntry;

static inline void
encode_uint_len (guint32 val, int len, guint8 *buf, guint8 **endbuf)
{
if (len == 2) {
g_assert (val < 65536);
encode_int16 (val, buf, endbuf);
} else {
encode_int ((gint32)val, buf, endbuf);
}
}

/*
* emit_extra_methods:
*
Expand All @@ -10972,6 +10983,7 @@ emit_extra_methods (MonoAotCompile *acfg)
GPtrArray *table;
HashEntry *entry, *new_entry;
int nmethods, max_chain_length;
guint32 max_method_index;
int *chain_lengths;

info_offsets = g_new0 (guint32, acfg->extra_methods->len);
Expand Down Expand Up @@ -11010,6 +11022,7 @@ emit_extra_methods (MonoAotCompile *acfg)
g_ptr_array_add (table, NULL);
chain_lengths = g_new0 (int, table_size);
max_chain_length = 0;
max_method_index = 0;
for (guint i = 0; i < acfg->extra_methods->len; ++i) {
MonoMethod *method = (MonoMethod *)g_ptr_array_index (acfg->extra_methods, i);
MonoCompile *cfg = (MonoCompile *)g_hash_table_lookup (acfg->method_to_cfg, method);
Expand All @@ -11026,6 +11039,7 @@ emit_extra_methods (MonoAotCompile *acfg)

chain_lengths [hash] ++;
max_chain_length = MAX (max_chain_length, chain_lengths [hash]);
max_method_index = MAX (max_method_index, value);

new_entry = (HashEntry *)mono_mempool_alloc0 (acfg->mempool, sizeof (HashEntry));
new_entry->key = key;
Expand All @@ -11048,25 +11062,36 @@ emit_extra_methods (MonoAotCompile *acfg)

//printf ("MAX: %d\n", max_chain_length);

buf_size = table->len * 12 + 4;
int key_len = 4;
int value_len = max_method_index < 65536 ? 2 : 4;
int next_len = table->len < 65536 ? 2 : 4;

buf_size = table->len * 12 + 20;
p = buf = (guint8 *)g_malloc (buf_size);
/* Hash size */
encode_int (table_size, p, &p);

/* Number of rows */
encode_int (table->len, p, &p);
/* Column sizes */
encode_int (key_len, p, &p);
encode_int (value_len, p, &p);
encode_int (next_len, p, &p);
/* Data */
for (guint i = 0; i < table->len; ++i) {
entry = (HashEntry *)g_ptr_array_index (table, i);
if (entry == NULL) {
encode_int (0, p, &p);
encode_int (0, p, &p);
encode_int (0, p, &p);
} else {
//g_assert (entry->key > 0);
encode_int (entry->key, p, &p);
encode_int (entry->value, p, &p);

int key = 0;
int value = 0;
int next = 0;
if (entry != NULL) {
key = entry->key;
value = entry->value;
if (entry->next)
encode_int (entry->next->index, p, &p);
else
encode_int (0, p, &p);
next = entry->next->index;
}
encode_uint_len (key, key_len, p, &p);
encode_uint_len (value, value_len, p, &p);
encode_uint_len (next, next_len, p, &p);
}
g_assert (p - buf <= buf_size);

Expand Down
49 changes: 35 additions & 14 deletions src/mono/mono/mini/aot-runtime.c
Original file line number Diff line number Diff line change
Expand Up @@ -372,6 +372,21 @@ decode_value (guint8 *ptr, guint8 **rptr)
return len;
}

static guint32
decode_uint_with_len (int len, guint8 *p)
{
int value = 0;

if (len == 2) {
value = (guint32)p [0] + ((guint32)p [1] << 8);
} else if (len == 4) {
value = (guint32)p [0] + ((guint32)p [1] << 8) + ((guint32)p [2] << 16) + ((guint32)p [3] << 24);
} else {
g_assert_not_reached ();
}
return value;
}

/*
* mono_aot_get_offset:
*
Expand Down Expand Up @@ -4352,9 +4367,8 @@ static guint32
find_aot_method_in_amodule (MonoAotModule *code_amodule, MonoMethod *method, guint32 hash_full)
{
ERROR_DECL (error);
guint32 table_size, entry_size, hash;
guint32 *table, *entry;
guint32 index;
guint32 hash, index;
guint32 *table;
// static guint32 n_extra_decodes; // used for debugging

// The AOT module containing the MonoMethod
Expand All @@ -4366,24 +4380,31 @@ find_aot_method_in_amodule (MonoAotModule *code_amodule, MonoMethod *method, gui
if (!metadata_amodule || metadata_amodule->out_of_date || !code_amodule || code_amodule->out_of_date)
return 0xffffff;

table_size = code_amodule->extra_method_table [0];
hash = hash_full % table_size;
table = code_amodule->extra_method_table + 1;
entry_size = 3;
table = code_amodule->extra_method_table;
guint32 hash_table_size = table [0];
int key_len = table [2];
int value_len = table [3];
int next_len = table [4];
int entry_size = key_len + value_len + next_len;

entry = &table [hash * entry_size];
hash = hash_full % hash_table_size;

if (entry [0] == 0)
return 0xffffff;
guint8 *data = (guint8*)(table + 5);
guint8 *entry = data + (hash * entry_size);
guint32 key, value, next;

index = 0xffffff;
while (TRUE) {
guint32 key = entry [0];
guint32 value = entry [1];
guint32 next = entry [entry_size - 1];
MonoMethod *m;
guint8 *p, *orig_p;

key = decode_uint_with_len (key_len, entry);
value = decode_uint_with_len (value_len, entry + key_len);
next = decode_uint_with_len (next_len, entry + key_len + next_len);

if (key == 0)
return 0xffffff;

p = code_amodule->blob + key;
orig_p = p;

Expand Down Expand Up @@ -4417,7 +4438,7 @@ find_aot_method_in_amodule (MonoAotModule *code_amodule, MonoMethod *method, gui
}*/

if (next != 0)
entry = &table [next * entry_size];
entry = data + (next * entry_size);
else
break;
}
Expand Down

0 comments on commit 6e773f2

Please sign in to comment.