Performance tweaks

This commit is contained in:
Deukhoofd committed 2025-08-18 16:37:52 +02:00
1 parent e14d8d82f3
commit 29ddcd8d74
52 files changed
+71 -46

No files matched your search

+2 -3
View File
@@ -30,8 +30,8 @@ public static class Program
morph.SetDensity(2); // Higher than 1 sets fluid to 0.
morph.SetMotion(InterpolationMethod.Spline);
morph.SetFading(InterpolationMethod.Perlin);
morph.SetThreads(8);
morph.SetCycleLength(100000);
morph.SetThreads(80);
morph.SetCycleLength(10000);
morph.SetFeather(0);
morph.SetKeepBackground(false);
morph.SetFinite(true);
@@ -52,7 +52,6 @@ public static class Program
}
return 0;
// Add your main logic here
}
private static bool LoadFiles(Morph morph, Options options)
Binary file not shown.

Before

Width:  |  Height:  |  Size: 27 KiB

After

Width:  |  Height:  |  Size: 27 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 31 KiB

After

Width:  |  Height:  |  Size: 31 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 33 KiB

After

Width:  |  Height:  |  Size: 33 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 35 KiB

After

Width:  |  Height:  |  Size: 34 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 36 KiB

After

Width:  |  Height:  |  Size: 36 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 36 KiB

After

Width:  |  Height:  |  Size: 36 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 35 KiB

After

Width:  |  Height:  |  Size: 35 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 35 KiB

After

Width:  |  Height:  |  Size: 34 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 33 KiB

After

Width:  |  Height:  |  Size: 33 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 31 KiB

After

Width:  |  Height:  |  Size: 31 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 29 KiB

After

Width:  |  Height:  |  Size: 28 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 25 KiB

After

Width:  |  Height:  |  Size: 24 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 20 KiB

After

Width:  |  Height:  |  Size: 19 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 15 KiB

After

Width:  |  Height:  |  Size: 15 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 11 KiB

After

Width:  |  Height:  |  Size: 14 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 13 KiB

After

Width:  |  Height:  |  Size: 16 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 18 KiB

After

Width:  |  Height:  |  Size: 21 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 23 KiB

After

Width:  |  Height:  |  Size: 25 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 27 KiB

After

Width:  |  Height:  |  Size: 27 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 28 KiB

After

Width:  |  Height:  |  Size: 31 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 30 KiB

After

Width:  |  Height:  |  Size: 30 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 32 KiB

After

Width:  |  Height:  |  Size: 26 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 33 KiB

After

Width:  |  Height:  |  Size: 22 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 33 KiB

After

Width:  |  Height:  |  Size: 18 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 33 KiB

After

Width:  |  Height:  |  Size: 15 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 32 KiB

After

Width:  |  Height:  |  Size: 18 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 31 KiB

After

Width:  |  Height:  |  Size: 23 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 30 KiB

After

Width:  |  Height:  |  Size: 27 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 29 KiB

After

Width:  |  Height:  |  Size: 29 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 31 KiB

After

Width:  |  Height:  |  Size: 31 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 33 KiB

After

Width:  |  Height:  |  Size: 28 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 35 KiB

After

Width:  |  Height:  |  Size: 23 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 37 KiB

After

Width:  |  Height:  |  Size: 18 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 38 KiB

After

Width:  |  Height:  |  Size: 15 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 38 KiB

After

Width:  |  Height:  |  Size: 17 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 38 KiB

After

Width:  |  Height:  |  Size: 23 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 37 KiB

After

Width:  |  Height:  |  Size: 28 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 35 KiB

After

Width:  |  Height:  |  Size: 33 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 33 KiB

After

Width:  |  Height:  |  Size: 35 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 29 KiB

After

Width:  |  Height:  |  Size: 37 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 23 KiB

After

Width:  |  Height:  |  Size: 39 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 18 KiB

After

Width:  |  Height:  |  Size: 39 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 13 KiB

