Skip to content

Commit

Permalink
Several changes, upgraded Arch.LowLevel to latest version for improve…
Browse files Browse the repository at this point in the history
…d performance, some query adjustments for improved performance and added first templates to replace source-gen.
  • Loading branch information
genaray committed Aug 8, 2024
1 parent 3a3c41d commit 70f9f27
Show file tree
Hide file tree
Showing 9 changed files with 358 additions and 6 deletions.
4 changes: 2 additions & 2 deletions src/Arch.SourceGen/Fundamentals/Get.cs
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ public static StringBuilder AppendChunkIndexGet(this StringBuilder sb, int amoun
{
var generics = new StringBuilder().GenericWithoutBrackets(amount);
var inParams = new StringBuilder().InsertGenericParams(amount);
var arrays = new StringBuilder().GetChunkFirstGenericElements(amount);
var arrays = new StringBuilder().GetChunkFirstGenericElements(amount, "");

var gets = new StringBuilder();
for (var index = 0; index <= amount; index++)
Expand Down Expand Up @@ -186,7 +186,7 @@ public static StringBuilder AppendChunkIndexGetRows(this StringBuilder sb, int a
public static StringBuilder AppendChunkIndexGetRow(this StringBuilder sb, int amount)
{
var generics = new StringBuilder().GenericWithoutBrackets(amount);
var getArrays = new StringBuilder().GetChunkFirstGenericElements(amount);
var getArrays = new StringBuilder().GetChunkFirstGenericElements(amount, "");
var inParams = new StringBuilder().InsertGenericParams(amount);

var gets = new StringBuilder();
Expand Down
2 changes: 1 addition & 1 deletion src/Arch.SourceGen/Fundamentals/Set.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public static StringBuilder AppendChunkIndexSet(this StringBuilder sb, int amoun
{
var generics = new StringBuilder().GenericWithoutBrackets(amount);
var parameters = new StringBuilder().GenericInParams(amount);
var arrays = new StringBuilder().GetChunkFirstGenericElements(amount);
var arrays = new StringBuilder().GetChunkFirstGenericElements(amount, "");

var sets = new StringBuilder();
for (var index = 0; index <= amount; index++)
Expand Down
4 changes: 2 additions & 2 deletions src/Arch.SourceGen/StringBuilderExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,11 @@ public static StringBuilder GenericWithoutBrackets(this StringBuilder sb, int am
/// <param name="sb"></param>
/// <param name="amount"></param>
/// <returns></returns>
public static StringBuilder GetChunkFirstGenericElements(this StringBuilder sb, int amount)
public static StringBuilder GetChunkFirstGenericElements(this StringBuilder sb, int amount, String placeholder = "chunk.")
{
for (var localIndex = 0; localIndex <= amount; localIndex++)
{
sb.AppendLine($"ref var t{localIndex}FirstElement = ref chunk.GetFirst<T{localIndex}>();");
sb.AppendLine($"ref var t{localIndex}FirstElement = ref {placeholder}GetFirst<T{localIndex}>();");
}
return sb;
}
Expand Down
17 changes: 16 additions & 1 deletion src/Arch/Arch.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -94,12 +94,27 @@ Introduced breaking changes by renaming Group to Component and several other sma
</ItemGroup>

<ItemGroup>
<PackageReference Include="Arch.LowLevel" Version="1.1.1" />
<PackageReference Include="Arch.LowLevel" Version="1.1.3" />
<PackageReference Include="Collections.Pooled" Version="2.0.0-preview.27" />
<PackageReference Include="CommunityToolkit.HighPerformance" Version="8.2.2" />
<PackageReference Include="Microsoft.Extensions.ObjectPool" Version="7.0.0" />
<PackageReference Include="System.Runtime.CompilerServices.Unsafe" Version="6.0.0" />
<PackageReference Include="ZeroAllocJobScheduler" Version="1.1.2" />
</ItemGroup>

<ItemGroup>
<None Update="Templates\Component.tt">
<Generator>TextTemplatingFileGenerator</Generator>
<LastGenOutput>Component.cs</LastGenOutput>
</None>
<None Update="Templates\Components.tt">
<Generator>TextTemplatingFileGenerator</Generator>
<LastGenOutput>Components.cs</LastGenOutput>
</None>
<None Update="Templates\EntityComponents.tt">
<Generator>TextTemplatingFileGenerator</Generator>
<LastGenOutput>EntityComponents.cs</LastGenOutput>
</None>
</ItemGroup>

</Project>
46 changes: 46 additions & 0 deletions src/Arch/Templates/Component.tt
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<#@ template language="C#" #>
<#@ output extension=".cs" #>
<#@ import namespace="System.Text" #>
<#@ include file="Helpers.ttinclude" #>

using Arch.Core;
using System;
using System.Threading;

namespace Arch.Core.Utils;

<#
for (var index = 1; index < Amount; index++)
{
var generics = AppendGenerics(index);
var types = AppendTypes(index);
#>

/// <inheritdoc cref="Component"/>
public static class Component<<#= generics #>>
{
internal static readonly int Id;

/// <summary>
/// An <see cref="Signature"/> for this given set of components.
/// </summary>
public static readonly Signature Signature;

/// <summary>
/// The hash code for this given set of components.
/// </summary>
public static readonly int Hash;

static Component()
{
Id = Interlocked.Increment(ref Component.Id);
Signature = new Signature(new [] { <#= types #> });
Hash = Signature.GetHashCode();
}
}

<#
}
#>


63 changes: 63 additions & 0 deletions src/Arch/Templates/Components.tt
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<#@ template language="C#" #>
<#@ output extension=".cs" #>
<#@ import namespace="System.Text" #>
<#@ include file="Helpers.ttinclude" #>

using System;
using System.Runtime.CompilerServices;
using CommunityToolkit.HighPerformance;
using Arch.Core.Utils;

namespace Arch.Core;

<#
for (var index = 1; index < Amount; index++)
{
var generics = AppendGenerics(index);
var parameters = AppendGenericRefParameters(index);

var refStructs = new StringBuilder();
for (var i = 0; i < index; i++)
refStructs.AppendLine($"public Ref<T{i}> t{i};");

var references = new StringBuilder();
for (var i = 0; i < index; i++)
references.AppendLine($"public ref T{i} t{i};");

var assignRefStructs = new StringBuilder();
for (var i = 0; i < index; i++)
assignRefStructs.AppendLine($"t{i} = new Ref<T{i}>(ref t{i}Component);");

var assignRefs = new StringBuilder();
for (var i = 0; i < index; i++)
assignRefs.AppendLine($"t{i} = ref t{i}Component;");
#>

[SkipLocalsInit]
public ref struct Components<<#= generics #>>
{

#if NETSTANDARD2_1 || NET6_0
<#= refStructs #>
#else
<#= references #>
#endif

[SkipLocalsInit]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public Components(<#= parameters #>){

#if NETSTANDARD2_1 || NET6_0
<#= assignRefStructs #>
#else
<#= assignRefs #>
#endif

}
}

<#
}
#>


67 changes: 67 additions & 0 deletions src/Arch/Templates/EntityComponents.tt
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<#@ template language="C#" #>
<#@ output extension=".cs" #>
<#@ import namespace="System.Text" #>
<#@ include file="Helpers.ttinclude" #>

using System;
using System.Runtime.CompilerServices;
using CommunityToolkit.HighPerformance;
using Arch.Core.Utils;

namespace Arch.Core;

<#
for (var index = 1; index < Amount; index++)
{
var generics = AppendGenerics(index);
var parameters = AppendGenericRefParameters(index);

var refStructs = new StringBuilder();
for (var i = 0; i < index; i++)
refStructs.AppendLine($"public Ref<T{i}> t{i};");

var references = new StringBuilder();
for (var i = 0; i < index; i++)
references.AppendLine($"public ref T{i} t{i};");

var assignRefStructs = new StringBuilder();
for (var i = 0; i < index; i++)
assignRefStructs.AppendLine($"t{i} = new Ref<T{i}>(ref t{i}Component);");

var assignRefs = new StringBuilder();
for (var i = 0; i < index; i++)
assignRefs.AppendLine($"t{i} = ref t{i}Component;");
#>

[SkipLocalsInit]
public ref struct EntityComponents<<#= generics #>>
{

#if NETSTANDARD2_1 || NET6_0
public ReadOnlyRef<Entity> Entity;
<#= refStructs #>
#else
public ref readonly Entity Entity;
<#= references #>
#endif

[SkipLocalsInit]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public EntityComponents(ref Entity entity, <#= parameters #>){

#if NETSTANDARD2_1 || NET6_0
Entity = new ReadOnlyRef<Entity>(in entity);
<#= assignRefStructs #>
#else
Entity = ref entity;
<#= assignRefs #>
#endif

}
}

<#
}
#>


97 changes: 97 additions & 0 deletions src/Arch/Templates/Helpers.ttinclude
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
<#@ import namespace="System.Text" #>
<#@ import namespace="System.Collections.Generic" #>
<#+

public int Amount = 25;

string AppendGenerics(int amount)
{
var sb = new StringBuilder();
for (var i = 0; i < amount; i++)
{
if (i > 0) sb.Append(", ");
sb.Append($"T{i}");
}
return sb.ToString();
}

string AppendTypes(int amount)
{
var sb = new StringBuilder();
for (var i = 0; i < amount; i++)
{
sb.Append($"Component<T{i}>.ComponentType,");
}
return sb.ToString();
}

/// <summary>
/// Lists ref params in a row as parameters.
/// <example>
/// <code>
/// ref T0 t0Component, ref T1 t1Component,...
/// </code>
/// </example>
/// </summary>
/// <param name="sb"></param>
/// <param name="amount"></param>
/// <returns></returns>
public StringBuilder AppendGenericRefParameters(int amount)
{
var sb = new StringBuilder();
for (var localIndex = 0; localIndex < amount; localIndex++)
{
sb.Append($"ref T{localIndex} t{localIndex}Component,");
}

sb.Length--;
return sb;
}


/// <summary>
/// Lists in params in a row as parameters.
/// <example>
/// <code>
/// in T0 t0Component, in T1 t1Component,...
/// </code>
/// </example>
/// </summary>
/// <param name="sb"></param>
/// <param name="amount"></param>
/// <returns></returns>
public StringBuilder AppendGenericInDefaultParams(int amount, string name = "Component")
{
var sb = new StringBuilder();
for (var localIndex = 0; localIndex < amount; localIndex++)
{
sb.Append($"in T{localIndex} t{localIndex}{name} = default,");
}

sb.Length--;
return sb;
}

/// <summary>
/// Inserts ref params in a row as parameters.
/// <example>
/// <code>
/// in t0Component, in t1Component,...
/// </code>
/// </example>
/// </summary>
/// <param name="sb"></param>
/// <param name="amount"></param>
/// <returns></returns>
public StringBuilder InsertGenericInParams(int amount)
{
var sb = new StringBuilder();
for (var localIndex = 0; localIndex < amount; localIndex++)
{
sb.Append($"in t{localIndex}Component,");
}

sb.Length--;
return sb;
}
#>
Loading

0 comments on commit 70f9f27

Please sign in to comment.