<div dir="ltr">Thanks for the great tip Ron.<div><br></div><div>I had a look at how spinning is performed for Object monitors (<a href="https://github.com/openjdk/jdk/blob/master/src/hotspot/share/runtime/objectMonitor.cpp#L1790">https://github.com/openjdk/jdk/blob/master/src/hotspot/share/runtime/objectMonitor.cpp#L1790</a>) in order to get an idea of how much spinning should be performed before blocking. Judging by the adaptive heuristics, it's obviously quite a complex topic, especially in the general case. Despite that, I wondered if a fixed number of spin iterations would suffice in my specific use-case given that it is a single hot critical section with a well defined fixed length. Sure enough a few experiments revealed that spinning for 200-500 iterations seemed to get the best results for new and old multi-core hardware, so I now have performance back on a par with Object monitors.</div><div><br></div><div>I will be able to revert back to using Object monitors once the recent monitor support has been officially released, because the Object monitor spin heuristics are more likely to work well across a wider range of hardware both now and in the future.</div><div><br></div><div>Cheers,</div><div>Matt</div><div><br></div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Sat, 29 Jun 2024 at 15:54, Ron Pressler <<a href="mailto:ron.pressler@oracle.com">ron.pressler@oracle.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><br>
<br>
> On 21 Jun 2024, at 16:57, Matthew Swift <<a href="mailto:matthew.swift@gmail.com" target="_blank">matthew.swift@gmail.com</a>> wrote:<br>
> <br>
> I know that ReentrantLock is supposed to be a little bit less efficient in highly contended situations, but I was surprised by an 8-10% impact. Is that expected?<br>
<br>
<br>
I wouldn’t say at all that it’s supposed to be slightly less efficient but rather, as Alan mentioned, the behaviour of ReentrantLock and native monitors is a little different — monitors spin for a bit before blocking. The reason this isn’t the default for ReentrantLock is that this may or may not be what you want, depending on circumstance. However, the ReentrantLock API supports adding spinning manually with a loop calling ReentrantLock.tryLock in the condition and Thread.onSpinWait in the body.<br>
<br>
— Ron</blockquote></div>