virtualx-engine/modules/mono/editor
Ignacio Roldán Etcheverry 3f645f980c C#: Optimize Variant conversion callbacks
These callbacks are used for marshaling by callables and generic Godot
collections.

C# generics don't support specialization the way C++ templates do.
I knew NativeAOT could optimize away many type checks when the types
are known at compile time, but I didn't trust the JIT would do as good
a job, so I initially went with cached function pointers.

Well, it turns out the JIT is also very good at optimizing in this
scenario, so I'm changing the methods to do the conversion directly,
rather than returning a function pointer for the conversion.

The methods were moved to `VariantUtils`, and were renamed from
`GetFromVariantCallback/GetToVariantCallback` to `ConvertTo/CreateFrom`.

The new implementation looks like it goes through many `if` checks
at runtime to find the right branch for the type, but in practice it
works pretty much like template specialization. The JIT only generates
code for the relevant branch. Together with inlining, the result is
very close or the same as doing the conversion manually:

```cs
godot_variant variant;

int foo = variant.Int;
int bar = VariantUtils.ConvertTo<int>(variant);
```

If the type is a generic Godot collection, the conversion still goes
through a function pointer call.

The new code happens to be much shorter as well, with the file going
from 1057 lines to 407.

Side note: `Variant.cs` was mistakenly created in the wrong folder,
so I moved it to the `Core` folder.
2022-11-25 03:14:10 +01:00
..
Godot.NET.Sdk Merge pull request #68580 from rhofour/fix-source-generation 2022-11-23 17:19:26 +01:00
GodotTools Add readonly to C# methods and types that don't mutate 2022-11-14 20:32:20 +01:00
script_templates C#: Assume 64-bit types when type has no meta 2022-09-01 10:29:52 +02:00
bindings_generator.cpp C#: Optimize Variant conversion callbacks 2022-11-25 03:14:10 +01:00
bindings_generator.h C#: Assume 64-bit types when type has no meta 2022-09-01 10:29:52 +02:00
code_completion.cpp Unify usage of GLOBAL/EDITOR_GET 2022-10-18 19:01:48 +02:00
code_completion.h Update copyright statements to 2022 2022-01-03 21:27:34 +01:00
editor_internal_calls.cpp Port remaining connections to callable_mp 2022-09-18 13:08:54 +02:00
editor_internal_calls.h C#: Replace P/Invoke with delegate pointers 2022-08-22 03:36:52 +02:00
hostfxr_resolver.cpp Fix MacOS compiler flags in .NET module 2022-09-08 23:29:01 +02:00
hostfxr_resolver.h C#: Replace libnethost dependency to find hostfxr 2022-09-07 16:36:36 +02:00
semver.cpp C#: Replace libnethost dependency to find hostfxr 2022-09-07 16:36:36 +02:00
semver.h C#: Replace libnethost dependency to find hostfxr 2022-09-07 16:36:36 +02:00