Skip to content

Commit

Permalink
Merge pull request #23 from BeanCheeseBurrito/v4-world-local-type-ids
Browse files Browse the repository at this point in the history
Implement world-local component id resolution and complete rewrite of type registration code
  • Loading branch information
BeanCheeseBurrito authored Apr 20, 2024
2 parents 93986a2 + a10cd59 commit 9b3731c
Show file tree
Hide file tree
Showing 59 changed files with 3,947 additions and 1,654 deletions.
11 changes: 6 additions & 5 deletions src/Flecs.NET.Benchmarks/Core/AddRemoveTypeTag.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@

namespace Flecs.NET.Benchmarks.Core
{
public unsafe class AddRemoveTypeTag
public unsafe class AddRemoveTag
{
[Params(100000)] public int EntityCount;
[Params(100000)]
public int EntityCount;

public World World;
public Entity[] Entities;
Expand All @@ -22,7 +23,7 @@ public void Setup()
Entities = new Entity[EntityCount];

World.Component<Tag>();
TagId = Type<Tag>.RawId;
TagId = World.Component<Tag>();

for (int i = 0; i < EntityCount; i++)
Entities[i] = World.Entity();
Expand All @@ -35,7 +36,7 @@ public void Cleanup()
}

[Benchmark]
public void TypeTag()
public void AddRemoveTypeTag()
{
for (int e = 0; e < EntityCount; e++)
Entities[e].Add<Tag>();
Expand All @@ -45,7 +46,7 @@ public void TypeTag()
}

[Benchmark]
public void RawTag()
public void AddRemoveIntegerTag()
{
for (int e = 0; e < EntityCount; e++)
ecs_add_id(World, Entities[e], TagId);
Expand Down
4 changes: 2 additions & 2 deletions src/Flecs.NET.Benchmarks/Flecs.NET.Benchmarks.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFrameworks>netcoreapp3.1;net6.0;net7.0</TargetFrameworks>
<TargetFrameworks>netcoreapp3.1;net6.0;net7.0;net8.0</TargetFrameworks>
<Nullable>enable</Nullable>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<NoWarn>$(NoWarn);CS8618</NoWarn>
Expand All @@ -11,7 +11,7 @@
<Import Project="../Flecs.NET.Native/Flecs.NET.Native.targets"/>

<ItemGroup>
<PackageReference Include="BenchmarkDotNet" Version="0.13.7"/>
<PackageReference Include="BenchmarkDotNet" Version="0.13.12"/>
<ProjectReference Include="../Flecs.NET/Flecs.NET.csproj"/>
</ItemGroup>

Expand Down
2 changes: 1 addition & 1 deletion src/Flecs.NET.Benchmarks/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
using BenchmarkDotNet.Jobs;
using BenchmarkDotNet.Running;

[assembly: SimpleJob(runtimeMoniker: RuntimeMoniker.Net70, launchCount: 1, warmupCount: 5, iterationCount: 10)]
// [assembly: SimpleJob(runtimeMoniker: RuntimeMoniker.Net70, launchCount: 1, warmupCount: 5, iterationCount: 10)]

namespace Flecs.NET.Benchmarks
{
Expand Down
2 changes: 1 addition & 1 deletion src/Flecs.NET.Bindgen/Flecs.NET.Bindgen.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Bindgen.NET" Version="0.1.10"/>
<PackageReference Include="Bindgen.NET" Version="0.1.11"/>
</ItemGroup>

</Project>
56 changes: 32 additions & 24 deletions src/Flecs.NET.Bindings/Flecs.g.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13127,7 +13127,9 @@ public partial class BindgenInternal
{
public static readonly System.Collections.Generic.List<string> DllFilePaths;

public static System.IntPtr _libraryHandle = System.IntPtr.Zero;
public static System.IntPtr LibraryHandle = System.IntPtr.Zero;

public static readonly object Lock = new object ();

public static bool IsLinux => System.Runtime.InteropServices.RuntimeInformation.IsOSPlatform(System.Runtime.InteropServices.OSPlatform.Linux);

Expand Down Expand Up @@ -13184,12 +13186,12 @@ public static bool TryLoad(string path, out System.IntPtr handle)
public static System.IntPtr GetExport(string symbol)
{
#if NET5_0_OR_GREATER
return System.Runtime.InteropServices.NativeLibrary.GetExport(_libraryHandle, symbol);
return System.Runtime.InteropServices.NativeLibrary.GetExport(LibraryHandle, symbol);
#else
if (IsLinux)
{
GetLastErrorLinux();
System.IntPtr handle = GetExportLinux(_libraryHandle, symbol);
System.IntPtr handle = GetExportLinux(LibraryHandle, symbol);
if (handle != System.IntPtr.Zero)
return handle;
byte* errorResult = GetLastErrorLinux();
Expand All @@ -13202,7 +13204,7 @@ public static System.IntPtr GetExport(string symbol)
if (IsOsx)
{
GetLastErrorOsx();
System.IntPtr handle = GetExportOsx(_libraryHandle, symbol);
System.IntPtr handle = GetExportOsx(LibraryHandle, symbol);
if (handle != System.IntPtr.Zero)
return handle;
byte* errorResult = GetLastErrorOsx();
Expand All @@ -13214,7 +13216,7 @@ public static System.IntPtr GetExport(string symbol)

if (IsWindows)
{
System.IntPtr handle = GetExportWindows(_libraryHandle, symbol);
System.IntPtr handle = GetExportWindows(LibraryHandle, symbol);
if (handle != System.IntPtr.Zero)
return handle;
int errorCode = GetLastErrorWindows();
Expand All @@ -13237,45 +13239,51 @@ public static void ResolveLibrary()
fileExtension = ".dll";
else
throw new System.InvalidOperationException("Can't determine native library file extension for the current system.");
System.IntPtr handle = default;
foreach (string dllFilePath in DllFilePaths)
{
string fileName = System.IO.Path.GetFileName(dllFilePath);
string parentDir = $"{dllFilePath}/..";
string searchDir = System.IO.Path.IsPathRooted(dllFilePath) ? System.IO.Path.GetFullPath(parentDir) + "/" : System.IO.Path.GetFullPath(System.AppDomain.CurrentDomain.BaseDirectory + parentDir) + "/";
if (TryLoad($"{searchDir}{fileName}", out _libraryHandle))
return;
if (TryLoad($"{searchDir}{fileName}{fileExtension}", out _libraryHandle))
return;
if (TryLoad($"{searchDir}lib{fileName}", out _libraryHandle))
return;
if (TryLoad($"{searchDir}lib{fileName}{fileExtension}", out _libraryHandle))
return;
if (TryLoad($"{searchDir}{fileName}", out handle))
goto Return;
if (TryLoad($"{searchDir}{fileName}{fileExtension}", out handle))
goto Return;
if (TryLoad($"{searchDir}lib{fileName}", out handle))
goto Return;
if (TryLoad($"{searchDir}lib{fileName}{fileExtension}", out handle))
goto Return;
if (!fileName.StartsWith("lib") || fileName == "lib")
continue;
string unprefixed = fileName.Substring(4);
if (TryLoad($"{searchDir}{unprefixed}", out _libraryHandle))
return;
if (TryLoad($"{searchDir}{unprefixed}{fileExtension}", out _libraryHandle))
return;
if (TryLoad($"{searchDir}{unprefixed}", out handle))
goto Return;
if (TryLoad($"{searchDir}{unprefixed}{fileExtension}", out handle))
goto Return;
}

#if NET7_0_OR_GREATER
_libraryHandle = System.Runtime.InteropServices.NativeLibrary.GetMainProgramHandle();
handle = System.Runtime.InteropServices.NativeLibrary.GetMainProgramHandle();
#else
if (IsLinux)
_libraryHandle = LoadLibraryLinux(null, 0x101);
handle = LoadLibraryLinux(null, 0x101);
else if (IsOsx)
_libraryHandle = LoadLibraryOsx(null, 0x101);
handle = LoadLibraryOsx(null, 0x101);
else if (IsWindows)
_libraryHandle = GetModuleHandle(null);
handle = GetModuleHandle(null);
#endif
Return:
LibraryHandle = handle;
}

public static void* LoadDllSymbol(string variableSymbol, out void* field)
{
if (_libraryHandle == System.IntPtr.Zero)
ResolveLibrary();
return field = (void*)GetExport(variableSymbol);
lock (Lock)
{
if (LibraryHandle == System.IntPtr.Zero)
ResolveLibrary();
return field = (void*)GetExport(variableSymbol);
}
}
}
}
Expand Down
27 changes: 27 additions & 0 deletions src/Flecs.NET.Bindings/FlecsExtensions.g.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;

[assembly: InternalsVisibleTo("Flecs.NET")]

namespace Flecs.NET.Bindings
{
public static unsafe partial class Native
{
#if NET5_0_OR_GREATER
[SuppressGCTransition]
#endif
[DllImport(BindgenInternal.DllImportPath, EntryPoint = "ecs_get_binding_ctx", CallingConvention = CallingConvention.Cdecl)]
internal static extern void* ecs_get_binding_ctx_fast(ecs_world_t* world);

// Temporary hack until the suspend/resume functions are made public.
[DllImport(BindgenInternal.DllImportPath, EntryPoint = "flecs_suspend_readonly", CallingConvention = CallingConvention.Cdecl)]
internal static extern ecs_world_t* flecs_suspend_readonly(ecs_world_t* world, ecs_suspend_readonly_state_t* state);

[DllImport(BindgenInternal.DllImportPath, EntryPoint = "flecs_resume_readonly", CallingConvention = CallingConvention.Cdecl)]
internal static extern void flecs_resume_readonly(ecs_world_t* world, ecs_suspend_readonly_state_t* state);

// Larger size just in case it changes.
[StructLayout(LayoutKind.Explicit, Size = 96 * 2)]
internal struct ecs_suspend_readonly_state_t { }
}
}
File renamed without changes.
5 changes: 0 additions & 5 deletions src/Flecs.NET.Tests/CSharp/Core/EventTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,6 @@ namespace Flecs.NET.Tests.CSharp.Core
{
public class EventTests
{
public EventTests()
{
FlecsInternal.Reset();
}

[Fact]
public void EntityEmitEventWithManagedPayload()
{
Expand Down
5 changes: 0 additions & 5 deletions src/Flecs.NET.Tests/CSharp/Core/ObserverTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,6 @@ namespace Flecs.NET.Tests.CSharp.Core
{
public unsafe class ObserverTests
{
public ObserverTests()
{
FlecsInternal.Reset();
}

[Fact]
private void IterDelegateCallback()
{
Expand Down
5 changes: 0 additions & 5 deletions src/Flecs.NET.Tests/CSharp/Core/QueryBuilderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,6 @@ namespace Flecs.NET.Tests.CSharp.Core
[SuppressMessage("ReSharper", "AccessToModifiedClosure")]
public class QueryBuilderTests
{
public QueryBuilderTests()
{
FlecsInternal.Reset();
}

[Fact]
private void GroupBy()
{
Expand Down
5 changes: 0 additions & 5 deletions src/Flecs.NET.Tests/CSharp/Core/QueryTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,6 @@ namespace Flecs.NET.Tests.CSharp.Core
{
public unsafe class QueryTests
{
public QueryTests()
{
FlecsInternal.Reset();
}

[Fact]
private void IterDelegateCallback()
{
Expand Down
5 changes: 0 additions & 5 deletions src/Flecs.NET.Tests/CSharp/Core/SystemTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,6 @@ namespace Flecs.NET.Tests.CSharp.Core
{
public unsafe class SystemTests
{
public SystemTests()
{
FlecsInternal.Reset();
}

[Fact]
private void IterDelegateCallback()
{
Expand Down
Loading

0 comments on commit 9b3731c

Please sign in to comment.