Skip to content

Commit be3ad0d

Browse files
authored
[mono] Optimize (typedef|table|declsec)_locator metadata loading (#100157)
* Optimize typedef_locator, table_locator and declsec_locator by hoisting initialization out * Cleanup duplicated code with different understandings of locator_t
1 parent 4e98e57 commit be3ad0d

File tree

4 files changed

+165
-157
lines changed

4 files changed

+165
-157
lines changed

src/mono/mono/component/hot_reload.c

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2776,14 +2776,6 @@ add_param_info_for_method (BaselineInfo *base_info, uint32_t param_token, uint32
27762776
}
27772777
}
27782778

2779-
/* HACK - keep in sync with locator_t in metadata/metadata.c */
2780-
typedef struct {
2781-
guint32 idx; /* The index that we are trying to locate */
2782-
guint32 col_idx; /* The index in the row where idx may be stored */
2783-
MonoTableInfo *t; /* pointer to the table */
2784-
guint32 result;
2785-
} upd_locator_t;
2786-
27872779
void*
27882780
hot_reload_metadata_linear_search (MonoImage *base_image, MonoTableInfo *base_table, const void *key, BinarySearchComparer comparer)
27892781
{
@@ -2804,11 +2796,10 @@ hot_reload_metadata_linear_search (MonoImage *base_image, MonoTableInfo *base_ta
28042796
g_assert (success);
28052797
uint32_t rows = table_info_get_rows (latest_mod_table);
28062798

2807-
upd_locator_t *loc = (upd_locator_t*)key;
2799+
mono_locator_t *loc = (mono_locator_t*)key;
28082800
g_assert (loc);
2809-
loc->result = 0;
28102801
/* HACK: this is so that the locator can compute the row index of the given row. but passing the mutant table to other metadata functions could backfire. */
2811-
loc->t = (MonoTableInfo*)latest_mod_table;
2802+
*loc = mono_locator_init ((MonoTableInfo*)latest_mod_table, loc->idx, loc->col_idx);
28122803
for (uint32_t idx = 0; idx < rows; ++idx) {
28132804
const char *row = latest_mod_table->base + idx * latest_mod_table->row_size;
28142805
if (!comparer (loc, row))

src/mono/mono/metadata/debug-mono-ppdb.c

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ enum {
6060
MONO_HAS_CUSTOM_DEBUG_MASK = 0x1f
6161
};
6262

63-
gboolean
63+
gboolean
6464
mono_get_pe_debug_info_full (MonoImage *image, guint8 *out_guid, gint32 *out_age, gint32 *out_timestamp, guint8 **ppdb_data,
6565
int *ppdb_uncompressed_size, int *ppdb_compressed_size, char **pdb_path, GArray *pdb_checksum_hash_type, GArray *pdb_checksum)
6666
{
@@ -761,20 +761,11 @@ mono_ppdb_lookup_locals (MonoDebugMethodInfo *minfo)
761761
return mono_ppdb_lookup_locals_internal (image, method_idx);
762762
}
763763

764-
/*
765-
* We use this to pass context information to the row locator
766-
*/
767-
typedef struct {
768-
guint32 idx; /* The index that we are trying to locate */
769-
guint32 col_idx; /* The index in the row where idx may be stored */
770-
MonoTableInfo *t; /* pointer to the table */
771-
guint32 result;
772-
} locator_t;
773-
764+
// FIXME: This duplicates table_locator from metadata.c
774765
static int
775766
table_locator (const void *a, const void *b)
776767
{
777-
locator_t *loc = (locator_t *)a;
768+
mono_locator_t *loc = (mono_locator_t *)a;
778769
const char *bb = (const char *)b;
779770
guint32 table_index = GPTRDIFF_TO_UINT32 ((bb - loc->t->base) / loc->t->row_size);
780771
guint32 col;
@@ -808,14 +799,16 @@ lookup_custom_debug_information (MonoImage* image, guint32 token, uint8_t parent
808799
{
809800
MonoTableInfo *tables = image->tables;
810801
MonoTableInfo *table = &tables[MONO_TABLE_CUSTOMDEBUGINFORMATION];
811-
locator_t loc;
802+
mono_locator_t loc;
812803

813804
if (!table->base)
814805
return 0;
815806

816-
loc.idx = (mono_metadata_token_index (token) << MONO_HAS_CUSTOM_DEBUG_BITS) | parent_type;
817-
loc.col_idx = MONO_CUSTOMDEBUGINFORMATION_PARENT;
818-
loc.t = table;
807+
loc = mono_locator_init (
808+
table,
809+
(mono_metadata_token_index (token) << MONO_HAS_CUSTOM_DEBUG_BITS) | parent_type,
810+
MONO_CUSTOMDEBUGINFORMATION_PARENT
811+
);
819812

820813
if (!mono_binary_search (&loc, table->base, table_info_get_rows (table), table->row_size, table_locator))
821814
return NULL;

src/mono/mono/metadata/metadata-internals.h

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1134,6 +1134,56 @@ mono_metadata_get_class_guid (MonoClass* klass, uint8_t* guid, MonoError *error)
11341134

11351135
#define MONO_CLASS_IS_INTERFACE_INTERNAL(c) ((mono_class_get_flags (c) & TYPE_ATTRIBUTE_INTERFACE) || mono_type_is_generic_parameter (m_class_get_byval_arg (c)))
11361136

1137+
/*
1138+
* We use this to pass context information to the row locator
1139+
*/
1140+
typedef struct {
1141+
// caller inputs
1142+
// note: we can't optimize around locator_t.idx yet because a few call sites mutate it
1143+
guint32 idx; /* The index that we are trying to locate */
1144+
// no call sites mutate this so we can optimize around it
1145+
guint32 col_idx; /* The index in the row where idx may be stored */
1146+
// no call sites mutate this so we can optimize around it
1147+
MonoTableInfo *t; /* pointer to the table */
1148+
1149+
// optimization data
1150+
gint32 metadata_has_updates; // -1: uninitialized. 0/1: value
1151+
const char * t_base;
1152+
guint t_row_size;
1153+
guint32 t_rows;
1154+
guint32 column_size;
1155+
const char * first_column_data;
1156+
1157+
// result
1158+
guint32 result;
1159+
} mono_locator_t;
1160+
1161+
MONO_ALWAYS_INLINE static mono_locator_t
1162+
mono_locator_init (MonoTableInfo *t, guint32 idx, guint32 col_idx)
1163+
{
1164+
mono_locator_t result = { 0, };
1165+
1166+
result.idx = idx;
1167+
result.col_idx = col_idx;
1168+
result.t = t;
1169+
1170+
g_assert (t);
1171+
// FIXME: Callers shouldn't rely on this
1172+
if (!t->base)
1173+
return result;
1174+
1175+
// optimization data for decode_locator_row
1176+
result.metadata_has_updates = -1;
1177+
result.t_base = t->base;
1178+
result.t_row_size = t->row_size;
1179+
result.t_rows = table_info_get_rows (t);
1180+
g_assert (col_idx < mono_metadata_table_count (t->size_bitfield));
1181+
result.column_size = mono_metadata_table_size (t->size_bitfield, col_idx);
1182+
result.first_column_data = result.t_base + t->column_offsets [col_idx];
1183+
1184+
return result;
1185+
}
1186+
11371187
static inline gboolean
11381188
m_image_is_raw_data_allocated (MonoImage *image)
11391189
{

0 commit comments

Comments
 (0)