Files
sbox-public/engine/Sandbox.Engine/Scene/GameObjectSystems/VerletRopeSystem.cs
Lorenz Junglas 5f088c419e Avoid per-frame KeyValuePair[] allocations in Parallel.ForEach (#4128)
Parallel.ForEach(IEnumerable<T>) allocates KeyValuePair<long, T>[] chunks
internally for work distribution. Passing a List<T> avoids this entirely.
2026-02-23 17:52:20 +01:00

26 lines
661 B
C#

namespace Sandbox;
/// <summary>
/// Simulates VerletRope components in parallel during PrePhysicsStep
/// </summary>
internal sealed class VerletRopeGameSystem : GameObjectSystem
{
private readonly List<VerletRope> _ropes = new();
public VerletRopeGameSystem( Scene scene ) : base( scene )
{
// Listen to StartFixedUpdate to run before physics
Listen( Stage.StartFixedUpdate, -100, UpdateRopes, "UpdateRopes" );
}
void UpdateRopes()
{
_ropes.Clear();
Scene.GetAll<VerletRope>( _ropes );
if ( _ropes.Count == 0 ) return;
var timeDelta = Time.Delta;
Sandbox.Utility.Parallel.ForEach( _ropes, rope => rope.Simulate( timeDelta ) );
}
}