Wouldn't this be nice?
Reinier Zwitserloot
reinier at zwitserloot.com
Thu Nov 12 07:37:45 PST 2009
Yes, java should absolutely gain @NonNull as a JVM primitive, and the JVM
should be smart enough to treat @NonNull PrimitiveWrapper as primitives, so
that you can for example create:
List<@NonNull Byte> list = new ArrayList<>();
and have it be as fast as a byte array, and take ~1 byte per byte stored
instead of ~24 bytes per byte stored which happens when you try this now, on
a 64-bit VM. (8 bytes for the byte itself, 8 bytes for the reference stored
in the array, and 8 bytes overhead for the object, which is probably
lowballing it).
Practically speaking though, that's very hard to get right. The hot spot
compiler is not going to be able to figure out that e.g. ArrayList's 'new
Object[size]' array construction should actually make a byte[] array unless
there's reification, and that's just a glimmer of the complications you're
going to run into with this.
Keeping it somewhat simpler, the hotspot compiler might just be able to
recognize that this flow is happening:
A calls method B, which has called invokestatic on method:
java/lang/Character - valueOf - (C)Ljava/lang/Character;
right before every ARETURN instruction.
A, immediately after invoking method B, always runs invokevirtual on method:
java/lang/Character - charValue - ()C
If this is true, then it should always be legal to ignore both method calls;
they are each other's opposite.
No need for JVM awareness of @NonNull annotations; if the above situation
occurs, then the Character object in question that is never made could never
have been null.
--Reinier Zwitserloot
On Thu, Nov 12, 2009 at 4:08 PM, Mark Mahieu <markmahieu at googlemail.com>wrote:
> And back again:
>
> char -> Character -> char
>
> or perhaps an Iterable<@NonNull Character> would be more interesting:
>
> char -> @NonNull Character -> char
>
>
> Which begs the question - if one were to optimise this, would the language
> spec or the VM be a better place to do so?
>
>
> Mark
>
>
>
>
> On 12 Nov 2009, at 14:39, Reinier Zwitserloot wrote:
>
> char to Character, obviously.
>>
>> --Reinier Zwitserloot
>>
>>
>> On Thu, Nov 12, 2009 at 1:48 PM, Ulf Zibis <Ulf.Zibis at gmx.de> wrote:
>>
>> Sorry for missing the mailing list by CC:
>>>
>>>
>>> Am 11.11.2009 20:12, Joshua Bloch schrieb:
>>>
>>>> Ulf,
>>>>
>>>> Presumably you mean interface CharSequence implements
>>>> Iterable<Character>.
>>>>
>>> Yes right
>>>
>>>> But I really want to avoid the boxing and unboxing.
>>>>
>>> Can you explain where we have boxing and unboxing here?
>>>
>>> Thanks,
>>> Ulf
>>>
>>>
>>>> Regards,
>>>>
>>>> Josh
>>>>
>>>> On Wed, Nov 11, 2009 at 11:00 AM, Ulf Zibis <Ulf.Zibis at gmx.de
>>>> <mailto:Ulf.Zibis at gmx.de>> wrote:
>>>>
>>>> Am 11.11.2009 17:46, Joshua Bloch schrieb:
>>>>
>>>> Paulo,
>>>>
>>>> I have a hard time intuiting the semantics by just looking a
>>>> the statement.
>>>> What I'm really sorry that I didn't put into the original
>>>> statement is:
>>>>
>>>> for (char c : myString)
>>>>
>>>>
>>>> Another Idea:
>>>>
>>>> interface CharSequence implements Iterable
>>>>
>>>> -Ulf
>>>>
>>>>
>>>>
>>>>
>>>
>>>
>>>
>>
>
More information about the coin-dev
mailing list