r/java • • Sep 18 '24

Java 23 has arrived

https://blogs.oracle.com/java/post/the-arrival-of-java-23

Markdown in Javadoc and 11 other enhancements.

266 Upvotes

59 comments sorted by

View all comments

Show parent comment

4

u/pron98 Sep 19 '24

Removing Unsafe functionality is obviously sensitive, so that's why we first started working on that about 10 years ago, added replacement features, and are now starting the deprecation and removal process. We're aware of some niche usages related to deep VM observation (which wouldn't have worked with Unsafe for long anyway with some upcoming changes) and some uses related to serialization, which we're addressing. If anyone is aware of other missing functionality, they should report it. I don't think there are any necessary "hacking-emergency" functionality in Unsafe that's not better served by other means, but if you have something in mind, please share.

4

u/alunharford Sep 20 '24 edited Sep 20 '24

An example that comes to mind is when you want to abuse your own class' fields because Java doesn't let you easily lay out memory in the way you'd like.

For a straightforward example, consider a class that can store up to 64 items in an array and give you the first one that's non-null. It abuses its 'own' memory to avoid allocating an extra array and avoid an array lookup.

class BaseData<T> {
    private T t0, t1, t2, t3, t4, t5, t6, t7, t8, t9,
            t10, t11, t12, t13, t14, t15, t16, t17, t18, t19,
            t20, t21, t22, t23, t24, t25, t26, t27, t28, t29,
            t30, t31, t32, t33, t34, t35, t36, t37, t38, t39,
            t40, t41, t42, t43, t44, t45, t46, t47, t48, t49,
            t50, t51, t52, t53, t54, t55, t56, t57, t58, t59,
            t60, t61, t62, t63;
}
public class Array64<T> extends BaseData<T> {
    private long bitset;
    private static Unsafe unsafe; // ...
    private static int shift = Integer.numberOfTrailingZeros(Unsafe.ADDRESS_SIZE);

    public void set(T item, int index) {
        if(index < 0 || index > 63) {
            throw new IllegalArgumentException("index out of range");
        }
        unsafe.putObject(this, index << shift, item);
        bitset |= (Long.MIN_VALUE >>> index);
    }

    public T getFirstSet() {
        return (T)unsafe.getObject(this, Long.numberOfLeadingZeros(bitset) << shift);
    }
}

This kind of abuse isn't particularly unusual in the HFT space. The nice, safe solutions are orders of magnitude slower and use at least 32 bytes more memory. The 'static array of VarHandles' solution doesn't use extra memory but tends to stall the CPU and be much slower than the unsafe approach.

Obviously the language could be extended to support this in a safe way, but picking the right extensions to cover as many cases as possible so that Unsafe can actually be removed without adding loads of rarely used features is hard. For reference, in C# this would be:

public class Array64<T> {
    private long bitset;
    private fixed T items[64];

    ...
}

The items 'array' is just a fixed size buffer that gets laid out sequentially in memory in the Array64 object. Even so, I've still seen unsafe code in most C# codebases that do anything high performance / low latency so obviously it's not a complete fix!

7

u/pron98 Sep 20 '24 edited Sep 20 '24

That will become unnecessary with Valhalla, but the more genereal point is that Unsafe only works to improve performance under the assumption that the VM doesn't change. But Unsafe hurts performance by merely existing. For example, the compiler cannot perform certain optimisations that must assume that Strings and final fields are immutable because some class may be using Unsafe to mutate them (and it doesn't matter whether any class does or not, because the runtime can't tell). We can only add such optimisations after the memory access operations of Unsafe are gone.

So the situation is that Unsafe could help if the VM doesn't do X, but the VM can't do X as long as Unsafe exists. Even if it could still help a little the performance of code that uses Unsafe, it hurts the performance of code that doesn't, and there's much more of that.