Skip to content

Commit

Permalink
Optimize EncodeBase83
Browse files Browse the repository at this point in the history
  • Loading branch information
Bond-009 committed Jul 3, 2023
1 parent 90bad6d commit 5275ad3
Show file tree
Hide file tree
Showing 7 changed files with 69 additions and 26 deletions.
17 changes: 17 additions & 0 deletions BlurHashSharp.sln
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BlurHashSharp.Tests", "test
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BlurHashSharp.SkiaSharp.Tests", "tests\BlurHashSharp.SkiaSharp.Tests\BlurHashSharp.SkiaSharp.Tests.csproj", "{B075D075-93C4-4A19-8657-773593308FD4}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "benches", "benches", "{16AA13F8-F18E-4C4D-98B9-B45E9E659E5B}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BlurHashSharp.Benches", "benches\BlurHashSharp.Benches\BlurHashSharp.Benches.csproj", "{E34874CB-32B1-402D-8E3C-071676468135}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -76,11 +80,24 @@ Global
{B075D075-93C4-4A19-8657-773593308FD4}.Release|x64.Build.0 = Release|Any CPU
{B075D075-93C4-4A19-8657-773593308FD4}.Release|x86.ActiveCfg = Release|Any CPU
{B075D075-93C4-4A19-8657-773593308FD4}.Release|x86.Build.0 = Release|Any CPU
{E34874CB-32B1-402D-8E3C-071676468135}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{E34874CB-32B1-402D-8E3C-071676468135}.Debug|Any CPU.Build.0 = Debug|Any CPU
{E34874CB-32B1-402D-8E3C-071676468135}.Debug|x64.ActiveCfg = Debug|Any CPU
{E34874CB-32B1-402D-8E3C-071676468135}.Debug|x64.Build.0 = Debug|Any CPU
{E34874CB-32B1-402D-8E3C-071676468135}.Debug|x86.ActiveCfg = Debug|Any CPU
{E34874CB-32B1-402D-8E3C-071676468135}.Debug|x86.Build.0 = Debug|Any CPU
{E34874CB-32B1-402D-8E3C-071676468135}.Release|Any CPU.ActiveCfg = Release|Any CPU
{E34874CB-32B1-402D-8E3C-071676468135}.Release|Any CPU.Build.0 = Release|Any CPU
{E34874CB-32B1-402D-8E3C-071676468135}.Release|x64.ActiveCfg = Release|Any CPU
{E34874CB-32B1-402D-8E3C-071676468135}.Release|x64.Build.0 = Release|Any CPU
{E34874CB-32B1-402D-8E3C-071676468135}.Release|x86.ActiveCfg = Release|Any CPU
{E34874CB-32B1-402D-8E3C-071676468135}.Release|x86.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
{680BD7F7-37FA-44B3-8297-91C6EF00A870} = {22515C2C-F86B-404F-A0B0-60F0B8FE9B8C}
{2422C8A8-5121-4BA4-8B8F-F8D00C659E98} = {22515C2C-F86B-404F-A0B0-60F0B8FE9B8C}
{EA7DE5B7-8A37-4BF1-B10A-7CE7B982CD8A} = {AA9B09A6-896C-4CB8-8B56-7AA05697B218}
{B075D075-93C4-4A19-8657-773593308FD4} = {AA9B09A6-896C-4CB8-8B56-7AA05697B218}
{E34874CB-32B1-402D-8E3C-071676468135} = {16AA13F8-F18E-4C4D-98B9-B45E9E659E5B}
EndGlobalSection
EndGlobal
4 changes: 2 additions & 2 deletions benches/BlurHashSharp.Benches/BlurHashSharp.Benches.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net5.0</TargetFramework>
<TargetFramework>net7.0</TargetFramework>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="BenchmarkDotNet" Version="0.12.1" />
<PackageReference Include="BenchmarkDotNet" Version="0.13.5" />
</ItemGroup>

<ItemGroup>
Expand Down
27 changes: 27 additions & 0 deletions benches/BlurHashSharp.Benches/EncodeBase83Benches.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using System;
using BenchmarkDotNet.Attributes;

