Fix lock contention in FluidValue.Create#432
Conversation
|
There is a risk with the swap technique that multiple threads can never get to an agreement with all types in the dictionary. Example with two types, So ideally the only thing to fix would be the |
|
https://docs.microsoft.com/en-us/dotnet/api/system.threading.interlocked.compareexchange?view=net-6.0#System_Threading_Interlocked_CompareExchange_System_Object__System_Object_System_Object_ - exchange only happens if field has same reference as in start, if not, someone did successful update and the current race loser will try again. But can also be changed back to double-checked locking. |
|
I understand now, thanks for the explanation. There might be other places I could use that pattern then, I have used the "switch" technique in some places knowing the limitation I was explaining. This technique solves this quite nicely. |
While doing performance checkup in Orchard (load test profiling agency theme) I noticed that there was quite big lock contention problem in Fluid, it originates from #387 where the logic never assigns anything on cache miss thus making the locking and looping happen every time for types like
ContentItem(non-dictionary type).Fixed issue by using
Interlocked.CompareExchange(no locks needed) and also storing null as result for given type. The dictionary was public, don't know why (Orchard doesn't seem to need it), made it private.Main
This PR