docs(engine-ambient): explain why _components GetOrAdd factory is contention-safe
Reviewer noted the factory may be invoked more than once under contention. Document the analysis inline so a future reader doesn't have to redo it: the discarded instance's mutations land on private fields of a soon-unreachable object, and the only shared sentinel (_noopViewMaterial) is read-only. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -225,6 +225,10 @@ namespace UnityEngine
|
||||
var fresh = new System.Collections.Concurrent.ConcurrentDictionary<Type, object>();
|
||||
map = System.Threading.Interlocked.CompareExchange(ref _components, fresh, null) ?? fresh;
|
||||
}
|
||||
// GetOrAdd may invoke the factory more than once under contention; only one result wins.
|
||||
// Safe here: the discarded instance has its _go set to `this` (private write to a soon-
|
||||
// unreachable object) and WireComponentFields only assigns to the new tree's own private
|
||||
// fields. The shared _noopViewMaterial sentinel below is read-only. No global state leaks.
|
||||
return map.GetOrAdd(t, ty =>
|
||||
{
|
||||
object inst;
|
||||
|
||||
Reference in New Issue
Block a user