This is the mail archive of the java-patches@gcc.gnu.org mailing list for the Java project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Patch: FYI: speed up String.equals


I'm checking this in.  This fixes PR 23495.

This replaces hand-written loops with memcmp and memcpy where
appropriate in String.  This results in a nice speedup for
String.equals (at least).  I also added a check of the hash codes in
String.equals, which provides a separate performance improvement.

I tested this using the test case in the PR.  This is somewhat noisy
but yields a measurable speedup.

We could probably get even better speedups in some situations, for
instance if we know both pointers are aligned properly we could do
4-byte (or on a 64 bit machine, 8 byte) comparisons at once.  I didn't
investigate this, I'm naively hoping that memcmp or memcpy will do
this for us.

Tom

Index: ChangeLog
from  Tom Tromey  <tromey@redhat.com>
	PR libgcj/23495:
	* java/lang/natString.cc (_Jv_NewString): Use memcpy.
	(equals): Use memcmp.
	(contentEquals): Likewise.
	(getChars): Use memcpy.
	(toCharArray): Likewise.
	(regionMatches): Use memcmp.
	(regionMatches): Likewise.
	(startsWith): Likewise.
	(concat): Use memcpy.
	(valueOf): Likewise.

Index: java/lang/natString.cc
===================================================================
--- java/lang/natString.cc	(revision 111868)
+++ java/lang/natString.cc	(working copy)
@@ -409,8 +409,7 @@
 {
   jstring str = _Jv_AllocString(len);
   jchar* data = JvGetStringChars (str);
-  while (--len >= 0)
-    *data++ = *chars++;
+  memcpy (data, chars, len * sizeof (jchar));
   return str;
 }
 
@@ -534,16 +533,20 @@
   jstring other = (jstring) anObject;
   if (count != other->count)
     return false;
-  /* if both are interned, return false. */
-  jint i = count;
+
+  // If both have cached hash codes, check that.  If the cached hash
+  // codes are zero, don't bother trying to compute them.
+  int myHash = cachedHashCode;
+  int otherHash = other->cachedHashCode;
+  if (myHash && otherHash && myHash != otherHash)
+    return false;
+
+  // We could see if both are interned, and return false.  But that
+  // seems too expensive.
+
   jchar *xptr = JvGetStringChars (this);
   jchar *yptr = JvGetStringChars (other);
-  while (--i >= 0)
-    {
-      if (*xptr++ != *yptr++)
-	return false;
-    }
-  return true;
+  return ! memcmp (xptr, yptr, count * sizeof (jchar));
 }
 
 jboolean
@@ -556,13 +559,9 @@
     return false;
   if (data == buffer->value)
     return true; // Possible if shared.
-  jint i = count;
   jchar *xptr = JvGetStringChars(this);
   jchar *yptr = elements(buffer->value);
-  while (--i >= 0)
-    if (*xptr++ != *yptr++)
-      return false;
-  return true;
+  return ! memcmp (xptr, yptr, count * sizeof (jchar));
 }
 
 jboolean
@@ -599,9 +598,8 @@
     throw new ArrayIndexOutOfBoundsException;
   jchar *dPtr = elements (dst) + dstBegin;
   jchar *sPtr = JvGetStringChars (this) + srcBegin;
-  jint i = srcEnd-srcBegin;
-  while (--i >= 0)
-    *dPtr++ = *sPtr++;
+  jint i = srcEnd - srcBegin;
+  memcpy (dPtr, sPtr, i * sizeof (jchar));
 }
 
 jbyteArray
@@ -666,8 +664,7 @@
   jchar *dPtr = elements (array);
   jchar *sPtr = JvGetStringChars (this);
   jint i = count;
-  while (--i >= 0)
-    *dPtr++ = *sPtr++;
+  memcpy (dPtr, sPtr, i * sizeof (jchar));
   return array;
 }
 
@@ -704,12 +701,7 @@
   jchar *tptr = JvGetStringChars (this) + toffset;
   jchar *optr = JvGetStringChars (other) + ooffset;
   jint i = len;
-  while (--i >= 0)
-    {
-      if (*tptr++ != *optr++)
-	return false;
-    }
-  return true;
+  return ! memcmp (tptr, optr, i * sizeof (jchar));
 }
 
 jint
@@ -742,25 +734,20 @@
   jchar *optr = JvGetStringChars (other) + ooffset;
   jint i = len;
   if (ignoreCase)
-    while (--i >= 0)
-      {
-	jchar tch = *tptr++;
-	jchar och = *optr++;
-	if ((java::lang::Character::toLowerCase (tch)
-	     != java::lang::Character::toLowerCase (och))
-	    && (java::lang::Character::toUpperCase (tch)
-		!= java::lang::Character::toUpperCase (och)))
-	  return false;
-      }
-  else
-    while (--i >= 0)
-      {
-	jchar tch = *tptr++;
-	jchar och = *optr++;
-	if (tch != och)
-	  return false;
-      }
-  return true;
+    {
+      while (--i >= 0)
+	{
+	  jchar tch = *tptr++;
+	  jchar och = *optr++;
+	  if ((java::lang::Character::toLowerCase (tch)
+	       != java::lang::Character::toLowerCase (och))
+	      && (java::lang::Character::toUpperCase (tch)
+		  != java::lang::Character::toUpperCase (och)))
+	    return false;
+	}
+      return true;
+    }
+  return ! memcmp (tptr, optr, i * sizeof (jchar));
 }
 
 jboolean
@@ -771,12 +758,7 @@
     return false;
   jchar *xptr = JvGetStringChars (this) + toffset;
   jchar *yptr = JvGetStringChars (prefix);
-  while (--i >= 0)
-    {
-      if (*xptr++ != *yptr++)
-	return false;
-    }
-  return true;
+  return ! memcmp (xptr, yptr, i * sizeof (jchar));
 }
 
 jint
@@ -868,12 +850,11 @@
   jchar *dstPtr = JvGetStringChars(result);
   jchar *srcPtr = JvGetStringChars(this);
   jint i = count;
-  while (--i >= 0)
-    *dstPtr++ = *srcPtr++;
+  memcpy (dstPtr, srcPtr, i * sizeof (jchar));
+  dstPtr += i;
   srcPtr = JvGetStringChars(str);
   i = str->count;
-  while (--i >= 0)
-    *dstPtr++ = *srcPtr++;
+  memcpy (dstPtr, srcPtr, i * sizeof (jchar));
   return result;
 }
 
@@ -1048,8 +1029,7 @@
   jstring result = JvAllocString(count);
   jchar *sPtr = elements (data) + offset;
   jchar *dPtr = JvGetStringChars(result);
-  while (--count >= 0)
-    *dPtr++ = *sPtr++;
+  memcpy (dPtr, sPtr, count * sizeof (jchar));
   return result;
 }
 


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]