namespace BlurHashSharp.Benches
{
[MemoryDiagnoser]
public class EncodeBase83Benches
{
private int _data;

[Params(1, 2, 4)]
public int N { get; set; }

[GlobalSetup]
public void GlobalSetup()
{
_data = new Random(42).Next();
}

[Benchmark]
public void EncodeBase83()
{
Span<char> dest = stackalloc char[4];
CoreBlurHashEncoder.EncodeBase83(_data, N, dest);
}
}
}
2 changes: 0 additions & 2 deletions benches/BlurHashSharp.Benches/MaxBenches.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
using System;
using System.IO;
using System.Linq;
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Running;

namespace BlurHashSharp.Benches
{
Expand Down
7 changes: 2 additions & 5 deletions benches/BlurHashSharp.Benches/PrecomputeCosinesBenches.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
using System;
using System.IO;
using System.Linq;
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Running;

namespace BlurHashSharp.Benches
{
Expand All @@ -21,9 +18,9 @@ public void GlobalSetup()
}

[Benchmark]
public void PrecomputeCosines() => CoreBlurHashEncoder.PrecomputeCosines(_data, N, MathF.PI / N);
public void PrecomputeCosines() => CoreBlurHashEncoder.PrecomputeCosines(_data, MathF.PI / N);

[Benchmark]
public void PrecomputeCosinesZero() => CoreBlurHashEncoder.PrecomputeCosines(_data, N, 0f);
public void PrecomputeCosinesZero() => CoreBlurHashEncoder.PrecomputeCosines(_data, 0f);
}
}
6 changes: 3 additions & 3 deletions benches/BlurHashSharp.Benches/Program.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System;
using BenchmarkDotNet.Running;
using BenchmarkDotNet.Running;

namespace BlurHashSharp.Benches
{
Expand All @@ -8,7 +7,8 @@ public static class Program
public static void Main(string[] args)
{
// _ = BenchmarkRunner.Run<MaxBenches>();
_ = BenchmarkRunner.Run<PrecomputeCosinesBenches>();
// _ = BenchmarkRunner.Run<PrecomputeCosinesBenches>();
_ = BenchmarkRunner.Run<EncodeBase83Benches>();
}
}
}
32 changes: 18 additions & 14 deletions src/BlurHashSharp/CoreBlurHashEncoder.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
using System;
using System.Runtime.CompilerServices;
using System.Diagnostics;
using System.Runtime.Intrinsics;
using System.Runtime.Intrinsics.Arm;
using System.Runtime.Intrinsics.X86;

namespace BlurHashSharp
{
Expand Down Expand Up @@ -121,11 +126,11 @@ static int ThrowPixelFormatArgumentException()

for (int yC = 0; yC < yComponents; yC++)
{
PrecomputeCosines(cosYLookup, height, MathF.PI * yC / height);
PrecomputeCosines(cosYLookup, MathF.PI * yC / height);

for (int xC = 0; xC < xComponents; xC++)
{
PrecomputeCosines(cosXLookup, width, MathF.PI * xC / width);
PrecomputeCosines(cosXLookup, MathF.PI * xC / width);

float c1 = 0;
float c2 = 0;
Expand Down Expand Up @@ -209,7 +214,7 @@ static int ThrowPixelFormatArgumentException()
});
}

internal static void PrecomputeCosines(Span<float> table, int count, float offset)
internal static void PrecomputeCosines(Span<float> table, float offset)
{
if (offset == 0f)
{
Expand All @@ -218,7 +223,7 @@ internal static void PrecomputeCosines(Span<float> table, int count, float offse
}

float coef = 0f;
for (int i = 0; i < count; i++)
for (int i = 0; i < table.Length; i++)
{
table[i] = MathF.Cos(coef);
coef += offset;
Expand Down Expand Up @@ -260,20 +265,19 @@ internal static float SignSqrtF(float value)

internal static int EncodeBase83(int value, int length, Span<char> destination)
{
const int Base = 83;
const string Characters = @"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz#$%*+,-.:;=?@[]^_{|}~";
int divisor = 1;

int i = 0;
for (; i < length - 1; i++)
{
divisor *= 83;
}
Debug.Assert(Base == Characters.Length);
Debug.Assert(length <= destination.Length);

destination[length - 1] = Characters[value % Base];

for (i = 0; i < length; i++)
int tmpValue = value;
for (int i = length - 2; i >= 0; i--)
{
int digit = (value / divisor) % 83;
divisor /= 83;
destination[i] = Characters[digit];
tmpValue /= Base;
destination[i] = Characters[tmpValue % Base];
}

return length;
Expand Down

0 comments on commit 5275ad3

Please sign in to comment.