harmony-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From telli...@apache.org
Subject svn commit: r787995 - in /harmony/enhanced/classlib/trunk/modules/luni/src: main/java/java/lang/String.java test/api/common/org/apache/harmony/luni/tests/java/lang/String2Test.java
Date Wed, 24 Jun 2009 12:30:04 GMT
Author: tellison
Date: Wed Jun 24 12:30:03 2009
New Revision: 787995

URL: http://svn.apache.org/viewvc?rev=787995&view=rev
Log:
Improve performance of String.hashCode() by avoiding multiplier.

Modified:
    harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/lang/String.java
    harmony/enhanced/classlib/trunk/modules/luni/src/test/api/common/org/apache/harmony/luni/tests/java/lang/String2Test.java

Modified: harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/lang/String.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/lang/String.java?rev=787995&r1=787994&r2=787995&view=diff
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/lang/String.java (original)
+++ harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/lang/String.java Wed Jun
24 12:30:03 2009
@@ -937,11 +937,12 @@
     @Override
     public int hashCode() {
         if (hashCode == 0) {
-            int hash = 0, multiplier = 1;
-            for (int i = offset + count - 1; i >= offset; i--) {
-                hash += value[i] * multiplier;
-                int shifted = multiplier << 5;
-                multiplier = shifted - multiplier;
+            if (count == 0) {
+                return 0;
+            }
+            int hash = 0;
+            for (int i = offset; i < count + offset; i++) {
+                hash = value[i] + ((hash << 5) - hash);
             }
             hashCode = hash;
         }

Modified: harmony/enhanced/classlib/trunk/modules/luni/src/test/api/common/org/apache/harmony/luni/tests/java/lang/String2Test.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/luni/src/test/api/common/org/apache/harmony/luni/tests/java/lang/String2Test.java?rev=787995&r1=787994&r2=787995&view=diff
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/luni/src/test/api/common/org/apache/harmony/luni/tests/java/lang/String2Test.java
(original)
+++ harmony/enhanced/classlib/trunk/modules/luni/src/test/api/common/org/apache/harmony/luni/tests/java/lang/String2Test.java
Wed Jun 24 12:30:03 2009
@@ -529,11 +529,9 @@
             hwHashCode += hw1.charAt(counter) * powerOfThirtyOne;
             powerOfThirtyOne *= 31;
         }
-        assertTrue("String did not hash to correct value--got: "
-                + String.valueOf(hw1.hashCode()) + " but wanted: "
-                + String.valueOf(hwHashCode), hw1.hashCode() == hwHashCode);
-        assertTrue("The empty string \"\" did not hash to zero", 0 == ""
-                .hashCode());
+        assertEquals("String did not hash to correct value", hwHashCode, hw1.hashCode());
+        assertEquals("The empty string \"\" did not hash to zero", 0, "".hashCode());
+        assertEquals("Calculated wrong string hashcode", -1933545242, "Harmony".hashCode());
     }
 
     /**



Mime
View raw message