You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@harmony.apache.org by ap...@apache.org on 2007/03/14 13:35:01 UTC
svn commit: r518115 - in
/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/lang:
Character.java Integer.java Long.java Short.java
Author: apetrenko
Date: Wed Mar 14 05:35:00 2007
New Revision: 518115
URL: http://svn.apache.org/viewvc?view=rev&rev=518115
Log:
Patch for HARMONY-3392 "[Classlib][luni][performance] Improvement of java.lang.(Ineteger,Short,Long,Character).valueOf() methods"
Modified:
harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/lang/Character.java
harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/lang/Integer.java
harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/lang/Long.java
harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/lang/Short.java
Modified: harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/lang/Character.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/lang/Character.java?view=diff&rev=518115&r1=518114&r2=518115
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/lang/Character.java (original)
+++ harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/lang/Character.java Wed Mar 14 05:35:00 2007
@@ -1701,13 +1701,6 @@
return value - c.value;
}
- /*
- * Provides a cache for the 'valueOf' method. A size of 512 should cache the
- * first couple pages of Unicode, which includes the ASCII/Latin-1
- * characters, which other parts of this class are optimized for.
- */
- private static final Character[] CACHE = new Character[512];
-
/**
* <p>
* Returns a <code>Character</code> instance for the <code>char</code>
@@ -1720,18 +1713,28 @@
* @since 1.5
*/
public static Character valueOf(char c) {
- if (c > CACHE.length) {
+ if (c >= CACHE_LEN ) {
return new Character(c);
}
- synchronized (CACHE) {
- Character ch = CACHE[c];
- if (ch == null) {
- CACHE[c] = ch = new Character(c);
+ return valueOfCache.CACHE[c];
+ }
+
+ private static final int CACHE_LEN = 512;
+
+ static class valueOfCache {
+ /*
+ * Provides a cache for the 'valueOf' method. A size of 512 should cache the
+ * first couple pages of Unicode, which includes the ASCII/Latin-1
+ * characters, which other parts of this class are optimized for.
+ */
+ private static final Character[] CACHE = new Character[CACHE_LEN ];
+
+ static {
+ for(int i=0; i<CACHE.length; i++){
+ CACHE[i] = new Character((char)i);
}
- return ch;
}
}
-
/**
* <p>
* A test for determining if the <code>codePoint</code> is a valid Unicode
@@ -2491,7 +2494,7 @@
*/
@Override
public boolean equals(Object object) {
- return (object == this) || (object instanceof Character)
+ return (object instanceof Character)
&& (value == ((Character) object).value);
}
Modified: harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/lang/Integer.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/lang/Integer.java?view=diff&rev=518115&r1=518114&r2=518115
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/lang/Integer.java (original)
+++ harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/lang/Integer.java Wed Mar 14 05:35:00 2007
@@ -76,13 +76,6 @@
// defined to be "java.lang.Integer.TYPE";
/**
- * <p>
- * A cache of instances used by {@link #valueOf(int)} and auto-boxing.
- * </p>
- */
- private static final Integer[] CACHE = new Integer[256];
-
- /**
* Constructs a new instance of the receiver which represents the int valued
* argument.
*
@@ -212,7 +205,7 @@
*/
@Override
public boolean equals(Object o) {
- return (o == this) || (o instanceof Integer)
+ return (o instanceof Integer)
&& (value == ((Integer) o).value);
}
@@ -816,10 +809,22 @@
if (i < -128 || i > 127) {
return new Integer(i);
}
- synchronized (CACHE) {
- int idx = 128 + i; // 128 matches a cache size of 256
- Integer result = CACHE[idx];
- return (result == null ? CACHE[idx] = new Integer(i) : result);
+ return valueOfCache.CACHE [i+128];
+
+ }
+
+ static class valueOfCache {
+ /**
+ * <p>
+ * A cache of instances used by {@link Integer#valueOf(int)} and auto-boxing.
+ * </p>
+ */
+ static final Integer[] CACHE = new Integer[256];
+
+ static {
+ for(int i=-128; i<=127; i++) {
+ CACHE[i+128] = new Integer(i);
+ }
}
}
}
Modified: harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/lang/Long.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/lang/Long.java?view=diff&rev=518115&r1=518114&r2=518115
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/lang/Long.java (original)
+++ harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/lang/Long.java Wed Mar 14 05:35:00 2007
@@ -75,12 +75,6 @@
*/
public static final int SIZE = 64;
- /**
- * <p>
- * A cache of instances used by {@link #valueOf(long)} and auto-boxing.
- * </p>
- */
- private static final Long[] CACHE = new Long[256];
/**
* Constructs a new instance of the receiver which represents the long
@@ -212,7 +206,7 @@
*/
@Override
public boolean equals(Object o) {
- return (o == this) || (o instanceof Long)
+ return (o instanceof Long)
&& (value == ((Long) o).value);
}
@@ -830,10 +824,21 @@
if (lng < -128 || lng > 127) {
return new Long(lng);
}
- synchronized (CACHE) {
- int idx = 128 + (int) lng; // 128 matches a cache size of 256
- Long result = CACHE[idx];
- return (result == null ? CACHE[idx] = new Long(lng) : result);
+ return valueOfCache.CACHE[128+(int)lng];
+ }
+
+ static class valueOfCache {
+ /**
+ * <p>
+ * A cache of instances used by {@link Long#valueOf(long)} and auto-boxing.
+ * </p>
+ */
+ static final Long[] CACHE = new Long[256];
+
+ static {
+ for(int i=-128; i<=127; i++) {
+ CACHE[i+128] = new Long(i);
+ }
}
}
}
Modified: harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/lang/Short.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/lang/Short.java?view=diff&rev=518115&r1=518114&r2=518115
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/lang/Short.java (original)
+++ harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/lang/Short.java Wed Mar 14 05:35:00 2007
@@ -68,12 +68,6 @@
// Note: This can't be set to "short.class", since *that* is
// defined to be "java.lang.Short.TYPE";
- /**
- * <p>
- * A cache of instances used by {@link #valueOf(short)} and auto-boxing.
- * </p>
- */
- private static final Short[] CACHE = new Short[256];
/**
* Constructs a new instance of this class given a string.
@@ -173,7 +167,7 @@
*/
@Override
public boolean equals(Object object) {
- return (object == this) || (object instanceof Short)
+ return (object instanceof Short)
&& (value == ((Short) object).value);
}
@@ -357,10 +351,21 @@
if (s < -128 || s > 127) {
return new Short(s);
}
- synchronized (CACHE) {
- int idx = 128 + s; // 128 matches a cache size of 256
- Short result = CACHE[idx];
- return (result == null ? CACHE[idx] = new Short(s) : result);
+ return valueOfCache.CACHE[s+128];
+ }
+
+ static class valueOfCache {
+ /**
+ * <p>
+ * A cache of instances used by {@link Short#valueOf(short)} and auto-boxing.
+ * </p>
+ */
+ private static final Short[] CACHE = new Short[256];
+
+ static {
+ for(int i=-128; i<=127; i++) {
+ CACHE[i+128] = new Short((short)i);
+ }
}
}
}