This optimizes GapContent.getChars() to only copy the buffer when really really necessary. It also makes this method respect the Segment.isPartialReturn() property for further optimization. This makes a couple of tests in Intel's testsuite happy and should be good for performance.

2006-08-10  Roman Kennke  <[EMAIL PROTECTED]>

        * javax/swing/text/GapContent.java
        (getChars): Optimized to only copy array when really necessary.
        Respect the partialReturn property.

/Roman
Index: javax/swing/text/GapContent.java
===================================================================
RCS file: /cvsroot/classpath/classpath/javax/swing/text/GapContent.java,v
retrieving revision 1.54
diff -u -1 -2 -r1.54 GapContent.java
--- javax/swing/text/GapContent.java	27 Jul 2006 15:39:42 -0000	1.54
+++ javax/swing/text/GapContent.java	10 Aug 2006 21:19:19 -0000
@@ -486,47 +486,61 @@
     int length = length();
     if (where < 0)
       throw new BadLocationException("the where argument may not be below zero", where);
     if (where >= length)
       throw new BadLocationException("the where argument cannot be greater"
           + " than the content length", where);
     if ((where + len) > length)
       throw new BadLocationException("len plus where cannot be greater"
           + " than the content length", len + where);
     if (len < 0)
       throw new BadLocationException("negative length not allowed: ", len);
 
-    // check if requested segment is contiguous
-    if ((where < gapStart) && ((gapStart - where) < len))
-    {
-      // requested segment is not contiguous -> copy the pieces together
-      char[] copy = new char[len];
-      int lenFirst = gapStart - where; // the length of the first segment
-      System.arraycopy(buffer, where, copy, 0, lenFirst);
-      System.arraycopy(buffer, gapEnd, copy, lenFirst, len - lenFirst);
-      txt.array = copy;
-      txt.offset = 0;
-      txt.count = len;
-    }
-    else
-    {
-      // requested segment is contiguous -> we can simply return the
-      // actual content
-      txt.array = buffer;
-      if (where < gapStart)
+    // Optimized to copy only when really needed. 
+    if (where + len <= gapStart)
+      {
+        // Simple case: completely before gap.
+        txt.array = buffer;
         txt.offset = where;
-      else
-        txt.offset = where + (gapEnd - gapStart);
-      txt.count = len;
-    }
+        txt.count = len;
+      }
+    else if (where > gapStart)
+      {
+        // Completely after gap, adjust offset.
+        txt.array = buffer;
+        txt.offset = gapEnd + where - gapStart;
+        txt.count = len;
+      }
+    else
+      {
+        // Spans the gap.
+        int beforeGap = gapStart - where;
+        if (txt.isPartialReturn())
+          {
+            // Return the part before the gap when partial return is allowed.
+            txt.array = buffer;
+            txt.offset = where;
+            txt.count = beforeGap;
+          }
+        else
+          {
+            // Copy pieces together otherwise.
+            txt.array = new char[len];
+            txt.offset = 0;
+            System.arraycopy(buffer, where, txt.array, 0, beforeGap);
+            System.arraycopy(buffer, gapEnd, txt.array, beforeGap,
+                             len - beforeGap);
+            txt.count = len;
+          }
+      }
   }
 
   /**
    * Creates and returns a mark at the specified position.
    * 
    * @param offset the position at which to create the mark
    * 
    * @return the create Position object for the mark
    * 
    * @throws BadLocationException if the offset is not a valid position in the
    *         buffer
    */

Reply via email to