Skip to content

Commit e16e7ae

Browse files
committed
[mono] Cache delegate->method_ptr in MonoDelegateTrampInfo instead of in delegate->method_code.
Repurpose the method_code field to store the MonoDelegateTrampInfo pointer for future optimizations.
1 parent 60188fa commit e16e7ae

File tree

4 files changed

+8
-25
lines changed

4 files changed

+8
-25
lines changed

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

+2-5
Original file line numberDiff line numberDiff line change
@@ -836,11 +836,8 @@ struct _MonoDelegate {
836836
gpointer delegate_trampoline;
837837
/* Extra argument passed to the target method in llvmonly mode */
838838
gpointer extra_arg;
839-
/*
840-
* If non-NULL, this points to a memory location which stores the address of
841-
* the compiled code of the method, or NULL if it is not yet compiled.
842-
*/
843-
guint8 **method_code;
839+
/* MonoDelegateTrampInfo */
840+
gpointer invoke_info;
844841
gpointer interp_method;
845842
/* Interp method that is executed when invoking the delegate */
846843
gpointer interp_invoke_impl;

src/mono/mono/metadata/object-offsets.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ DECL_OFFSET(MonoDelegate, target)
8585
DECL_OFFSET(MonoDelegate, method_ptr)
8686
DECL_OFFSET(MonoDelegate, invoke_impl)
8787
DECL_OFFSET(MonoDelegate, method)
88-
DECL_OFFSET(MonoDelegate, method_code)
88+
DECL_OFFSET(MonoDelegate, invoke_info)
8989
DECL_OFFSET(MonoDelegate, method_is_virtual)
9090
DECL_OFFSET(MonoDelegate, bound)
9191
DECL_OFFSET(MonoDelegate, extra_arg)

src/mono/mono/mini/method-to-ir.c

+3-15
Original file line numberDiff line numberDiff line change
@@ -3692,6 +3692,9 @@ handle_delegate_ctor (MonoCompile *cfg, MonoClass *klass, MonoInst *target, Mono
36923692
return obj;
36933693
}
36943694

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

3707-
/*
3708-
* To avoid looking up the compiled code belonging to the target method
3709-
* in mono_delegate_trampoline (), we allocate a per-domain memory slot to
3710-
* store it, and we fill it after the method has been compiled.
3711-
*/
3712-
if (!method->dynamic) {
3713-
MonoInst *code_slot_ins;
3714-
3715-
if (target_method_context_used)
3716-
code_slot_ins = emit_get_rgctx_method (cfg, target_method_context_used, method, MONO_RGCTX_INFO_METHOD_DELEGATE_CODE);
3717-
else
3718-
code_slot_ins = mini_emit_runtime_constant (cfg, MONO_PATCH_INFO_METHOD_CODE_SLOT, method);
3719-
MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STORE_MEMBASE_REG, obj->dreg, MONO_STRUCT_OFFSET (MonoDelegate, method_code), code_slot_ins->dreg);
3720-
}
3721-
37223710
/* Set invoke_impl field */
37233711
dreg = alloc_preg (cfg);
37243712
MONO_EMIT_NEW_LOAD_MEMBASE (cfg, dreg, info_ins->dreg, MONO_STRUCT_OFFSET (MonoDelegateTrampInfo, invoke_impl));

src/mono/mono/mini/mini-trampolines.c

+2-4
Original file line numberDiff line numberDiff line change
@@ -1081,8 +1081,8 @@ mono_delegate_trampoline (host_mgreg_t *regs, guint8 *code, gpointer *arg, guint
10811081
*/
10821082
if (method && !callvirt) {
10831083
/* Avoid the overhead of looking up an already compiled method if possible */
1084-
if (enable_caching && delegate->method_code && *delegate->method_code) {
1085-
delegate->method_ptr = *delegate->method_code;
1084+
if (enable_caching && method == tramp_info->method && tramp_info->method_ptr) {
1085+
delegate->method_ptr = tramp_info->method_ptr;
10861086
} else {
10871087
compiled_method = addr = mono_jit_compile_method (method, error);
10881088
if (!is_ok (error)) {
@@ -1091,8 +1091,6 @@ mono_delegate_trampoline (host_mgreg_t *regs, guint8 *code, gpointer *arg, guint
10911091
}
10921092
addr = mini_add_method_trampoline (method, compiled_method, need_rgctx_tramp, need_unbox_tramp);
10931093
delegate->method_ptr = addr;
1094-
if (enable_caching && delegate->method_code)
1095-
*delegate->method_code = (guint8 *)delegate->method_ptr;
10961094
}
10971095
} else {
10981096
if (need_rgctx_tramp)

0 commit comments

Comments
 (0)