summaryrefslogtreecommitdiffstats
path: root/modules/mono/managed_callable.cpp
diff options
context:
space:
mode:
authorIgnacio Etcheverry <ignalfonsore@gmail.com>2019-12-11 17:08:40 +0100
committerIgnacio Etcheverry <ignalfonsore@gmail.com>2020-03-17 21:51:05 +0100
commit0b814ea78d6b065e8e0785155207bd80b3d845c8 (patch)
tree6513402706d55505ec57cf88c97db5633ffabb9a /modules/mono/managed_callable.cpp
parent989a223c5a6d6ba5b1b098be8983888cb49b2525 (diff)
downloadredot-engine-0b814ea78d6b065e8e0785155207bd80b3d845c8.tar.gz
Mono/C#: Optimize the way we store GC handles for scripts
Don't store GC handles for C# script instances and instance bindings as 'Ref<MonoGCHandle>'; store the raw data instead. Initially this was not possible as we needed to store a Variant, but this had not been the case for a looong time yet the stored type was never updated.
Diffstat (limited to 'modules/mono/managed_callable.cpp')
-rw-r--r--modules/mono/managed_callable.cpp18
1 files changed, 10 insertions, 8 deletions
diff --git a/modules/mono/managed_callable.cpp b/modules/mono/managed_callable.cpp
index 75420612fc..a9cf64d1cc 100644
--- a/modules/mono/managed_callable.cpp
+++ b/modules/mono/managed_callable.cpp
@@ -44,8 +44,8 @@ bool ManagedCallable::compare_equal(const CallableCustom *p_a, const CallableCus
const ManagedCallable *a = static_cast<const ManagedCallable *>(p_a);
const ManagedCallable *b = static_cast<const ManagedCallable *>(p_b);
- MonoDelegate *delegate_a = (MonoDelegate *)a->delegate_handle->get_target();
- MonoDelegate *delegate_b = (MonoDelegate *)b->delegate_handle->get_target();
+ MonoDelegate *delegate_a = (MonoDelegate *)a->delegate_handle.get_target();
+ MonoDelegate *delegate_b = (MonoDelegate *)b->delegate_handle.get_target();
if (!delegate_a || !delegate_b) {
if (!delegate_a && !delegate_b)
@@ -66,7 +66,7 @@ bool ManagedCallable::compare_less(const CallableCustom *p_a, const CallableCust
uint32_t ManagedCallable::hash() const {
// hmm
uint32_t hash = delegate_invoke->get_name().hash();
- return hash_djb2_one_64(delegate_handle.ptr()->handle, hash);
+ return hash_djb2_one_64(delegate_handle.handle, hash);
}
String ManagedCallable::get_as_text() const {
@@ -92,12 +92,12 @@ void ManagedCallable::call(const Variant **p_arguments, int p_argcount, Variant
#ifdef GD_MONO_HOT_RELOAD
// Lost during hot-reload
ERR_FAIL_NULL(delegate_invoke);
- ERR_FAIL_COND(delegate_handle.is_null());
+ ERR_FAIL_COND(delegate_handle.is_released());
#endif
ERR_FAIL_COND(delegate_invoke->get_parameters_count() < p_argcount);
- MonoObject *delegate = delegate_handle->get_target();
+ MonoObject *delegate = delegate_handle.get_target();
MonoException *exc = NULL;
MonoObject *ret = delegate_invoke->invoke(delegate, p_arguments, &exc);
@@ -111,7 +111,7 @@ void ManagedCallable::call(const Variant **p_arguments, int p_argcount, Variant
}
void ManagedCallable::set_delegate(MonoDelegate *p_delegate) {
- delegate_handle = MonoGCHandle::create_strong((MonoObject *)p_delegate);
+ delegate_handle = MonoGCHandleData::new_strong_handle((MonoObject *)p_delegate);
MonoMethod *delegate_invoke_raw = mono_get_delegate_invoke(mono_object_get_class((MonoObject *)p_delegate));
const StringName &delegate_invoke_name = CSharpLanguage::get_singleton()->get_string_names().delegate_invoke_method_name;
delegate_invoke = memnew(GDMonoMethod(delegate_invoke_name, delegate_invoke_raw)); // TODO: Use pooling for this GDMonoMethod instances
@@ -132,12 +132,14 @@ ManagedCallable::ManagedCallable(MonoDelegate *p_delegate) {
#endif
}
-#ifdef GD_MONO_HOT_RELOAD
ManagedCallable::~ManagedCallable() {
+#ifdef GD_MONO_HOT_RELOAD
{
MutexLock lock(instances_mutex);
instances.remove(&self_instance);
instances_pending_reload.erase(this);
}
-}
#endif
+
+ delegate_handle.release();
+}