8075231: Typed array setters are very slow when index exceeds capacity
authorhannesw
Thu, 26 Mar 2015 22:13:41 +0100
changeset 29757 cc0d4b0167fb
parent 29756 cf18a59c63d7
child 29758 cb8621d74d60
8075231: Typed array setters are very slow when index exceeds capacity Reviewed-by: attila, lagergren
nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/objects/NativeFloat32Array.java
nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/objects/NativeFloat64Array.java
nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/objects/NativeInt16Array.java
nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/objects/NativeInt32Array.java
nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/objects/NativeInt8Array.java
nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/objects/NativeUint16Array.java
nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/objects/NativeUint32Array.java
nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/objects/NativeUint8Array.java
nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/objects/NativeUint8ClampedArray.java
--- a/nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/objects/NativeFloat32Array.java	Thu Mar 26 21:39:25 2015 +0100
+++ b/nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/objects/NativeFloat32Array.java	Thu Mar 26 22:13:41 2015 +0100
@@ -114,12 +114,11 @@
 
         private void setElem(final int index, final double elem) {
             try {
-                nb.put(index, (float)elem);
+                if (index < nb.limit()) {
+                    nb.put(index, (float) elem);
+                }
             } catch (final IndexOutOfBoundsException e) {
-                //swallow valid array indexes. it's ok.
-                if (index < 0) {
-                    throw new ClassCastException();
-                }
+                throw new ClassCastException();
             }
         }
 
--- a/nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/objects/NativeFloat64Array.java	Thu Mar 26 21:39:25 2015 +0100
+++ b/nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/objects/NativeFloat64Array.java	Thu Mar 26 22:13:41 2015 +0100
@@ -114,12 +114,11 @@
 
         private void setElem(final int index, final double elem) {
             try {
-                nb.put(index, elem);
+                if (index < nb.limit()) {
+                    nb.put(index, elem);
+                }
             } catch (final IndexOutOfBoundsException e) {
-                //swallow valid array indexes. it's ok.
-                if (index < 0) {
-                    throw new ClassCastException();
-                }
+                throw new ClassCastException();
              }
         }
 
--- a/nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/objects/NativeInt16Array.java	Thu Mar 26 21:39:25 2015 +0100
+++ b/nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/objects/NativeInt16Array.java	Thu Mar 26 22:13:41 2015 +0100
@@ -115,12 +115,11 @@
 
         private void setElem(final int index, final int elem) {
             try {
-                nb.put(index, (short)elem);
+                if (index < nb.limit()) {
+                    nb.put(index, (short) elem);
+                }
             } catch (final IndexOutOfBoundsException e) {
-                //swallow valid array indexes. it's ok.
-                if (index < 0) {
-                    throw new ClassCastException();
-                }
+                throw new ClassCastException();
             }
         }
 
--- a/nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/objects/NativeInt32Array.java	Thu Mar 26 21:39:25 2015 +0100
+++ b/nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/objects/NativeInt32Array.java	Thu Mar 26 22:13:41 2015 +0100
@@ -104,11 +104,11 @@
 
         private void setElem(final int index, final int elem) {
             try {
-                nb.put(index, elem);
-               } catch (final IndexOutOfBoundsException e) {
-                   if (index < 0) {
-                      throw new ClassCastException();
-                 }
+                if (index < nb.limit()) {
+                    nb.put(index, elem);
+                }
+            } catch (final IndexOutOfBoundsException e) {
+                throw new ClassCastException();
             }
         }
 
--- a/nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/objects/NativeInt8Array.java	Thu Mar 26 21:39:25 2015 +0100
+++ b/nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/objects/NativeInt8Array.java	Thu Mar 26 22:13:41 2015 +0100
@@ -113,12 +113,11 @@
 
         private void setElem(final int index, final int elem) {
             try {
-                nb.put(index, (byte)elem);
+                if (index < nb.limit()) {
+                    nb.put(index, (byte) elem);
+                }
             } catch (final IndexOutOfBoundsException e) {
-                //swallow valid array indexes. it's ok.
-                if (index < 0) {
-                    throw new ClassCastException();
-                }
+                throw new ClassCastException();
             }
         }
 
--- a/nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/objects/NativeUint16Array.java	Thu Mar 26 21:39:25 2015 +0100
+++ b/nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/objects/NativeUint16Array.java	Thu Mar 26 22:13:41 2015 +0100
@@ -104,12 +104,11 @@
 
         private void setElem(final int index, final int elem) {
             try {
-                nb.put(index, (char)elem);
+                if (index < nb.limit()) {
+                    nb.put(index, (char) elem);
+                }
             } catch (final IndexOutOfBoundsException e) {
-                //swallow valid array indexes. it's ok.
-                if (index < 0) {
-                    throw new ClassCastException();
-                }
+                throw new ClassCastException();
             }
         }
 
--- a/nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/objects/NativeUint32Array.java	Thu Mar 26 21:39:25 2015 +0100
+++ b/nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/objects/NativeUint32Array.java	Thu Mar 26 22:13:41 2015 +0100
@@ -113,12 +113,11 @@
 
         private void setElem(final int index, final int elem) {
             try {
-                nb.put(index, elem);
+                if (index < nb.limit()) {
+                    nb.put(index, elem);
+                }
             } catch (final IndexOutOfBoundsException e) {
-                //swallow valid array indexes. it's ok.
-                if (index < 0) {
-                    throw new ClassCastException();
-                }
+                throw new ClassCastException();
             }
         }
 
--- a/nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/objects/NativeUint8Array.java	Thu Mar 26 21:39:25 2015 +0100
+++ b/nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/objects/NativeUint8Array.java	Thu Mar 26 22:13:41 2015 +0100
@@ -104,12 +104,11 @@
 
         private void setElem(final int index, final int elem) {
             try {
-                nb.put(index, (byte)elem);
+                if (index < nb.limit()) {
+                    nb.put(index, (byte) elem);
+                }
             } catch (final IndexOutOfBoundsException e) {
-                //swallow valid array indexes. it's ok.
-                if (index < 0) {
-                    throw new ClassCastException();
-                }
+                throw new ClassCastException();
             }
         }
 
--- a/nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/objects/NativeUint8ClampedArray.java	Thu Mar 26 21:39:25 2015 +0100
+++ b/nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/objects/NativeUint8ClampedArray.java	Thu Mar 26 22:13:41 2015 +0100
@@ -133,18 +133,17 @@
 
         private void setElem(final int index, final int elem) {
             try {
-                final byte clamped;
-                if ((elem & 0xffff_ff00) == 0) {
-                    clamped = (byte)elem;
-                } else {
-                    clamped = elem < 0 ? 0 : (byte)0xff;
+                if (index < nb.limit()) {
+                    final byte clamped;
+                    if ((elem & 0xffff_ff00) == 0) {
+                        clamped = (byte) elem;
+                    } else {
+                        clamped = elem < 0 ? 0 : (byte) 0xff;
+                    }
+                    nb.put(index, clamped);
                 }
-                nb.put(index, clamped);
             } catch (final IndexOutOfBoundsException e) {
-                //swallow valid array indexes. it's ok.
-                if (index < 0) {
-                    throw new ClassCastException();
-                }
+                throw new ClassCastException();
             }
         }