Skip to content

Commit b50a83a

Browse files
committed
Use 'Volatile.Write' instead for better perf
1 parent f877f7c commit b50a83a

File tree

1 file changed

+5
-2
lines changed

1 file changed

+5
-2
lines changed

src/CommunityToolkit.Mvvm.SourceGenerators/Diagnostics/Analyzers/WinRTObservablePropertyOnFieldsIsNotAotCompatibleAnalyzer.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,11 @@ public override void Initialize(AnalysisContext context)
6868
fieldSymbol.ContainingType,
6969
fieldSymbol.Name));
7070

71-
// Notify that we did produce at least one diagnostic
72-
_ = Interlocked.CompareExchange(ref firstObservablePropertyAttribute, observablePropertyAttribute, null);
71+
// Notify that we did produce at least one diagnostic. Note: callbacks can run in parallel, so the order
72+
// is not guaranteed. As such, there's no point in using an interlocked compare exchange operation here,
73+
// since we couldn't rely on the value being written actually being the "first" occurrence anyway.
74+
// So we can just do a normal volatile read for better performance.
75+
Volatile.Write(ref firstObservablePropertyAttribute, observablePropertyAttribute);
7376
}
7477
}, SymbolKind.Field);
7578

0 commit comments

Comments
 (0)