Skip to content

Commit

Permalink
.gitignore BenchmarkDotNet artifacts
Browse files Browse the repository at this point in the history
Added new project Benchmarks

Benchmarks: Added two bench
  • Loading branch information
Xian55 committed Sep 8, 2024
1 parent 1a6edc0 commit bbbdd09
Show file tree
Hide file tree
Showing 6 changed files with 235 additions and 1 deletion.
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -293,4 +293,7 @@ HeadlessServer/data_config.json

PathingAPI/frame_config.json
PathingAPI/addon_config.json
PathingAPI/data_config.json
PathingAPI/data_config.json

# BenchmarkDotNet artifacts
**/BenchmarkDotNet.Artifacts/
18 changes: 18 additions & 0 deletions Benchmarks/Benchmarks.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="BenchmarkDotNet" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Core\Core.csproj" />
</ItemGroup>

</Project>
74 changes: 74 additions & 0 deletions Benchmarks/PPather/PPatherHasHoles.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
using BenchmarkDotNet.Attributes;

namespace Benchmarks.PPather;

[MemoryDiagnoser]
public class PPatherHasHoles
{

public bool hasholes = true;

public readonly uint holes;

// 0 ..3, 0 ..3
private static readonly int[] old_holetab_h = [0x1111, 0x2222, 0x4444, 0x8888];
private static readonly int[] old_holetab_v = [0x000F, 0x00F0, 0x0F00, 0xF000];

private static readonly int[] new_holetab = [
0x1111 & 0x000F, 0x1111 & 0x00F0, 0x1111 & 0x0F00, 0x1111 & 0xF000,
0x2222 & 0x000F, 0x2222 & 0x00F0, 0x2222 & 0x0F00, 0x2222 & 0xF000,
0x4444 & 0x000F, 0x4444 & 0x00F0, 0x4444 & 0x0F00, 0x4444 & 0xF000,
0x8888 & 0x000F, 0x8888 & 0x00F0, 0x8888 & 0x0F00, 0x8888 & 0xF000
];

[Benchmark(Baseline = true)]
[ArgumentsSource(nameof(Inputs_Fast))]
public bool IsHole_Old(int i, int j)
{
if (!hasholes)
return false;

i /= 2;
j /= 2;

return i <= 3 && j <= 3 && (holes & old_holetab_h[i] & old_holetab_v[j]) != 0;
}

[Benchmark]
[ArgumentsSource(nameof(Inputs_Fast))]
public bool IsHole_New(int i, int j)
{
if (!hasholes)
return false;

i >>= 1;
j >>= 1;

if (i > 3 || j > 3)
return false;

int index = (i << 2) | j;

return (holes & new_holetab[index]) != 0;
}

public static IEnumerable<object[]> Inputs_All()
{
const int min = -1; // -1
const int max = 5; // 5

// Generate a range of inputs from -1,-1 to 5,5
for (int i = min; i < max; i++)
{
for (int j = min; j < max; j++)
{
yield return [i, j];
}
}
}

public static IEnumerable<object[]> Inputs_Fast()
{
yield return [0, 0];
}
}
5 changes: 5 additions & 0 deletions Benchmarks/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
using BenchmarkDotNet.Running;

using Requirement;

BenchmarkSwitcher.FromAssembly(typeof(Program).Assembly).Run(args);
120 changes: 120 additions & 0 deletions Benchmarks/Requirement/CoreRequirementCreateTargetCastingSpell.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
using System.Runtime.CompilerServices;

using BenchmarkDotNet.Attributes;

namespace Requirement;

internal static class PlayerReader
{
public static bool IsTargetCasting() => true;

public static int SpellBeingCastByTarget { get; set; }
}

