Lock Contention when calling ToString from multiple threads
alitas opened this issue · comments
The default ToString
method on a lot of the NodaTime classes utilize PatternBclSupport
which in turn tries to read cached fields from NodaFormatInfo
. However, since the reads share the same lock, this causes lock contention whenever multiple threads try to serialize dates.
As noted in #1756, one attractive option is to make sure that any NodaFormatInfo
which is for a read-only culture (so likely to be reused) is eagerly initialized, so we don't need the lock.
I've started a draft implementation of that - but run into an issue where initializing the patterns relies on initializing DateTimeZone, which in turn uses (indirectly) NodaFormatInfo.InvariantCulture in FixedDateTimeZone.MakeId.
We've had type initialization problems like this before - I want to take another pass at isolating the "core" aspects from anything textual, when it comes to type initialization. I'm not sure how I can test that, but I can at least inspect the code manually and flag up anything I find.
Basically, I still think this is a good way to proceed (and some early tests with hacks to avoid the MakeId issue show promising performance results) but it'll take a little while.