Helco / zzio

Zanzarah - WIP modding tools and engine remake
MIT License
14 stars 3 forks source link

Reduce per-frame memory allocations #368

Open Helco opened 3 months ago

Helco commented 3 months ago

Some points where we can improve (per-frame) memory allocations. Many areas are only debug-relevant but developer experience is important as well...

Release builds

Collider enumerator

Both WorldCollider as well as TreeCollider are based on generator functions which always allocate due to the enumerator class. This will worsen code quality but is probably necessary

Fixed in #371.

Collider coarse intersectable

We usually know the exact type for the coarse intersectables but do not use them (e.g. AtomicTreeCollider will always use Box while GeometryNaiveCollider will always use Sphere). Due to the interface this currently causes a boxing allocation

Fixed in #371.

RW texture name lookup

For e.g. FindActorFloorCollisions we have to traverse RWBS from a triangle ID to the texture name. As RWBS traversal currently allocates (and will always be slow) we might want to create a cache or some other amortizing lookup to avoid this.

StaticMesh.GetSubMeshRange

Unfortunately we allocate up to four delegates per call, which due to its high use adds up. Maybe keep this on the back burner for now.

Various math functions

Various math functions use allocations for IEnumerable and/or LINQ usage. These include:

Debug builds

ECSExplorer

The ECSExplorer is especially bad in terms of allocations allocating delegates, LINQ objects and strings in GetEntityGroup

Asset ToString

Some asset types (e.g. AnimationAsset or ActorAsset) allocate in their ToString function which is used in the asset registry explorer. We should (lazily?) prepare those strings once.

Fixed in a025474005bf7dac7a05f496cbd5d0cd7d64bfdd

DefaultEcs.Safe

Due to the safe DefaultEcs variant we allocate delegates in ComponentPool<T>.AsComponents which is a lot. We should reduce that.