[MemoryDiagnoser]
public class CoreRequirementCreateTargetCastingSpell
{
private const char SEP1 = ':';
private const char SEP2 = ',';

public CoreRequirementCreateTargetCastingSpell()
{
/*
foreach(var input in CreateTargetCastingSpell_Inputs())
{
CreateTargetCastingSpell_Old(input);
CreateTargetCastingSpell_New(input);
}
*/
}

[Benchmark(Baseline = true)]
[ArgumentsSource(nameof(CreateTargetCastingSpell_Inputs))]
public void Run_Old(string text) => CreateTargetCastingSpell_Old(text);

[Benchmark]
[ArgumentsSource(nameof(CreateTargetCastingSpell_Inputs))]
public void Run_New(string text) => CreateTargetCastingSpell_New(text);

public Core.Requirement CreateTargetCastingSpell_Old(string requirement)
{
return create(requirement);
static Core.Requirement create(string requirement)
{
ReadOnlySpan<char> span = requirement;
int sep1 = span.IndexOf(SEP1);
// 'TargetCastingSpell'
if (sep1 == -1)
{
return new Core.Requirement
{
HasRequirement = PlayerReader.IsTargetCasting,
LogMessage = () => "Target casting"
};
}

// 'TargetCastingSpell:_1_?,_n_'
string[] spellsPart = span[(sep1 + 1)..].ToString().Split(SEP2);
HashSet<int> spellIds = spellsPart.Select(int.Parse).ToHashSet();

bool f() => spellIds.Contains(PlayerReader.SpellBeingCastByTarget);
string s() => $"Target casts {PlayerReader.SpellBeingCastByTarget} ∈ [{string.Join(SEP2, spellIds)}]";
return new Core.Requirement
{
HasRequirement = f,
LogMessage = s
};
}
}

[SkipLocalsInit]
public Core.Requirement CreateTargetCastingSpell_New(string requirement)
{
return create(requirement);
static Core.Requirement create(string requirement)
{
ReadOnlySpan<char> span = requirement;
int sep1 = span.IndexOf(SEP1);
// 'TargetCastingSpell'
if (sep1 == -1)
{
return new Core.Requirement
{
HasRequirement = PlayerReader.IsTargetCasting,
LogMessage = () => "Target casting"
};
}

// 'TargetCastingSpell:_1_?,_n_'
Span<Range> ranges = stackalloc Range[span.Length];
ReadOnlySpan<char> values = span[(sep1 + 1)..];
int count = values.Split(ranges, SEP2);

HashSet<int> spellIds = new(count);
foreach (var range in ranges[..count])
{
spellIds.Add(int.Parse(values[range]));
}

bool f() => spellIds.Contains(PlayerReader.SpellBeingCastByTarget);
string s() => $"Target casts {PlayerReader.SpellBeingCastByTarget} ∈ [{string.Join(SEP2, spellIds)}]";
return new Core.Requirement
{
HasRequirement = f,
LogMessage = s
};
}
}

public static IEnumerable<string> CreateTargetCastingSpell_Inputs()
{
yield return "TargetCastingSpell";
yield return "TargetCastingSpell:1";
yield return "TargetCastingSpell:1,12";
yield return "TargetCastingSpell:1,12,123";
yield return "TargetCastingSpell:1,12,123,1234";
yield return "TargetCastingSpell:4321,321,21,1";
yield return "TargetCastingSpell:4321648,3721,24841,148484";
}

}
14 changes: 14 additions & 0 deletions MasterOfPuppets.sln
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Frontend", "Frontend\Fronte
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "HeadlessServer", "HeadlessServer\HeadlessServer.csproj", "{8364AC12-DBF5-4DFF-B3A2-87472413DE01}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Benchmarks", "Benchmarks\Benchmarks.csproj", "{488984A0-F158-4EDA-A2CB-57C10D8596E1}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -185,6 +187,18 @@ Global
{8364AC12-DBF5-4DFF-B3A2-87472413DE01}.Release|x64.Build.0 = Release|x64
{8364AC12-DBF5-4DFF-B3A2-87472413DE01}.Release|x86.ActiveCfg = Release|x86
{8364AC12-DBF5-4DFF-B3A2-87472413DE01}.Release|x86.Build.0 = Release|x86
{488984A0-F158-4EDA-A2CB-57C10D8596E1}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{488984A0-F158-4EDA-A2CB-57C10D8596E1}.Debug|Any CPU.Build.0 = Debug|Any CPU
{488984A0-F158-4EDA-A2CB-57C10D8596E1}.Debug|x64.ActiveCfg = Debug|Any CPU
{488984A0-F158-4EDA-A2CB-57C10D8596E1}.Debug|x64.Build.0 = Debug|Any CPU
{488984A0-F158-4EDA-A2CB-57C10D8596E1}.Debug|x86.ActiveCfg = Debug|Any CPU
{488984A0-F158-4EDA-A2CB-57C10D8596E1}.Debug|x86.Build.0 = Debug|Any CPU
{488984A0-F158-4EDA-A2CB-57C10D8596E1}.Release|Any CPU.ActiveCfg = Release|Any CPU
{488984A0-F158-4EDA-A2CB-57C10D8596E1}.Release|Any CPU.Build.0 = Release|Any CPU
{488984A0-F158-4EDA-A2CB-57C10D8596E1}.Release|x64.ActiveCfg = Release|Any CPU
{488984A0-F158-4EDA-A2CB-57C10D8596E1}.Release|x64.Build.0 = Release|Any CPU
{488984A0-F158-4EDA-A2CB-57C10D8596E1}.Release|x86.ActiveCfg = Release|Any CPU
{488984A0-F158-4EDA-A2CB-57C10D8596E1}.Release|x86.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down

0 comments on commit bbbdd09

Please sign in to comment.