Replacing Unsafe.allocateInstance

Andrew Haley aph at redhat.com
Sat Jun 27 14:52:39 UTC 2015


On 27/06/15 01:54, Henri Tremblay wrote:
> The problems to solve seem to be:
> 
>    1. Find a place to put the new method in the official java.* API
>    2. Make it fast

But Java is designed to stop people from creating objects without a
constructor.  There are several places where this is required in order
to guarantee security.  So I can't see much hope of it ever being part
of the official API.

As to why Unsafe.allocateInstance() is slow, I have to admit that's a
mystery to me.  allocateInstance() should inline quite nicely.  I
suppose it might be a little bit slower if HotSpot can't determine
during compilation the class you're trying to create.  But I'm looking
at UnsafeFactoryInstantiator.java and it should be fine.

The only thing to do is to do some measurements and look at the
generated code.  Mind you, 20ns is still pretty fast.

Andrew.


More information about the jdk9-dev mailing list