Question -- Are null-restricted arrays a possible future?
-
liangchenblue at gmail.com
Sat Jan 13 06:39:50 UTC 2024
Hello Alayachew,
Before we answer this question, let's first look at arrays, both in Java
and in other more native languages like C.
We all know all Java arrays are stored in heap and variably-sized, while in
native languages like C, there are stack-based arrays, but they have a
constant length at compile time and can be non-null.
To make an array field itself to be non-null, it most likely will be
handled as part of the more general-purpose non-null object pointer
restriction; since in the inlined/flattened layout, the array field is
going to be a pointer to the heap, like other non-value objects.
Compared to regular objects, there are 2 ways to achieve a non-null array:
1. You can probably generate a value record with a fixed number of fields
to serve as a fixed-size array. And this is effectively the same as
constant-sized arrays offered by C for stack usages, and Valhalla will be
able to handle it like regular value classes (though it might refuse to
inline because the object is too big)
2. All Java arrays have a zero-length value for its own type; we can
potentially designate, say, any new Class[0] as the non-null default value
of a Class[]. But this will hurt the existing identity assumptions around
zero-length arrays.
Also make note that Java arrays are polymorphic, i.e. you can assign a new
Class[0] to a field of type Object[] or ConstantDesc[], while value classes
are not. Alternative 1 sacrifices this polymorphism, while alternative 2
might bring confusion over a new Class[0] and a new Object[0] in an
Object[] field, and I'm not quite sure about the impact on their heap
layout yet.
On Fri, Jan 12, 2024 at 6:55 PM David Alayachew <davidalayachew at gmail.com>
wrote:
> Hello Valhalla Dev Team,
>
> I see the JEP Draft for Null-Restricted Value Class Types (
> https://openjdk.org/jeps/8316779), and it looks beautiful. I am extremely
> excited at the possibilities.
>
> At the very bottom of the draft, there is a tiny snippet --- "More general
> support for nullness features will be explored in a future JEP."
>
> Any chance that we could be looking at arrays that cannot be null?
>
> And to be clear, I am not saying arrays that cannot contain null. This JEP
> Draft already confirms that that is not only a possibility, but might very
> well become reality (only if this draft goes live, and then the feature
> exits preview into GA).
>
> No, I mean that the array itself cannot be null. Any possibility? And
> apologies if the answer is obvious. I don't want to make any assumptions
> here, since I am still wrapping my mind around how Valhalla made all of
> this stuff possible. I figured it's better to just ask.
>
> Thank you for your time and help!
> David Alayachew
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.openjdk.org/pipermail/valhalla-dev/attachments/20240113/d5a122c3/attachment.htm>
More information about the valhalla-dev
mailing list