After

Width:  |  Height:  |  Size: 39 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 14 KiB

After

Width:  |  Height:  |  Size: 38 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 19 KiB

After

Width:  |  Height:  |  Size: 36 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 23 KiB

After

Width:  |  Height:  |  Size: 33 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 26 KiB

After

Width:  |  Height:  |  Size: 31 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 26 KiB

After

Width:  |  Height:  |  Size: 26 KiB

+41 -30
View File
@@ -268,7 +268,11 @@ public class AtomorphThread : IDisposable
public void Start()
{
_running = true;
_stepThread = new Thread(Run) { Name = "AtomorphThread", };
_stepThread = new Thread(Run)
{
Name = "AtomorphThread",
IsBackground = true,
};
_stepThread.Start();
}
@@ -530,23 +534,24 @@ public class AtomorphThread : IDisposable
#else
private static object morph_mutex = new object();
private static void MorphAsync(Point[][] map, ulong width, ulong height, ulong repeat, uint rngSeed,
ref double gain)
private class GainWrapper
{
var random = new PcgRandom((int)rngSeed);
public double Gain { get; set; }
}
ulong y = (ulong)random.Next(0, (int)(height - 1));
private static async Task MorphAsync(Point[][] map, ulong width, ulong height, ulong repeat, Pcg32Single random,
GainWrapper gain)
{
ulong y = random.GenerateNext((uint)(height - 1));
ulong y_next = (y + 1) % (height);
ulong y_prev = (y > 0 ? y - 1 : height - 1);
for (ulong i = 0; i < repeat; ++i)
{
ulong x1, x2;
x1 = (ulong)random.Next(0, (int)(width - 1));
do
{
x2 = (ulong)random.Next(0, (int)(width - 1));
} while (x1 == x2);
ulong x1 = random.GenerateNext((uint)(width - 1));
ulong x2 = random.GenerateNext((uint)(width - 2));
if (x2 >= x1)
x2++;
var x1_y_prev = map[x1][y_prev];
var x2_y_prev = map[x2][y_prev];
@@ -573,45 +578,52 @@ public class AtomorphThread : IDisposable
{
if (map[x1][y].Word != x1_y.Word || map[x2][y].Word != x2_y.Word)
continue;
var buf = map[x2][y];
map[x2][y] = map[x1][y];
map[x1][y] = buf;
gain += c1 - c2;
(map[x2][y], map[x1][y]) = (map[x1][y], map[x2][y]);
gain.Gain += c1 - c2;
}
}
}
}
private Task[]? _morphWorkers = null;
private Pcg32Single[]? _morphRandoms = null;
private bool Morph()
{
var ch = _chains[_counter % _blobMapW];
if (ch.Width <= 1) return false;
var map = ch.Points;
var map = ch.Points!;
double gain = 0.0;
var gainWrapper = new GainWrapper { Gain = 0.0 };
if (_threadCount == 0)
{
MorphAsync(map, ch.Width, ch.Height, _cycleLength, (uint)_random.Next(), ref gain);
var random = new Pcg32Single((uint)_random.Next());
MorphAsync(map, ch.Width, ch.Height, _cycleLength, random, gainWrapper).ConfigureAwait(false).GetAwaiter()
.GetResult();
}
else
{
List<Thread> workers = [];
for (ulong i = 0; i < _threadCount; ++i)
if (_morphWorkers is null || (ulong)_morphWorkers.Length != _threadCount)
_morphWorkers = new Task[_threadCount];
if (_morphRandoms is null || (ulong)_morphRandoms.Length != _threadCount)
{
var thread = new Thread(() =>
MorphAsync(map, ch.Width, ch.Height, _cycleLength, (uint)_random.Next(), ref gain));
thread.Name = $"MorphThread-{i}";
thread.Start();
workers.Add(thread);
_morphRandoms = new Pcg32Single[_threadCount];
for (ulong i = 0; i < _threadCount; ++i)
{
_morphRandoms[i] = new Pcg32Single((uint)_random.Next());
}
}
while (workers.Any())
for (ulong i = 0; i < _threadCount; ++i)
{
workers.Last().Join();
workers.RemoveAt(workers.Count - 1);
var random = _morphRandoms[i];
var workerTask = MorphAsync(map, ch.Width, ch.Height, _cycleLength, random, gainWrapper);
_morphWorkers[i] = workerTask;
}
Task.WaitAll(_morphWorkers);
}
gain = gainWrapper.Gain;
_chainMapE -= gain;
return false;
@@ -717,8 +729,8 @@ public class AtomorphThread : IDisposable
{
pos = it;
var ownerPos = f.Owners[pos];
if (bl.Surface.Count(v => v == pos) != 0 || f.Owners.Count(v => v.Key == pos) == 0 ||
checkedBlobs.Count(v => v == ownerPos) != 0 || !HasPixel(frameKey, pos))
if (!HasPixel(frameKey, pos) || bl.Surface.Contains(pos) || !f.Owners.ContainsKey(pos) ||
checkedBlobs.Contains(ownerPos))
{
// Don't expand into itself and empty positions.
if (checkThreshold && (ulong)bl.Surface.Count >= _blobMinSize)
@@ -759,7 +771,6 @@ public class AtomorphThread : IDisposable
}
}
MainHandling:
if (bestFound)
{
// Merge with this blob.
+1
View File
@@ -15,6 +15,7 @@ public static class AtomorphUtilities
y = pt.Y + pt.YFract/(float)(byte.MaxValue+1);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static ulong PointDistance(Point p1, Point p2)
{
var xd = (ulong)Math.Abs(((byte.MaxValue+1)*p1.X+p1.XFract) - ((byte.MaxValue+1)*p2.X+p2.XFract));
+27 -13
View File
@@ -1,3 +1,5 @@
using System.Runtime.CompilerServices;
namespace Atomorph.Models;
public record struct Point
@@ -14,41 +16,53 @@ public record struct Point
// } point;
//
private ulong _word;
private ushort _x;
private ushort _y;
private byte _xFract;
private byte _yFract;
private PointFlags _flags;
public ulong Word
{
get => _word;
set => _word = value;
[method: MethodImpl(MethodImplOptions.AggressiveInlining)]
get => _x | ((ulong)_y << 16) | ((ulong)_xFract << 32) | ((ulong)_yFract << 40) | ((ulong)_flags << 48);
set
{
_x = (ushort)(value & 0xFFFF);
_y = (ushort)((value >> 16) & 0xFFFF);
_xFract = (byte)((value >> 32) & 0xFF);
_yFract = (byte)((value >> 40) & 0xFF);
_flags = (PointFlags)((value >> 48) & 0xFF);
}
}
public ushort X
{
get => (ushort)(_word & 0xFFFF);
set => _word = (_word & ~0xFFFFUL) | value;
get => _x;
set => _x = value;
}
public ushort Y
{
get => (ushort)((_word >> 16) & 0xFFFF);
set => _word = (_word & ~(0xFFFFUL << 16)) | ((ulong)value << 16);
get => _y;
set => _y = value;
}
public byte XFract
{
get => (byte)((_word >> 32) & 0xFF);
set => _word = (_word & ~(0xFFUL << 32)) | ((ulong)value << 32);
get => _xFract;
set => _xFract = value;
}
public byte YFract
{
get => (byte)((_word >> 40) & 0xFF);
set => _word = (_word & ~(0xFFUL << 40)) | ((ulong)value << 40);
get => _yFract;
set => _yFract = value;
}
public PointFlags Flags
{
get => (PointFlags)((_word >> 48) & 0xFF);
set => _word = (_word & ~(0xFFUL << 48)) | ((ulong)value << 48);
get => _flags;
set => _flags = value;
}
}