Here comes a small optimization to PlainView: I added the protected
method getLineBuffer() which provides access to a shared instance of
Segment, which can be reused everytime when something must be fetched
from the document. This should take some load from the allocator and
garbage collector.

2005-10-13  Roman Kennke  <[EMAIL PROTECTED]>

        * javax/swing/text/PlainView.java
        (lineBuffer): New field used as buffer to fetch text from a
        document.
        (getLineBuffer): New method, provides access to the new field
        above.
        (modelToView): Use new lineBuffer.
        (drawSelectedText): Likewise.
        (drawUnselectedText): Likewise.
        (determineMaxLineLength): Likewise.
        (viewToModel): Likewise.
        (updateDamage): Likewise.


/Roman
Index: javax/swing/text/PlainView.java
===================================================================
RCS file: /cvsroot/classpath/classpath/javax/swing/text/PlainView.java,v
retrieving revision 1.21
diff -u -r1.21 PlainView.java
--- javax/swing/text/PlainView.java	13 Oct 2005 12:51:00 -0000	1.21
+++ javax/swing/text/PlainView.java	13 Oct 2005 13:30:46 -0000
@@ -70,6 +70,11 @@
   
   protected FontMetrics metrics;
 
+  /**
+   * The instance returned by [EMAIL PROTECTED] #getLineBuffer()}.
+   */
+  private transient Segment lineBuffer;
+
   public PlainView(Element elem)
   {
     super(elem);
@@ -119,7 +124,7 @@
     // Get the rectangle for position.
     Element line = getElement().getElement(lineIndex);
     int lineStart = line.getStartOffset();
-    Segment segment = new Segment();
+    Segment segment = getLineBuffer();
     document.getText(lineStart, position - lineStart, segment);
     int xoffset = Utilities.getTabbedTextWidth(segment, metrics, rect.x,
 					       this, lineStart);
@@ -152,7 +157,7 @@
     throws BadLocationException
   {
     g.setColor(selectedColor);
-    Segment segment = new Segment();
+    Segment segment = getLineBuffer();
     getDocument().getText(p0, p1 - p0, segment);
     return Utilities.drawTabbedText(segment, x, y, g, this, 0);
   }
@@ -166,7 +171,7 @@
     else
       g.setColor(disabledColor);
 
-    Segment segment = new Segment();
+    Segment segment = getLineBuffer();
     getDocument().getText(p0, p1 - p0, segment);
     return Utilities.drawTabbedText(segment, x, y, g, this, segment.offset);
   }
@@ -228,7 +233,7 @@
     
     // otherwise we have to go through all the lines and find it
     Element el = getElement();
-    Segment seg = new Segment();
+    Segment seg = getLineBuffer();
     float span = 0;
     for (int i = 0; i < el.getElementCount(); i++)
       {
@@ -305,7 +310,7 @@
       return getEndOffset() - 1;
     
     Element line = root.getElement(lineClicked);
-    Segment s = new Segment();
+    Segment s = getLineBuffer();
 
     int start = line.getStartOffset();
     int end = line.getEndOffset();
@@ -385,7 +390,7 @@
     // If we've reached here, that means we haven't removed the longest line
     // and we have added at least one line, so we have to check if added lines
     // are longer than the previous longest line        
-    Segment seg = new Segment();
+    Segment seg = getLineBuffer();
     float longestNewLength = 0;
     Element longestNewLine = null;    
 
@@ -489,6 +494,20 @@
         host.repaint(0, repaintRec.y, host.getWidth(),
                      repaintRec.height);
       }    
+  }
+
+  /**
+   * Provides a [EMAIL PROTECTED] Segment} object, that can be used to fetch text from
+   * the document.
+   *
+   * @returna [EMAIL PROTECTED] Segment} object, that can be used to fetch text from
+   *          the document
+   */
+  protected Segment getLineBuffer()
+  {
+    if (lineBuffer == null)
+      lineBuffer = new Segment();
+    return lineBuffer;
   }
 }
 
_______________________________________________
Classpath-patches mailing list
[email protected]
http://lists.gnu.org/mailman/listinfo/classpath-patches

Reply via email to