Frame time on a big farm: batched grass, sleeping bundles, no light probes

Measured in a real 2560x1440 window on the player's Mommy save (RTX 4090):
the GPU took ~5 ms, the main thread the rest.

- Grass: a fully mown sector draws only its floor (one draw, not up to six);
  untouched and fully mown sectors share a mow map look, so the fur pile
  draws them all in one instanced call per pile (GC_SECTOR_BUFFER, sector
  frames from a structured buffer). Grass draws 1112 -> 337 at the base,
  876 -> 439 in the meadow.
- Bundles: loose ones sleep when they come to rest (our gravity kept ~600
  bodies awake), ones on belts skip the ground checks, and all of them are
  stepped from one CargoTick instead of two Unity messages each.
- Belts leave a jammed line alone and only look every few steps whether it
  can move; splitters test distance before transforming every bundle.
- Light probes off on everything (the planet has none) and the GPU Resident
  Drawer off: both cost CPU per moving renderer for nothing.
- -demogpu N -onscreen measures frame time with one feature off at a time.

Base view 17-19 ms -> 14-15 ms, meadow 14-16 ms -> 11.5-12 ms.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
NiemandKG
2026-11-09 20:01:02 +06:00
parent 80271aea72
commit 57d0f4d31a
17 changed files with 360 additions and 20 deletions

View File

