Skip to content

A Civilization-style strategy game about governing Israel from 15 May 1948. Real ministers, real map, every fact sourced. Unity + deterministic C# sim.

perf(map): the biome blend uses flat arrays, bit-identical weights (playtest 2, P2-1)

eyalgolan087c5f85c7dc
diff --git a/unity/Assets/Scripts/Map/BiomeBlender.cs b/unity/Assets/Scripts/Map/BiomeBlender.cs
index 570320c96..fb09f4d8c 100644
--- a/unity/Assets/Scripts/Map/BiomeBlender.cs
+++ b/unity/Assets/Scripts/Map/BiomeBlender.cs
@@ -14,7 +14,12 @@ namespace Statecraft.Game.Map
/// have more than 10% weight about 0.6 of a hex width (1.25 gave about 0.24).</summary>
public const float BlendRadiusHexes = 1.72f;
- readonly Dictionary<(int, int), List<(Vector2 p, int layer)>> grid = new Dictionary<(int, int), List<(Vector2, int)>>();
+ // The radius-sized grid as flat arrays (playtest 2, P2-1: the tuple-keyed dictionary made this the largest avoidable
+ // boot phase). Cell (ix, iy) holds items cellStart[c] .. cellStart[c + 1] - 1, in insertion order, so the weights are
+ // bit-identical to the dictionary version (BiomeBlenderSpeedTests).
+ readonly int minX, minY, nx, ny;
+ readonly int[] cellStart;
+ readonly float[] px, py; readonly int[] pl;
readonly List<(Vector2 p, int layer)> all = new List<(Vector2, int)>();
readonly float radius, cell; readonly int layerCount;
@@ -25,14 +30,27 @@ namespace Statecraft.Game.Map
this.layerCount = layerCount;
radius = BlendRadiusHexes * hexSizeM;
cell = radius; // a radius-sized cell: the 3x3 neighbourhood covers every centre within the radius
+ int maxX = int.MinValue, maxY = int.MinValue; minX = int.MaxValue; minY = int.MaxValue;
for (int i = 0; i < centres.Count; i++)
{
if (layers[i] < 0 || layers[i] >= layerCount) throw new ArgumentOutOfRangeException(nameof(layers));
- var c = (centres[i], layers[i]);
- all.Add(c);
- var k = Key(centres[i]);
- if (!grid.TryGetValue(k, out var l)) grid[k] = l = new List<(Vector2, int)>();
- l.Add(c);
+ all.Add((centres[i], layers[i]));
+ var (kx, ky) = Key(centres[i]);
+ minX = Math.Min(minX, kx); minY = Math.Min(minY, ky); maxX = Math.Max(maxX, kx); maxY = Math.Max(maxY, ky);
+ }
+ if (centres.Count == 0) { minX = minY = 0; maxX = maxY = -1; }
+ nx = maxX - minX + 1; ny = maxY - minY + 1;
+ cellStart = new int[nx * ny + 1];
+ var counts = new int[nx * ny];
+ foreach (var c in all) { var (kx, ky) = Key(c.p); counts[(kx - minX) + (ky - minY) * nx]++; }
+ for (int c = 0; c < counts.Length; c++) cellStart[c + 1] = cellStart[c] + counts[c];
+ px = new float[all.Count]; py = new float[all.Count]; pl = new int[all.Count];
+ var fill = (int[])cellStart.Clone();
+ foreach (var c in all) // insertion order within each cell
+ {
+ var (kx, ky) = Key(c.p);
+ var at = fill[(kx - minX) + (ky - minY) * nx]++;
+ px[at] = c.p.x; py[at] = c.p.y; pl[at] = c.layer;
}
}
@@ -45,15 +63,23 @@ namespace Statecraft.Game.Map
Array.Clear(w, 0, w.Length);
var (cx, cy) = Key(p); float total = 0f;
for (int dx = -1; dx <= 1; dx++)
+ {
+ int gx = cx + dx - minX;
+ if (gx < 0 || gx >= nx) continue;
for (int dy = -1; dy <= 1; dy++)
- if (grid.TryGetValue((cx + dx, cy + dy), out var l))
- foreach (var c in l)
- {
- float t = (c.p - p).magnitude / radius;
- if (t >= 1f) continue;
- float f = 1f - t * t * (3f - 2f * t); // 1 - smoothstep
- w[c.layer] += f; total += f;
- }
+ {
+ int gy = cy + dy - minY;
+ if (gy < 0 || gy >= ny) continue;
+ int c = gx + gy * nx;
+ for (int k = cellStart[c]; k < cellStart[c + 1]; k++)
+ {
+ float t = new Vector2(px[k] - p.x, py[k] - p.y).magnitude / radius;
+ if (t >= 1f) continue;
+ float f = 1f - t * t * (3f - 2f * t); // 1 - smoothstep
+ w[pl[k]] += f; total += f;
+ }
+ }
+ }
if (total > 0f) { for (int i = 0; i < w.Length; i++) w[i] /= total; return; }
w[NearestLayer(p)] = 1f;
}
diff --git a/unity/Assets/Tests/EditMode/BiomeBlenderSpeedTests.cs b/unity/Assets/Tests/EditMode/BiomeBlenderSpeedTests.cs
new file mode 100644
index 000000000..bc9c71094
--- /dev/null
+++ b/unity/Assets/Tests/EditMode/BiomeBlenderSpeedTests.cs
@@ -0,0 +1,76 @@
+#nullable enable
+using System;
+using System.Collections.Generic;
+using NUnit.Framework;
+using Statecraft.Game.Map;
+using UnityEngine;
+
+/// <summary>Playtest 2, P2-1: the biome blend was the largest avoidable boot phase (4.4 s of 14.3 s at an 8x slower CPU).
+/// Its grid moved from a tuple-keyed dictionary to flat arrays; the weights must stay bit-identical to the original
+/// algorithm (kept here as the reference), so the map looks exactly the same.</summary>
+public class BiomeBlenderSpeedTests
+{
+ /// <summary>The original BiomeBlender.Weights, verbatim in its arithmetic and iteration order.</summary>
+ sealed class Reference
+ {
+ readonly Dictionary<(int, int), List<(Vector2 p, int layer)>> grid = new Dictionary<(int, int), List<(Vector2, int)>>();
+ readonly List<(Vector2 p, int layer)> all = new List<(Vector2, int)>();
+ readonly float radius, cell; readonly int layerCount;
+ public Reference(IReadOnlyList<Vector2> centres, IReadOnlyList<int> layers, float hexSizeM, int layerCount)
+ {
+ this.layerCount = layerCount; radius = BiomeBlender.BlendRadiusHexes * hexSizeM; cell = radius;
+ for (int i = 0; i < centres.Count; i++)
+ {
+ var c = (centres[i], layers[i]); all.Add(c);
+ var k = Key(centres[i]);
+ if (!grid.TryGetValue(k, out var l)) grid[k] = l = new List<(Vector2, int)>();
+ l.Add(c);
+ }
+ }
+ (int, int) Key(Vector2 p) => (Mathf.FloorToInt(p.x / cell), Mathf.FloorToInt(p.y / cell));
+ public void Weights(Vector2 p, float[] w)
+ {
+ Array.Clear(w, 0, w.Length);
+ var (cx, cy) = Key(p); float total = 0f;
+ for (int dx = -1; dx <= 1; dx++)
+ for (int dy = -1; dy <= 1; dy++)
+ if (grid.TryGetValue((cx + dx, cy + dy), out var l))
+ foreach (var c in l)
+ {
+ float t = (c.p - p).magnitude / radius;
+ if (t >= 1f) continue;
+ float f = 1f - t * t * (3f - 2f * t);
+ w[c.layer] += f; total += f;
+ }
+ if (total > 0f) { for (int i = 0; i < w.Length; i++) w[i] /= total; return; }
+ float best = float.MaxValue; int layer = 0;
+ foreach (var c in all) { float d = (c.p - p).sqrMagnitude; if (d < best) { best = d; layer = c.layer; } }
+ w[layer] = 1f;
+ }
+ }
+
+ [Test]
+ public void TheFastBlendGivesExactlyTheOriginalWeights()
+ {
+ var rng = new System.Random(1948);
+ var centres = new List<Vector2>(); var layers = new List<int>();
+ const float hex = 5000f;
+ for (int q = -3; q < 40; q++) // a pointy-top hex lattice, like the map's
+ for (int r = 0; r < 90; r++)
+ {
+ centres.Add(new Vector2(hex * Mathf.Sqrt(3) * (q + r / 2f), hex * 1.5f * r));
+ layers.Add(rng.Next(7));
+ }
+ var fast = new BiomeBlender(centres, layers, hex, 7);
+ var reference = new Reference(centres, layers, hex, 7);
+ float[] a = new float[7], b = new float[7];
+ for (int i = 0; i < 20000; i++)
+ {
+ // inside the lattice, at its edges and well outside it (the nearest-centre fallback)
+ var p = new Vector2((float)(rng.NextDouble() * 400000 - 60000), (float)(rng.NextDouble() * 760000 - 60000));
+ fast.Weights(p, a); reference.Weights(p, b);
+ for (int k = 0; k < 7; k++)
+ Assert.AreEqual(BitConverter.SingleToInt32Bits(b[k]), BitConverter.SingleToInt32Bits(a[k]), $"point {p} layer {k}: {a[k]} vs {b[k]}");
+ }
+ }
+}
diff --git a/unity/Assets/Tests/EditMode/BiomeBlenderSpeedTests.cs.meta b/unity/Assets/Tests/EditMode/BiomeBlenderSpeedTests.cs.meta
new file mode 100644
index 000000000..e02b57239
--- /dev/null
+++ b/unity/Assets/Tests/EditMode/BiomeBlenderSpeedTests.cs.meta
@@ -0,0 +1,2 @@
+fileFormatVersion: 2
+guid: 818e3413953504498a1a9c3fddcebd8f
\ No newline at end of file