Skip to content

Commit

Permalink
[mono] Cache delegate->method_ptr in MonoDelegateTrampInfo instead of…
Browse files Browse the repository at this point in the history
… in delegate->method_code.

Repurpose the method_code field to store the MonoDelegateTrampInfo pointer
for future optimizations.
  • Loading branch information
vargaz committed Mar 21, 2023
1 parent 60188fa commit e16e7ae
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 25 deletions.
7 changes: 2 additions & 5 deletions src/mono/mono/metadata/object-internals.h
Original file line number Diff line number Diff line change
Expand Up @@ -836,11 +836,8 @@ struct _MonoDelegate {
gpointer delegate_trampoline;
/* Extra argument passed to the target method in llvmonly mode */
gpointer extra_arg;
/*
* If non-NULL, this points to a memory location which stores the address of
* the compiled code of the method, or NULL if it is not yet compiled.
*/
guint8 **method_code;
/* MonoDelegateTrampInfo */
gpointer invoke_info;
gpointer interp_method;
/* Interp method that is executed when invoking the delegate */
gpointer interp_invoke_impl;
Expand Down
2 changes: 1 addition & 1 deletion src/mono/mono/metadata/object-offsets.h
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ DECL_OFFSET(MonoDelegate, target)
DECL_OFFSET(MonoDelegate, method_ptr)
DECL_OFFSET(MonoDelegate, invoke_impl)
DECL_OFFSET(MonoDelegate, method)
DECL_OFFSET(MonoDelegate, method_code)
DECL_OFFSET(MonoDelegate, invoke_info)
DECL_OFFSET(MonoDelegate, method_is_virtual)
DECL_OFFSET(MonoDelegate, bound)
DECL_OFFSET(MonoDelegate, extra_arg)
Expand Down
18 changes: 3 additions & 15 deletions src/mono/mono/mini/method-to-ir.c
Original file line number Diff line number Diff line change
Expand Up @@ -3692,6 +3692,9 @@ handle_delegate_ctor (MonoCompile *cfg, MonoClass *klass, MonoInst *target, Mono
return obj;
}

/* Set invoke_info field */
MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STORE_MEMBASE_REG, obj->dreg, MONO_STRUCT_OFFSET (MonoDelegate, invoke_info), info_ins->dreg);

/* Set method field */
if (target_method_context_used || invoke_context_used) {
// We copy from the delegate trampoline info as it's faster than a rgctx fetch
Expand All @@ -3704,21 +3707,6 @@ handle_delegate_ctor (MonoCompile *cfg, MonoClass *klass, MonoInst *target, Mono
MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STORE_MEMBASE_REG, obj->dreg, MONO_STRUCT_OFFSET (MonoDelegate, method), method_ins->dreg);
}

/*
* To avoid looking up the compiled code belonging to the target method
* in mono_delegate_trampoline (), we allocate a per-domain memory slot to
* store it, and we fill it after the method has been compiled.
*/
if (!method->dynamic) {
MonoInst *code_slot_ins;

if (target_method_context_used)
code_slot_ins = emit_get_rgctx_method (cfg, target_method_context_used, method, MONO_RGCTX_INFO_METHOD_DELEGATE_CODE);
else
code_slot_ins = mini_emit_runtime_constant (cfg, MONO_PATCH_INFO_METHOD_CODE_SLOT, method);
MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STORE_MEMBASE_REG, obj->dreg, MONO_STRUCT_OFFSET (MonoDelegate, method_code), code_slot_ins->dreg);
}

/* Set invoke_impl field */
dreg = alloc_preg (cfg);
MONO_EMIT_NEW_LOAD_MEMBASE (cfg, dreg, info_ins->dreg, MONO_STRUCT_OFFSET (MonoDelegateTrampInfo, invoke_impl));
Expand Down
6 changes: 2 additions & 4 deletions src/mono/mono/mini/mini-trampolines.c
Original file line number Diff line number Diff line change
Expand Up @@ -1081,8 +1081,8 @@ mono_delegate_trampoline (host_mgreg_t *regs, guint8 *code, gpointer *arg, guint
*/
if (method && !callvirt) {
/* Avoid the overhead of looking up an already compiled method if possible */
if (enable_caching && delegate->method_code && *delegate->method_code) {
delegate->method_ptr = *delegate->method_code;
if (enable_caching && method == tramp_info->method && tramp_info->method_ptr) {
delegate->method_ptr = tramp_info->method_ptr;
} else {
compiled_method = addr = mono_jit_compile_method (method, error);
if (!is_ok (error)) {
Expand All @@ -1091,8 +1091,6 @@ mono_delegate_trampoline (host_mgreg_t *regs, guint8 *code, gpointer *arg, guint
}
addr = mini_add_method_trampoline (method, compiled_method, need_rgctx_tramp, need_unbox_tramp);
delegate->method_ptr = addr;
if (enable_caching && delegate->method_code)
*delegate->method_code = (guint8 *)delegate->method_ptr;
}
} else {
if (need_rgctx_tramp)
Expand Down

0 comments on commit e16e7ae

Please sign in to comment.