From 65ace6381c2f45e132ddaa3418b11b2bec87c986 Mon Sep 17 00:00:00 2001 From: notgiven688 Date: Mon, 23 Dec 2024 13:49:01 +0100 Subject: [PATCH] Prevent lock contention for arbiters. --- src/Jitter2/World.Detect.cs | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/src/Jitter2/World.Detect.cs b/src/Jitter2/World.Detect.cs index 70b41b7d..2af5b1d0 100644 --- a/src/Jitter2/World.Detect.cs +++ b/src/Jitter2/World.Detect.cs @@ -427,23 +427,24 @@ public void GetArbiter(ulong id0, ulong id1, RigidBody b0, RigidBody b1, out Arb arb = new Arbiter(); } + var h = memContacts.Allocate(true); + arb.Handle = h; + h.Data.Init(b0, b1); + h.Data.Key = arbiterKey; + arb.Body1 = b0; + arb.Body2 = b1; + + Debug.Assert(arb != null && memContacts.IsActive(arb.Handle)); + bool success = arbiters.TryAdd(arbiterKey, arb); if (success) { - var h = memContacts.Allocate(true); - arb.Handle = h; - h.Data.Init(b0, b1); - h.Data.Key = arbiterKey; - arb.Body1 = b0; - arb.Body2 = b1; - - Debug.Assert(arb != null && memContacts.IsActive(arb.Handle)); - deferredArbiters.Add(arb); } else { + memContacts.Free(h); Arbiter.Pool.Push(arb); } }