Avoid per-instance lock object in InterruptibleLazy and DelayInitArrayMap - #20088
Avoid per-instance lock object in InterruptibleLazy and DelayInitArrayMap#20088auduchinok wants to merge 1 commit into
Conversation
…yMap Both types allocated a dedicated `syncObj = obj()` for their one-time initialisation. These instances are internal and never locked externally, and there are enough of them (one per lazy IL member, per ILTypeDefs / ILMethodDefs, etc.) that the extra bare System.Object adds up to tens of MB on a large project. Lock on `this` instead and drop the field. Measured on a single-file FCS check against a project with ~486 references: bare System.Object instances dropped from ~1,000,000 to ~29,000 (~-22 MB). Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
25074ee to
bdb5b04
Compare
|
The way |
Thanks for the suggestion! The problem is it still keeps the memory for that field, and for many thousands of types it's becoming a bit too much. |
|
Here's an interesting discovery about how this PR overlaps with #20090:
|
I've been measuring memory allocations on some my changes and found out that these sync objects retail a lot of memory due to usages like in
ImportILTypeDef. This is a debatable change: it makes things more dangerous (an issue could be if someone locks on the lazy itself) but at the same it's an implementation detail inside the compiler which is fairly advanced, so I hope that it can be used responsibly.