Skip to content

armgit / godot

Public Imported from GitHub
0

Godot Engine – Multi-platform 2D and 3D game engine

C#: Cache Array/Vector/String memory alignment information at startup

Andreia Gaita76922f886e76
diff --git a/modules/mono/editor/Godot.NET.Sdk/Godot.SourceGenerators/GodotPluginsInitializerGenerator.cs b/modules/mono/editor/Godot.NET.Sdk/Godot.SourceGenerators/GodotPluginsInitializerGenerator.cs
index 467313dc28d..2d49db74cb5 100644
--- a/modules/mono/editor/Godot.NET.Sdk/Godot.SourceGenerators/GodotPluginsInitializerGenerator.cs
+++ b/modules/mono/editor/Godot.NET.Sdk/Godot.SourceGenerators/GodotPluginsInitializerGenerator.cs
@@ -32,14 +32,16 @@ namespace GodotPlugins.Game
{
try
{
+ NativeFuncs.Initialize(unmanagedCallbacks, unmanagedCallbacksSize);
+
+ Marshaling.Initialize();
+
DllImportResolver dllImportResolver = new GodotDllImportResolver(godotDllHandle).OnResolveDllImport;
var coreApiAssembly = typeof(global::Godot.GodotObject).Assembly;
NativeLibrary.SetDllImportResolver(coreApiAssembly, dllImportResolver);
- NativeFuncs.Initialize(unmanagedCallbacks, unmanagedCallbacksSize);
-
ManagedCallbacks.Create(outManagedCallbacks);
ScriptManagerBridge.LookupScriptsInAssembly(typeof(global::GodotPlugins.Game.Main).Assembly);
diff --git a/modules/mono/glue/GodotSharp/GodotPlugins/Main.cs b/modules/mono/glue/GodotSharp/GodotPlugins/Main.cs
index 6117ae17eaa..b1fee8996ec 100644
--- a/modules/mono/glue/GodotSharp/GodotPlugins/Main.cs
+++ b/modules/mono/glue/GodotSharp/GodotPlugins/Main.cs
@@ -93,6 +93,9 @@ namespace GodotPlugins
{
try
{
+ NativeFuncs.Initialize(unmanagedCallbacks, unmanagedCallbacksSize);
+ Marshaling.Initialize();
+
_editorHint = editorHint.ToBool();
_dllImportResolver = new GodotDllImportResolver(godotDllHandle).OnResolveDllImport;
@@ -101,7 +104,6 @@ namespace GodotPlugins
NativeLibrary.SetDllImportResolver(CoreApiAssembly, _dllImportResolver);
AlcReloadCfg.Configure(alcReloadEnabled: _editorHint);
- NativeFuncs.Initialize(unmanagedCallbacks, unmanagedCallbacksSize);
if (_editorHint)
{
diff --git a/modules/mono/glue/GodotSharp/GodotSharp/Core/NativeInterop/InteropStructs.cs b/modules/mono/glue/GodotSharp/GodotSharp/Core/NativeInterop/InteropStructs.cs
index 3584ba65e6a..3e2a1aa9dee 100644
--- a/modules/mono/glue/GodotSharp/GodotSharp/Core/NativeInterop/InteropStructs.cs
+++ b/modules/mono/glue/GodotSharp/GodotSharp/Core/NativeInterop/InteropStructs.cs
@@ -538,7 +538,7 @@ namespace Godot.NativeInterop
public readonly unsafe int Size
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
- get => (int)NativeFuncs.godotsharp_string_size(in this);
+ get => _ptr != IntPtr.Zero ? (int)(*(ulong*)((byte*)_ptr - Marshaling.DataOffset + Marshaling.SizeOffset)) : 0;
}
}
@@ -764,6 +764,11 @@ namespace Godot.NativeInterop
// There are more fields here, but we don't care as we never store this in C#
+ public readonly int Size
+ {
+ [MethodImpl(MethodImplOptions.AggressiveInlining)]
+ get => _arrayVector.Size;
+ }
public readonly unsafe bool IsReadOnly
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
@@ -776,6 +781,11 @@ namespace Godot.NativeInterop
{
private IntPtr _writeProxy;
public unsafe godot_variant* _ptr;
+ public readonly unsafe int Size
+ {
+ [MethodImpl(MethodImplOptions.AggressiveInlining)]
+ get => _ptr != null ? (int)(*(ulong*)((byte*)_ptr - Marshaling.DataOffset + Marshaling.SizeOffset)) : 0;
+ }
}
public readonly unsafe godot_variant* Elements
@@ -793,7 +803,7 @@ namespace Godot.NativeInterop
public readonly unsafe int Size
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
- get => (int)NativeFuncs.godotsharp_array_size(in this);
+ get => _p != null ? _p->Size : 0;
}
public readonly unsafe bool IsReadOnly
@@ -924,7 +934,7 @@ namespace Godot.NativeInterop
public readonly unsafe int Size
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
- get => (int)NativeFuncs.godotsharp_packed_byte_array_size(in this);
+ get => _ptr != null ? (int)(*(ulong*)((byte*)_ptr - Marshaling.DataOffset + Marshaling.SizeOffset)) : 0;
}
}
@@ -955,7 +965,7 @@ namespace Godot.NativeInterop
public readonly unsafe int Size
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
- get => (int)NativeFuncs.godotsharp_packed_int32_array_size(in this);
+ get => _ptr != null ? (int)(*(ulong*)((byte*)_ptr - Marshaling.DataOffset + Marshaling.SizeOffset)) : 0;
}
}
@@ -986,7 +996,7 @@ namespace Godot.NativeInterop
public readonly unsafe int Size
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
- get => (int)NativeFuncs.godotsharp_packed_int64_array_size(in this);
+ get => _ptr != null ? (int)(*(ulong*)((byte*)_ptr - Marshaling.DataOffset + Marshaling.SizeOffset)) : 0;
}
}
@@ -1017,7 +1027,7 @@ namespace Godot.NativeInterop
public readonly unsafe int Size
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
- get => (int)NativeFuncs.godotsharp_packed_float32_array_size(in this);
+ get => _ptr != null ? (int)(*(ulong*)((byte*)_ptr - Marshaling.DataOffset + Marshaling.SizeOffset)) : 0;
}
}
@@ -1048,7 +1058,7 @@ namespace Godot.NativeInterop
public readonly unsafe int Size
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
- get => (int)NativeFuncs.godotsharp_packed_float64_array_size(in this);
+ get => _ptr != null ? (int)(*(ulong*)((byte*)_ptr - Marshaling.DataOffset + Marshaling.SizeOffset)) : 0;
}
}
@@ -1079,7 +1089,7 @@ namespace Godot.NativeInterop
public readonly unsafe int Size
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
- get => (int)NativeFuncs.godotsharp_packed_string_array_size(in this);
+ get => _ptr != null ? (int)(*(ulong*)((byte*)_ptr - Marshaling.DataOffset + Marshaling.SizeOffset)) : 0;
}
}
@@ -1110,7 +1120,7 @@ namespace Godot.NativeInterop
public readonly unsafe int Size
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
- get => (int)NativeFuncs.godotsharp_packed_vector2_array_size(in this);
+ get => _ptr != null ? (int)(*(ulong*)((byte*)_ptr - Marshaling.DataOffset + Marshaling.SizeOffset)) : 0;
}
}
@@ -1141,7 +1151,7 @@ namespace Godot.NativeInterop
public readonly unsafe int Size
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
- get => (int)NativeFuncs.godotsharp_packed_vector3_array_size(in this);
+ get => _ptr != null ? (int)(*(ulong*)((byte*)_ptr - Marshaling.DataOffset + Marshaling.SizeOffset)) : 0;
}
}
@@ -1173,7 +1183,7 @@ namespace Godot.NativeInterop
public readonly unsafe int Size
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
- get => (int)NativeFuncs.godotsharp_packed_vector4_array_size(in this);
+ get => _ptr != null ? (int)(*(ulong*)((byte*)_ptr - Marshaling.DataOffset + Marshaling.SizeOffset)) : 0;
}
}
@@ -1205,7 +1215,7 @@ namespace Godot.NativeInterop
public readonly unsafe int Size
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
- get => (int)NativeFuncs.godotsharp_packed_color_array_size(in this);
+ get => _ptr != null ? (int)(*(ulong*)((byte*)_ptr - Marshaling.DataOffset + Marshaling.SizeOffset)) : 0;
}
}
diff --git a/modules/mono/glue/GodotSharp/GodotSharp/Core/NativeInterop/Marshaling.cs b/modules/mono/glue/GodotSharp/GodotSharp/Core/NativeInterop/Marshaling.cs
index 69ea567c990..2ed455415c0 100644
--- a/modules/mono/glue/GodotSharp/GodotSharp/Core/NativeInterop/Marshaling.cs
+++ b/modules/mono/glue/GodotSharp/GodotSharp/Core/NativeInterop/Marshaling.cs
@@ -16,6 +16,20 @@ namespace Godot.NativeInterop
{
public static class Marshaling
{
+ public static ulong RefCountOffset { get; set; }
+ public static ulong CapacityOffset { get; set; } = RefCountOffset + (ulong)IntPtr.Size;
+ public static ulong SizeOffset { get; set; } = CapacityOffset + (ulong)IntPtr.Size;
+ public static ulong DataOffset { get; set; } = SizeOffset + (ulong)IntPtr.Size;
+
+ public static void Initialize()
+ {
+ NativeFuncs.godotsharp_initialize_marshaling_information(out ulong refCountOffset, out ulong capacityOffset, out ulong sizeOffset, out ulong dataOffset);
+ RefCountOffset = refCountOffset;
+ CapacityOffset = capacityOffset;
+ SizeOffset = sizeOffset;
+ DataOffset = dataOffset;
+ }
+
internal static Variant.Type ConvertManagedTypeToVariantType(Type type, out bool r_nil_is_variant)
{
r_nil_is_variant = false;
diff --git a/modules/mono/glue/GodotSharp/GodotSharp/Core/NativeInterop/NativeFuncs.cs b/modules/mono/glue/GodotSharp/GodotSharp/Core/NativeInterop/NativeFuncs.cs
index 48150535486..39abc4d70e3 100644
--- a/modules/mono/glue/GodotSharp/GodotSharp/Core/NativeInterop/NativeFuncs.cs
+++ b/modules/mono/glue/GodotSharp/GodotSharp/Core/NativeInterop/NativeFuncs.cs
@@ -41,6 +41,8 @@ namespace Godot.NativeInterop
internal static partial godot_bool godotsharp_dotnet_module_is_initialized();
+ internal static partial void godotsharp_initialize_marshaling_information(out ulong r_ref_count_offset, out ulong r_capacity_offset, out ulong r_size_offset, out ulong r_data_offset);
+
public static partial IntPtr godotsharp_method_bind_get_method(in godot_string_name p_classname,
in godot_string_name p_methodname);
@@ -606,31 +608,5 @@ namespace Godot.NativeInterop
// Object
public static partial void godotsharp_object_to_string(IntPtr ptr, out godot_string r_str);
-
- // Vector
-
- public static partial long godotsharp_string_size(in godot_string p_self);
-
- public static partial long godotsharp_packed_byte_array_size(in godot_packed_byte_array p_self);
-
- public static partial long godotsharp_packed_int32_array_size(in godot_packed_int32_array p_self);
-
- public static partial long godotsharp_packed_int64_array_size(in godot_packed_int64_array p_self);
-
- public static partial long godotsharp_packed_float32_array_size(in godot_packed_float32_array p_self);
-
- public static partial long godotsharp_packed_float64_array_size(in godot_packed_float64_array p_self);
-
- public static partial long godotsharp_packed_string_array_size(in godot_packed_string_array p_self);
-
- public static partial long godotsharp_packed_vector2_array_size(in godot_packed_vector2_array p_self);
-
- public static partial long godotsharp_packed_vector3_array_size(in godot_packed_vector3_array p_self);
-
- public static partial long godotsharp_packed_vector4_array_size(in godot_packed_vector4_array p_self);
-
- public static partial long godotsharp_packed_color_array_size(in godot_packed_color_array p_self);
-
- public static partial long godotsharp_array_size(in godot_array p_self);
}
}
diff --git a/modules/mono/glue/runtime_interop.cpp b/modules/mono/glue/runtime_interop.cpp
index 95d421593f6..a89b1b58288 100644
--- a/modules/mono/glue/runtime_interop.cpp
+++ b/modules/mono/glue/runtime_interop.cpp
@@ -1566,62 +1566,31 @@ void godotsharp_object_to_string(Object *p_ptr, godot_string *r_str) {
String("<" + p_ptr->get_class() + "#" + itos(p_ptr->get_instance_id()) + ">"));
}
-#ifdef __cplusplus
-}
-#endif
-
-int64_t godotsharp_string_size(const String *p_self) {
- return p_self->size();
-}
-
-int64_t godotsharp_packed_byte_array_size(const PackedByteArray *p_self) {
- return p_self->size();
-}
-
-int64_t godotsharp_packed_int32_array_size(const PackedInt32Array *p_self) {
- return p_self->size();
-}
-
-int64_t godotsharp_packed_int64_array_size(const PackedInt64Array *p_self) {
- return p_self->size();
-}
-
-int64_t godotsharp_packed_float32_array_size(const PackedFloat32Array *p_self) {
- return p_self->size();
-}
-
-int64_t godotsharp_packed_float64_array_size(const PackedFloat64Array *p_self) {
- return p_self->size();
-}
-
-int64_t godotsharp_packed_string_array_size(const PackedStringArray *p_self) {
- return p_self->size();
-}
-
-int64_t godotsharp_packed_vector2_array_size(const PackedVector2Array *p_self) {
- return p_self->size();
-}
-
-int64_t godotsharp_packed_vector3_array_size(const PackedVector3Array *p_self) {
- return p_self->size();
+void godotsharp_initialize_marshaling_information(size_t *r_ref_count_offset, size_t *r_capacity_offset, size_t *r_size_offset, size_t *r_data_offset) {
+ // Must be kept in sync with cowdata.h
+ // These are the definitions from cowdata.h that we must send out to managed land so that native interop struct (string/array/etc) alignment
+ // calculations match, since C# isn't aware of native alignments.
+ // For example, some platforms define max_align_t in such a way that alignof(max_align_t) and sizeof(max_align_t) returns 32, not 16.
+ static constexpr size_t ref_count_offset = 0;
+ static constexpr size_t capacity_offset = Memory::get_aligned_address(ref_count_offset + sizeof(SafeNumeric<uint64_t>), alignof(uint64_t));
+ static constexpr size_t size_offset = Memory::get_aligned_address(capacity_offset + sizeof(uint64_t), alignof(uint64_t));
+ static constexpr size_t data_offset = Memory::get_aligned_address(size_offset + sizeof(uint64_t), Memory::MAX_ALIGN);
+
+ *r_ref_count_offset = ref_count_offset;
+ *r_capacity_offset = capacity_offset;
+ *r_size_offset = size_offset;
+ *r_data_offset = data_offset;
}
-int64_t godotsharp_packed_vector4_array_size(const PackedVector4Array *p_self) {
- return p_self->size();
-}
-
-int64_t godotsharp_packed_color_array_size(const PackedColorArray *p_self) {
- return p_self->size();
-}
-
-int64_t godotsharp_array_size(const Array *p_self) {
- return p_self->size();
+#ifdef __cplusplus
}
+#endif
// The order in this array must match the declaration order of
// the methods in 'GodotSharp/Core/NativeInterop/NativeFuncs.cs'.
static const void *unmanaged_callbacks[]{
(void *)godotsharp_dotnet_module_is_initialized,
+ (void *)godotsharp_initialize_marshaling_information,
(void *)godotsharp_method_bind_get_method,
(void *)godotsharp_method_bind_get_method_with_compatibility,
(void *)godotsharp_get_class_constructor,
@@ -1846,18 +1815,6 @@ static const void *unmanaged_callbacks[]{
(void *)godotsharp_var_to_str,
(void *)godotsharp_err_print_error,
(void *)godotsharp_object_to_string,
- (void *)godotsharp_string_size,
- (void *)godotsharp_packed_byte_array_size,
- (void *)godotsharp_packed_int32_array_size,
- (void *)godotsharp_packed_int64_array_size,
- (void *)godotsharp_packed_float32_array_size,
- (void *)godotsharp_packed_float64_array_size,
- (void *)godotsharp_packed_string_array_size,
- (void *)godotsharp_packed_vector2_array_size,
- (void *)godotsharp_packed_vector3_array_size,
- (void *)godotsharp_packed_vector4_array_size,
- (void *)godotsharp_packed_color_array_size,
- (void *)godotsharp_array_size,
};
const void **godotsharp::get_runtime_interop_funcs(int32_t &r_size) {