two Hash font optimization
authortupshin <tupshin@tupshin.com>
Wed, 10 Mar 2004 01:16:04 +0000 (01:16 +0000)
committertupshin <tupshin@tupshin.com>
Wed, 10 Mar 2004 01:16:04 +0000 (01:16 +0000)
This patch makes a huge difference in startup performance.
Scenario: A simple app that contains 240 boxes each with text foo.
Measurement: time from launch until core stops using 100% CPU
normal Font.java: 61 seconds
this Font.java: 7 seconds

darcs-hash:20040310011604-a9258-ae21b540fe7add307aa7cf8ef82f8a3a1521fd06.gz

src/org/ibex/Font.java

index ea17dff..48fd0de 100644 (file)
@@ -3,6 +3,8 @@ package org.ibex;
 import org.ibex.translators.*;
 import org.ibex.util.*;
 import java.io.*;
+import java.util.Hashtable;
+
 import org.ibex.js.JSExn;
 
 // FEATURE: this could be cleaner
@@ -36,7 +38,8 @@ public class Font {
     // Statics //////////////////////////////////////////////////////////////////////
 
     private static final Freetype freetype = new Freetype();
-    static final Queue glyphsToBeRendered = new Queue(255);
+    static final Hashtable glyphsToBeCached = new Hashtable();
+    static final Hashtable glyphsToBeDisplayed = new Hashtable();
     private static Cache fontCache = new Cache(100);
     public static Font getFont(Stream stream, int pointsize) {
         Font ret = (Font)fontCache.get(stream, new Integer(pointsize));
@@ -62,48 +65,51 @@ public class Font {
                                 final Scheduler.Task callback) {
         boolean encounteredUnrenderedGlyph = false;
         int width = 0, height = 0;
-        for(int i=0; i<text.length(); i++) {
-            final char c = text.charAt(i);
-            Glyph g = glyphs[c];
-            if (g == null) {
-                g = Platform.createGlyph(this, c);
-                glyphs[c] = g;
-            }
-            if (!g.isLoaded) {
-                //Log.debug(Font.class, "rasterizeGlyphs encountered unrasterized glyph " + g.c + " of font " + this);
-                //System.out.println("rasterizeGlyphs encountered unrasterized glyph " + g.c + " of font " + this);
-                glyphsToBeRendered.prepend(g);              // even if it's already in the queue, boost its priority
-                encounteredUnrenderedGlyph = true;
-            } else if (!encounteredUnrenderedGlyph) {
-                if (pb != null) pb.drawGlyph(g, x + width, y + g.font.max_ascent - g.baseline, cx1, cy1, cx2, cy2, textcolor);
-                width += g.advance;
-                height = java.lang.Math.max(height, max_ascent + max_descent);
-            }
-        }
-
-        if (!encounteredUnrenderedGlyph) return ((((long)width) << 32) | (long)(height & 0xffffffffL));
-
-        if (callback != null) Scheduler.add(new Scheduler.Task() {
-                public void perform() throws IOException, JSExn {
-                    // FEATURE this isn't terribly efficient... perhaps the task should go on the last glyph?
-                    for(int i=0; i<text.length(); i++) {
-                        Glyph g = glyphs[text.charAt(i)];
-                        if (g == null || !g.isLoaded) { Scheduler.add(this); return; }
-                    }
-                    callback.perform();
-                }});
-
+        
         // preload the Latin-1 charset with low priority (we'll probably want it)
         if (!latinCharsPreloaded) {
-            for(int i=48; i<57; i++) glyphsToBeRendered.append(glyphs[i] = Platform.createGlyph(this, (char)i));
-            for(int i=32; i<47; i++) glyphsToBeRendered.append(glyphs[i] = Platform.createGlyph(this, (char)i));
-            for(int i=57; i<128; i++) glyphsToBeRendered.append(glyphs[i] = Platform.createGlyph(this, (char)i));
-            latinCharsPreloaded = true;
-        }
-        if (!glyphRenderingTaskIsScheduled) {
+            for(int i=48; i<57; i++) glyphsToBeCached.put(glyphs[i] = Platform.createGlyph(this, (char)i),"");
+            for(int i=32; i<47; i++) glyphsToBeCached.put(glyphs[i] = Platform.createGlyph(this, (char)i),"");
+            for(int i=57; i<128; i++) glyphsToBeCached.put(glyphs[i] = Platform.createGlyph(this, (char)i),"");            
+            }
+        latinCharsPreloaded = true;
+        
+       if (!glyphRenderingTaskIsScheduled) {
             Scheduler.add(glyphRenderingTask);
             glyphRenderingTaskIsScheduled = true;
         }
+        
+        for(int i=0; i<text.length(); i++) {
+               final char c = text.charAt(i);
+               Glyph g = glyphs[c];
+               if (g == null) {
+                       g = Platform.createGlyph(this, c);
+                       glyphs[c] = g;
+               }
+               if (!g.isLoaded) {
+                       if (!glyphsToBeDisplayed.contains(g)) {
+                               glyphsToBeDisplayed.put(g, "");              // move glyph to the display hash
+                               glyphsToBeCached.remove(g);
+                               encounteredUnrenderedGlyph = true;
+                       }
+               } else if (!encounteredUnrenderedGlyph) {
+                       if (pb != null) pb.drawGlyph(g, x + width, y + g.font.max_ascent - g.baseline, cx1, cy1, cx2, cy2, textcolor);
+                       width += g.advance;
+                       height = java.lang.Math.max(height, max_ascent + max_descent);
+               }
+        }
+        
+        if (!encounteredUnrenderedGlyph) return ((((long)width) << 32) | (long)(height & 0xffffffffL));
+        
+        if (callback != null) Scheduler.add(new Scheduler.Task() {
+               public void perform() throws IOException, JSExn {
+                       // FEATURE this isn't terribly efficient... perhaps the task should go on the last glyph?
+                       for(int i=0; i<text.length(); i++) {
+                               Glyph g = glyphs[text.charAt(i)];
+                               if (g == null || !g.isLoaded) { Scheduler.add(this); return; }
+                       }
+                       callback.perform();
+               }});
         return -1;
     }
 
@@ -120,14 +126,26 @@ public class Font {
     }
 
     static final Scheduler.Task glyphRenderingTask = new Scheduler.Task() { public void perform() {
-        Glyph g = (Glyph)glyphsToBeRendered.remove(false);
-        if (g == null) { glyphRenderingTaskIsScheduled = false; return; }
-        Log.debug(Font.class, "glyphRenderingTask dequeued glyph " + g.c + " of font " + g.font);
-        if (!g.isLoaded) {
-            Log.debug(Glyph.class, "rendering glyph " + g.c);
+        Glyph g = null;
+        if (!glyphsToBeDisplayed.isEmpty()) {
+            g = (Glyph)glyphsToBeDisplayed.keys().nextElement();
+            glyphsToBeDisplayed.remove(g);
+            Log.debug(Font.class, "glyphRenderingTask removed glyph " + g.c + " of font " /*+ g.font*/ + " from glyphsToBeDisplayed");
+        }
+        else if (!glyphsToBeCached.isEmpty()) {
+            g = (Glyph)glyphsToBeCached.keys().nextElement();
+            glyphsToBeCached.remove(g);
+            Log.debug(Font.class, "glyphRenderingTask removed glyph " + g.c + " of font " /*+ g.font*/ + " from glyphsToBeCached");
+        }
+        if (g != null) {
+            Log.debug(Glyph.class, "rendering glyph " + g);
             try { freetype.renderGlyph(g); } catch (IOException e) { Log.info(Freetype.class, e); }
+            Scheduler.add(this);          // keep ourselves in the queue until there are no glyphs to render
+            glyphRenderingTaskIsScheduled = true;
+        }
+        else {
+            glyphRenderingTaskIsScheduled = false;
+            return;
         }
-        Scheduler.add(this);          // keep ourselves in the queue until there are no glyphs to render
-        glyphRenderingTaskIsScheduled = true;
     } };
 }