|
|
|
Back to newsletter 191 contents
Last month I listed almost all the JEPs that have performance impact in Java 9. One JEP that I didn't list was 285: Spin-Wait Hints. I don't think it's one that many people need to know about, but for those of you who want to know about the cutting edge of ultra-low latency, it's interesting. First I'll need to explain where it's useful.
The best way to consider spin-waits is to think of an ideal scenario where you'd want to use them. So here's the scenario:
(Note: attaching a thread to a specific core is not difficult (eg using OS commands or with libraries like OpenHFT's Java-Affinity), but ensuring that nothing else runs on that core is challenging (eg see Mark Price's talk on trying to achieve that; or if you're in a hurry my extracted tips from that talk gives summary details).
The scenario is waiting for some state to change; and we want to respond to that change as fast as we possibly can, so we just repeatedly check the condition doing nothing else - looping on the condition check (the "spin" part of the spin-wait). Note this is an anti-pattern - if you had too much in the way of spin-wait, your CPU could be fully utilised without doing very much at all. But to achieve that "as soon as possible" hard requirement, it can be worth it.
Okay, that's spin-wait but about the JEP? So if the JVM knew it was doing a spin-wait loop, on some hardware it is possible to optimize the inter-thread comms a little. But it's difficult for the JVM to know that the application is spin-waiting rather than just looping doing something. The JEP provides a way for the application to tell the JVM it's spin-waiting by calling the Thread.onSpinWait() method in the body of the loop. On x86, this gives a 20 nanosecond improvement to the latency in responding to the state change in the spin-wait.
When 20 nanoseconds per request matter to you, you need to know about Thread.onSpinWait().
Now on to our usual links to tools, articles, news, talks, blogs. And if you need the tips from this month's articles and talks, as ever they are extracted into this month's tips page.
Java performance tuning related news.
Java performance tuning related tools.
Back to newsletter 191 contents