Replace the concurrent collections of MA0234 with locked collections
What changed
DoNotUseNotYetInitializedStaticFieldAnalyzer (MA0234) kept its per-type state in a ConcurrentBag<FieldReferenceInfo> and a ConcurrentDictionary<IFieldSymbol, bool> used as a set. Both are now a List<T> and a HashSet<T> guarded by a lock.
Why
The analyzer allocates one AnalyzerContext per named type that declares a static field, so these two collections are allocated thousands of times in a large compilation. Both are write-only during the operation actions and enumerated once in the symbol end action — the workload these concurrent collections are the worst at:
ConcurrentBag<T>is built for same-thread add/take via work-stealing thread-local queues. Nothing is ever taken here, so that machinery buys nothing. Each instance allocates aThreadLocal<T>plus per-thread queue infrastructure (andThreadLocalids are a globally managed resource that widens every participating thread's slot table). Enumerating it freezes the bag by taking every per-thread lock and copies the whole content into aList<T>snapshot.ConcurrentDictionary<T, bool>allocates a bucket table plus a lock array sized toEnvironment.ProcessorCounton construction.
A List<T> and a HashSet<T> allocate nothing but the object until the first add, and the adds are rare — they only happen for field references inside static field initializers or assignments in a static constructor — so lock contention is negligible.
Notes for reviewers
lock (_collection)around a plain collection is the pattern already used byClassMustBeSealedAnalyzer,MakeClassStaticAnalyzerandAvoidUnusedInternalTypesAnalyzerin this repository.- Enumeration in
ReportDiagnosticsneeds no lock: Roslyn guarantees the symbol end action runs after all the operation actions registered in the symbol start action. A comment records this. SymbolEqualityComparer.Defaultstill works as theHashSet<IFieldSymbol>comparer throughIEqualityComparer<T>contravariance.- The
fieldDeclarationInfoscache stays aConcurrentDictionary: it is a single, read-heavy instance shared by every context of the compilation, which is what that type is good at. - No behavior change, so no documentation change.
dotnet run --project src/DocumentationGeneratorexits 0 with no markdown changes.
Validation
- Build clean, 0 warnings.
- The 16 MA0234 tests pass on both
roslyn5.9androslyn4.8.