@ -96,10 +96,14 @@ namespace GrassCutter
bool seen = false;
Cargo inJunction = null;
float side = 0f;
var here = transform.position;
foreach (var c in Cargo.All)
{
if (!c) continue;
var l = transform.InverseTransformPoint(c.transform.position);
var at = c.transform.position;
// most of a farm's bundles are nowhere near: the cheap test first
if ((at - here).sqrMagnitude > 4f) continue;
var l = transform.InverseTransformPoint(at);
if (Mathf.Abs(l.x) > 0.55f || Mathf.Abs(l.z) > 0.55f || l.y < -0.2f || l.y > 1.4f) continue;
if (l.z < -0.2f) continue;
inJunction = c;

View File

@ -48,6 +48,7 @@ namespace GrassCutter
public override void OnNetworkSpawn()
{
All.Add(this);
CargoTick.Ensure();
if (Time.timeSinceLevelLoad > 3f) Tween.Scale(transform, Vector3.one * 0.3f, Vector3.one, 0.35f, Ease.OutBack);
}
@ -65,10 +66,14 @@ namespace GrassCutter
float _fallV;
void FixedUpdate()
/// <summary>One physics step, called by <see cref="CargoTick"/> for every bundle at once: two thousand Unity
/// FixedUpdate messages cost more than the work in them.</summary>
internal void Step()
{
if (!IsSpawned) return;
var rb = _g.Body;
// riding a belt or lying still: nothing below needs looking at (most of a big farm's bundles, every step)
if (Belt || (rb && !rb.isKinematic && rb.IsSleeping())) { _fallV = 0f; _wasInGrass = !Belt; return; }
var p = transform.position;
// tossed into a depot

View File

@ -0,0 +1,32 @@
using UnityEngine;
namespace GrassCutter
{
/// <summary>
/// Steps every bundle and every grabbable thing from one FixedUpdate. A big farm has two thousand bundles, and a Unity
/// message per object per physics step (two scripts each) cost more than what they do, which is mostly nothing.
/// </summary>
public class CargoTick : MonoBehaviour
{
static CargoTick s_on;
public static void Ensure()
{
if (s_on) return;
var go = new GameObject("CargoTick");
DontDestroyOnLoad(go);
s_on = go.AddComponent<CargoTick>();
}
void FixedUpdate()
{
// backwards: a step can hand a bundle to a depot, which takes it off the list
var g = Grabbable.All;
for (int i = g.Count - 1; i >= 0; i--)
if (i < g.Count && g[i]) g[i].Step();
var c = Cargo.All;
for (int i = c.Count - 1; i >= 0; i--)
if (i < c.Count && c[i]) c[i].Step();
}
}
}

View File

@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: c675567074ef89b49b1f9073c8ee1535

View File

@ -46,6 +46,9 @@ namespace GrassCutter
public float blend;
/// <summary>Has it settled which way it's going?</summary>
public bool set;
/// <summary>Where it was last put, and for how many steps it hasn't moved: a jammed line is left alone.</summary>
public float placedD = -1f;
public int still;
}
struct Link
@ -307,6 +310,7 @@ namespace GrassCutter
int i = 0;
while (i < Items.Count && Items[i].d > d) i++;
Items.Insert(i, s);
_idle = 0;
}
/// <summary>Server: take an item off this belt (a player grabbed it, it got despawned).</summary>
@ -334,7 +338,10 @@ namespace GrassCutter
void FixedUpdate()
{
if (!Server || !Piece || !Piece.IsSpawned || Items.Count == 0) return;
float dt = Time.fixedDeltaTime;
// a line that hasn't moved for a while only looks again every few steps whether the front can go on
if (_idle > 40 && ++_idleSkip % 6 != 0) return;
bool moved = false;
float dt = Time.fixedDeltaTime * (_idle > 40 ? 6f : 1f);
float step = Speed * dt;
for (int i = 0; i < Items.Count; i++)
{
@ -347,16 +354,25 @@ namespace GrassCutter
}
float L = Length(s.route);
float limit = Room(i, ref L);
if (s.d < limit) s.d = Mathf.Min(s.d + step, limit);
if (s.d < limit - 1e-5f) { s.d = Mathf.Min(s.d + step, limit); moved = true; }
if ((i == 0 || RouteCount > 1) && s.d >= L - 1e-4f && HandOff(s))
{
Items.RemoveAt(i--);
moved = true;
continue;
}
// stuck behind the one ahead: once it has come square and settled there is nothing to move
if (s.blend >= 1f && Mathf.Abs(s.d - s.placedD) < 1e-5f) { if (++s.still > 40) continue; }
else s.still = 0;
Place(s, dt);
s.placedD = s.d;
if (s.blend < 1f) moved = true;
}
_idle = moved ? 0 : _idle + 1;
}
int _idle, _idleSkip;
/// <summary>How far an item can go: to the turn until it has chosen, then the end of its route, and never into the one ahead.</summary>
float Room(int i, ref float L)
{

View File

@ -44,6 +44,7 @@ namespace GrassCutter
public override void OnNetworkSpawn()
{
CargoTick.Ensure();
All.Add(this);
Holder.OnValueChanged += (_, h) => ApplyHeldLayer(h != Nobody);
Scale.OnValueChanged += (_, s) => transform.localScale = _baseScale * s;
@ -155,15 +156,28 @@ namespace GrassCutter
if (Body && !Body.isKinematic && !IsHeld) Body.AddForce(velocity, ForceMode.VelocityChange);
}
void FixedUpdate()
int _still;
Cargo _cargo;
bool _cargoLooked;
/// <summary>One physics step, from <see cref="CargoTick"/>.</summary>
internal void Step()
{
if (!IsSpawned || !IsOwner || !Body || Body.isKinematic) return;
// at rest it sleeps: our own gravity, pushed every step, would keep it awake for ever, and a big farm has
// hundreds of bundles lying about. A knock, a grab or a belt wakes it again.
if (Body.IsSleeping()) return;
var p = Body.position;
var up = Planet.Up(p);
if (!IsHeld) Body.AddForce(-up * GameDefs.LowGravity, ForceMode.Acceleration);
// there is no collider under the grass: the soil is a number
var c = GetComponent<Cargo>();
float below = Planet.Altitude(p) - (c ? c.halfHeight : 0.3f);
if (!_cargoLooked) { _cargo = GetComponent<Cargo>(); _cargoLooked = true; }
float below = Planet.Altitude(p) - (_cargo ? _cargo.halfHeight : 0.3f);
if (!IsHeld && Body.linearVelocity.sqrMagnitude < 0.004f && Body.angularVelocity.sqrMagnitude < 0.01f)
{
if (++_still > 25) { _still = 0; Body.Sleep(); return; }
}
else _still = 0;
if (below >= 0f) return;
Body.position = p - up * below;
var v = Body.linearVelocity;