RFR: 8259925: [Vector API] Unreasonable IndexOutOfBoundsException message when length < vlen

Jie Fu jiefu at openjdk.java.net
Wed Jan 20 09:39:10 UTC 2021


On Tue, 19 Jan 2021 21:40:24 GMT, Paul Sandoz <psandoz at openjdk.org> wrote:

>> Hi all,
>> 
>> For this reproducer:
>> 
>> import jdk.incubator.vector.ByteVector;
>> import jdk.incubator.vector.VectorSpecies;
>> 
>> public class Test {
>>     static final VectorSpecies<Byte> SPECIES_128 = ByteVector.SPECIES_128;
>>     static byte[] a = new byte[8];
>>     static byte[] b = new byte[8];
>> 
>>     public static void main(String[] args) {
>>         ByteVector av = ByteVector.fromArray(SPECIES_128, a, 0);
>>         av.intoArray(b, 0);
>>         System.out.println("b: " + b[0]);
>>     }
>> }
>> 
>> The following IndexOutOfBoundsException message (length -7) is unreasonable.
>> 
>> Exception in thread "main" java.lang.IndexOutOfBoundsException: Index 0 out of bounds for length -7
>> 
>> It might be better to fix it like this.
>> 
>> Exception in thread "main" java.lang.IndexOutOfBoundsException: Index 0 out of bounds for length 0
>> 
>> Thanks.
>> Best regards,
>> Jie
>
> That change may cause performance issues. I would recommend leaving as is for now even through the error message is not great. Bounds checking is quite sensitive and WIP. Notice that we also have an option to call `Objects.checkFromIndexSize` which expresses the intent more accurately, but that is currently less optimal (at least it was when i last checked since it is non an intrinsic).

Thanks @PaulSandoz for your review and comments.

Updated:
 - The performance issue has been fixed since there is no more operation for common cases.
 - The readability of OOB exception msg has been improved by following the style of Objects.checkFromIndexSize.
 - Less code generated (several blocks of code were optimized out for the Test::test method below).

import jdk.incubator.vector.ByteVector;
import jdk.incubator.vector.VectorSpecies;

public class Test {
    static final VectorSpecies<Byte> SPECIES_128 = ByteVector.SPECIES_128;
    static byte[] a = new byte[88];
    static byte[] b = new byte[88];

    public static void test() {
        ByteVector av = ByteVector.fromArray(SPECIES_128, a, 0);
        av.intoArray(b, 0);
    }

    public static void main(String[] args) {
        for (int i = 0; i < 100000; i++) {
            test();
        }
        System.out.println("b: " + b[0]);
    }
}

What do you think of it?
Thanks.

-------------

PR: https://git.openjdk.java.net/jdk/pull/2127


More information about the core-libs-dev mailing list