-
Enhancement
-
Resolution: Unresolved
-
P4
-
None
-
None
-
None
There is a lot of noisy code in classes like `AbstractList` and `ArrayList` which performed index checking the 1990's way. As a cleanup, it should be refactored to use `Objects.checkFromIndexSize` and its two brothers, or even the corresponding methods in `jdk.internal.util.Preconditions`.
There would be at least two benefits:
1. The JIT would pay more attention to optimizing the checks.
2. Ad hoc exception detection formatting code could be reduced.
Example:
```
--- a/src/java.base/share/classes/java/util/ArrayList.java
+++ b/src/java.base/share/classes/java/util/ArrayList.java
@@ -751,16 +751,24 @@ public class ArrayList<E> extends AbstractList<E>
* A version of rangeCheck used by add and addAll.
*/
private void rangeCheckForAdd(int index) {
- if (index > size || index < 0)
- throw new IndexOutOfBoundsException(outOfBoundsMsg(index));
+ //Objects.checkFromIndexSize(index, 0, size); // 0 means index==size is OK
+ // Error message: "Range [$index, $index + 0) out of bounds for length $size"
+ //Objects.checkFromToIndex(index, index, size); // this also works, but is harder on JIT
+ // Suboptimal message customizable via the internal Preconditions API.
+ jdk.internal.util.
+ Preconditions.checkFromIndexSize(index, 0, size, ArrayList::outOfBoundsMsg4jiuP);
}
/**
* Constructs an IndexOutOfBoundsException detail message.
* Of the many possible refactorings of the error handling code,
* this "outlining" performs best with both server and client VMs.
+ * FIXME: This formatting logic is duplicated at least five times
+ * in this package. Share it as a package-private method in
+ * somewhere like AbstractList or Objects.
*/
- private String outOfBoundsMsg(int index) {
+ private String outOfBoundsMsg4jiuP(String ignore, List<Number> indexZeroSize) {
+ Number index = indexZeroSize.get(0), size = indexZeroSize.get(2);
return "Index: "+index+", Size: "+size;
}
```
The above example is chosen to show how an *inclusive* range check may be done. The error message can be adjusted in the way shown.
There would be at least two benefits:
1. The JIT would pay more attention to optimizing the checks.
2. Ad hoc exception detection formatting code could be reduced.
Example:
```
--- a/src/java.base/share/classes/java/util/ArrayList.java
+++ b/src/java.base/share/classes/java/util/ArrayList.java
@@ -751,16 +751,24 @@ public class ArrayList<E> extends AbstractList<E>
* A version of rangeCheck used by add and addAll.
*/
private void rangeCheckForAdd(int index) {
- if (index > size || index < 0)
- throw new IndexOutOfBoundsException(outOfBoundsMsg(index));
+ //Objects.checkFromIndexSize(index, 0, size); // 0 means index==size is OK
+ // Error message: "Range [$index, $index + 0) out of bounds for length $size"
+ //Objects.checkFromToIndex(index, index, size); // this also works, but is harder on JIT
+ // Suboptimal message customizable via the internal Preconditions API.
+ jdk.internal.util.
+ Preconditions.checkFromIndexSize(index, 0, size, ArrayList::outOfBoundsMsg4jiuP);
}
/**
* Constructs an IndexOutOfBoundsException detail message.
* Of the many possible refactorings of the error handling code,
* this "outlining" performs best with both server and client VMs.
+ * FIXME: This formatting logic is duplicated at least five times
+ * in this package. Share it as a package-private method in
+ * somewhere like AbstractList or Objects.
*/
- private String outOfBoundsMsg(int index) {
+ private String outOfBoundsMsg4jiuP(String ignore, List<Number> indexZeroSize) {
+ Number index = indexZeroSize.get(0), size = indexZeroSize.get(2);
return "Index: "+index+", Size: "+size;
}
```
The above example is chosen to show how an *inclusive* range check may be done. The error message can be adjusted in the way shown.
- relates to
-
JDK-8270057 Use Objects.checkFromToIndex for j.u.c.CopyOnWriteArrayList
-
- Open
-