Smoother drives across the meadow: no more hitches opening sectors

- Sector maps are put away on a worker thread, and trimmed in batches
- A far sector's texture only needs its look; at most four new ones a frame
- Fresh maps fill rows between their borders instead of testing every cell
- Rocks are marked with plain numbers; marks come back on unpacked maps
- Worked-overlay sort uses float keys; idle belts look on staggered steps
- Chase camera cast no longer allocates; perf demo drives and runs too

Drive phase on a late save: p99 33 -> 16 ms, frames over 20 ms 242 -> 7.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
NiemandKG
2026-11-10 12:59:16 +06:00
parent 29485df9e1
commit 172c3b978d
6 changed files with 271 additions and 76 deletions

View File

@ -16,6 +16,44 @@ namespace GrassCutter
/// every profiler marker (a development build has one per script Update), the slow frames and what was busy in them.
/// Writes perf.txt beside the shots. Needs a development build for the per-script markers.
/// </summary>
IEnumerator Phase(StringBuilder outp, List<(string name, ProfilerRecorder rec)> recs, ProfilerRecorder loop, string label, float seconds, System.Action begin, System.Action<int> each)
{
var total = new Dictionary<string, double>();
var spikes = new List<(int frame, float at, double ms, string top)>();
var frames = new List<double>();
begin();
float t0 = Time.realtimeSinceStartup, until = t0 + seconds;
int f = 0;
while (Time.realtimeSinceStartup < until)
{
yield return null;
f++;
each(f);
double frameMs = loop.Valid ? loop.LastValue / 1e6 : Time.unscaledDeltaTime * 1000.0;
frames.Add(frameMs);
var busy = new List<(string, double)>();
foreach (var (name, rec) in recs)
{
if (!rec.Valid) continue;
double ms = rec.LastValue / 1e6;
if (ms <= 0 || ms > 1000) continue;
total[name] = (total.TryGetValue(name, out var t) ? t : 0) + ms;
if (frameMs > 20 && ms > 1 && name != "PlayerLoop") busy.Add((name, ms));
}
if (frameMs > 20)
spikes.Add((f, Time.realtimeSinceStartup - t0, frameMs, string.Join(", ", busy.OrderByDescending(b => b.Item2).Take(14).Select(b => $"{b.Item1} {b.Item2:0.0}"))));
}
frames.Sort();
double Pct(double p) => frames.Count == 0 ? 0 : frames[Mathf.Clamp((int)(frames.Count * p), 0, frames.Count - 1)];
outp.AppendLine();
outp.AppendLine($"=== {label}: {f} frames, mean {frames.Average():0.0} ms, median {Pct(0.5):0.0}, p95 {Pct(0.95):0.0}, p99 {Pct(0.99):0.0}, max {frames.LastOrDefault():0.0}; {spikes.Count} over 20 ms, {spikes.Count(s => s.ms > 50)} over 50 ms");
foreach (var kv in total.Where(kv => kv.Key.StartsWith("GC:") || kv.Key.StartsWith("GC.") || kv.Key.StartsWith("Physics") || kv.Key.StartsWith("Gfx.") || kv.Key.StartsWith("Loading") || kv.Key.Contains("Upload")).OrderByDescending(kv => kv.Value).Take(40))
outp.AppendLine($"{kv.Value / f,8:0.000} {kv.Key}");
outp.AppendLine("— slow frames —");
foreach (var s in spikes.OrderByDescending(s => s.ms).Take(20))
outp.AppendLine($"#{s.frame} t{s.at:0.0} {s.ms:0.0} ms: {s.top}");
}
IEnumerator Perf(int slot)
{
SaveSlots.Current = slot - 1;
@ -54,44 +92,35 @@ namespace GrassCutter
var loop = recs.FirstOrDefault(r => r.name == "PlayerLoop").rec;
outp.AppendLine($"recording {recs.Count} markers, PlayerLoop {(loop.Valid ? "ok" : "MISSING")}");
var total = new Dictionary<string, double>();
var spikes = new List<(int frame, double ms, string top)>();
var frames = new List<double>();
float until = Time.realtimeSinceStartup + 40f;
int f = 0;
// walk around a little, as a player would
Ctl.BotMove = new Vector2(0.3f, 1f);
while (Time.realtimeSinceStartup < until)
{
yield return null;
f++;
if (f % 120 == 0) Ctl.BotMove = new Vector2(-Ctl.BotMove.x, Ctl.BotMove.y);
double frameMs = loop.Valid ? loop.LastValue / 1e6 : Time.unscaledDeltaTime * 1000.0;
frames.Add(frameMs);
var busy = new List<(string, double)>();
foreach (var (name, rec) in recs)
{
if (!rec.Valid) continue;
double ms = rec.LastValue / 1e6;
if (ms <= 0 || ms > 1000) continue;
total[name] = (total.TryGetValue(name, out var t) ? t : 0) + ms;
if (frameMs > 25 && ms > 1 && ms < 1000 && (name.StartsWith("GC:") || name.StartsWith("Physics.Simulate") || name.StartsWith("Semaphore") || name.StartsWith("GC.") || name.StartsWith("Gfx."))) busy.Add((name, ms));
}
if (frameMs > 25)
spikes.Add((f, frameMs, string.Join(", ", busy.OrderByDescending(b => b.Item2).Take(10).Select(b => $"{b.Item1} {b.Item2:0.0}"))));
}
// three things a player does: walk about the base, drive a mower across fresh meadow, run through the meadow
yield return Phase(outp, recs, loop, "walk at the base", 25f, () => Ctl.BotMove = new Vector2(0.3f, 1f), f => { if (f % 120 == 0) Ctl.BotMove = new Vector2(-Ctl.BotMove.x, Ctl.BotMove.y); });
Ctl.BotMove = Vector2.zero;
var ride = Vehicle.All.Where(v => v && v.Def.width > 0f).OrderByDescending(v => v.Def.width).FirstOrDefault();
if (ride)
{
var far = Planet.FromBase(90f, 160f);
var up = far;
ride.ServerPut(Planet.OnSurface(far) + up * 1.2f, Quaternion.LookRotation(Vector3.ProjectOnPlane(Vector3.forward, up), up));
yield return new WaitForSeconds(1f);
Me.Teleport(ride.transform.position + ride.transform.right * 3f);
yield return new WaitForSeconds(0.5f);
ride.Interact();
yield return new WaitForSeconds(1.5f);
ride.Tank.Value = 0f;
yield return Phase(outp, recs, loop, $"drive the {ride.kind} through the meadow", 40f, () => Ctl.BotMove = new Vector2(0.15f, 1f), f => { });
Ctl.BotMove = Vector2.zero;
ride.RequestExit();
yield return new WaitForSeconds(1.5f);
}
else outp.AppendLine("no mower to drive");
var run = Planet.FromBase(-150f, 120f);
Me.Teleport(Planet.OnSurface(run) + run * 0.3f);
yield return new WaitForSeconds(1f);
yield return Phase(outp, recs, loop, "run through the meadow", 25f, () => { Ctl.BotMove = new Vector2(0f, 1f); Ctl.BotSprint = true; }, f => { if (f % 200 == 0) Ctl.BotMove = new Vector2(Ctl.BotMove.x > 0 ? -0.4f : 0.4f, 1f); });
Ctl.BotMove = Vector2.zero;
Ctl.BotSprint = false;
foreach (var (_, rec) in recs) rec.Dispose();
frames.Sort();
double Pct(double p) => frames.Count == 0 ? 0 : frames[Mathf.Clamp((int)(frames.Count * p), 0, frames.Count - 1)];
outp.AppendLine($"{f} frames: mean {frames.Average():0.0} ms, median {Pct(0.5):0.0}, p95 {Pct(0.95):0.0}, p99 {Pct(0.99):0.0}, max {frames.LastOrDefault():0.0}; {spikes.Count} over 25 ms");
outp.AppendLine("— mean ms a frame, top markers —");
foreach (var kv in total.OrderByDescending(kv => kv.Value).Take(70))
outp.AppendLine($"{kv.Value / f,8:0.000} {kv.Key}");
outp.AppendLine("— slow frames —");
foreach (var s in spikes.OrderByDescending(s => s.ms).Take(25))
outp.AppendLine($"#{s.frame} {s.ms:0.0} ms: {s.top}");
float render = 0f;
yield return BotCapture.FrameCost(40, v => render = v);
outp.AppendLine($"render (camera at the base, GPU waited for): {render:0.0} ms");