3 ms·
It's almost entirely about performance. Unsafe allows you access to concurrent intrinsics as well as memory layout functions. It's true that it is widely used
by kasey_junk 11y ago
It's almost entirely about performance. Unsafe allows you access to concurrent intrinsics as well as memory layout functions.
It's true that it is widely used, but the people that use it also understood the ramifications of using it. If you were trying to make jvm agnostic code you didn't use it. If you were trying to make code that could run on a variety of jvm versions, you didn't use it.
A lot of what people use it for is being rolled into documented/standard libraries and this is just the migration pains coming out.
- vbezhenar 11y agoWhy those concurrent intrinsics are not available with "safe" code? They are not compatible with Java Memory Model? They are not implemented at some platforms? What's so unsafe about concurrent primitives? There is API in java.util.concurrent.atomic, for example. I understand that Unsafe is used for direct access to memory, but why this direct access is needed? May be there are other ways to solve those problems.
- benjaminjackman 11y agoAFAIK there aren't other ways to get the performance required to be competitive in the financial space. People that are using these libraries don't want to use undocumented, Unsafe code. However, their hands are forced given the races they are competing in. They could switch to C++ but that's not really a great option for firms that have java code bases going back 10+ years. Plus, personally, and for a lot of others, the jvm plus a dash of sun.misc.Unsafe, is a lot better than going over to C++. More concretely, Here are two sets of trading libraries that use unsafe for direct memory access: https://github.com/real-logic/simple-binary-encoding https://github.com/real-logic/simple-binary-encoding https://github.com/OpenHFT https://github.com/OpenHFT (various libraries in here use it, once you drink the Unsafe Kool-aid it's hard to turn back from the performance benefits you obtain). If Oracle just rips off the Unsafe band-aid without a replacement, the business decision for these firms is still sound. They will just stay on 8 and start making migration plans (likely to the C++ bandwagon). However, It sounds like Oracle plans to give a sane alternative which is good and what everyone using Unsafe would prefer (along with deprecating it the meantime to give them some time to adapt). One real world example: The CME (Chicago Mercantile Exchange) reworked the entire way they distributed market data (v3 of their Market Data Protocol) to mirror the cutting edge in performance oriented serialization libraries (e.g. Cap'n Proto). They also commissioned an Open Source library for parsing that data (real-logic SBE), it uses unsafe to approach the performance of C++ code.
- kasey_junk 11y agoExactly. Though I'd add that its not just the financial space that is doing this for performance. Lots of people that are trying to scale high throughput systems use unsafe for similar reasons: Akka for instance uses it: https://github.com/akka/akka/blob/0de9f0ff40fc5e43540df58718ecb1818df35673/akka-actor/src/main/java/akka/dispatch/AbstractBoundedNodeQueue.java https://github.com/akka/akka/blob/0de9f0ff40fc5e43540df58718...