[cp-patches] Performance improvement for FreetypeGlyphVector.java

Andrew Haley aph at redhat.com
Wed Nov 5 14:17:02 UTC 2008


Yesterday I discovered some heinously inefficient code in FreetypeGlyphVector
which was causing a huge amount of time to be spent on pointless class
searches.

The cause was that getKerning was doing a dynamic lookup for
java/awt/geom/Point2D$Double for every character.

Andrew.


2008-11-05  Andrew Haley  <aph at redhat.com>

	* gnu/java/awt/peer/gtk/FreetypeGlyphVector.java (getKerning):
	Return result in a float[], not a Point2D.
	(performDefaultLayout): Call getKerning with a float[].
	* native/jni/gtk-peer/gnu_java_awt_peer_gtk_FreetypeGlyphVector.c
	(getKerning): Return result in a float[], not a Point2D.
	
Index: gnu/java/awt/peer/gtk/FreetypeGlyphVector.java
===================================================================
--- gnu/java/awt/peer/gtk/FreetypeGlyphVector.java	(revision 141575)
+++ gnu/java/awt/peer/gtk/FreetypeGlyphVector.java	(working copy)
@@ -247,7 +247,8 @@
   /**
    * Returns the kerning of a glyph pair
    */
-  private native Point2D getKerning(int leftGlyph, int rightGlyph, long font);
+  private native void getKerning(int leftGlyph, int rightGlyph, long font,
+				 float[] p);

   private native double[] getMetricsNative(int glyphCode, long font);

@@ -301,6 +302,7 @@
     GlyphMetrics gm = null;
     float x = 0;
     float y = 0;
+    float[] p = {0.0f, 0.0f};
     for(int i = 0; i < nGlyphs; i++)
       {
         gm = getGlyphMetrics( i );
@@ -314,9 +316,9 @@
         // using the same font
         if (i != nGlyphs-1 && fontSet[i] == fontSet[i+1])
           {
-            Point2D p = getKerning(glyphCodes[i], glyphCodes[i + 1], fontSet[i]);
-            x += p.getX();
-            y += p.getY();
+            getKerning(glyphCodes[i], glyphCodes[i + 1], fontSet[i], p);
+            x += p[0];
+            y += p[1];
           }
       }
     glyphPositions[nGlyphs * 2] = x;
Index: include/gnu_java_awt_peer_gtk_FreetypeGlyphVector.h
===================================================================
--- include/gnu_java_awt_peer_gtk_FreetypeGlyphVector.h	(revision 141575)
+++ include/gnu_java_awt_peer_gtk_FreetypeGlyphVector.h	(working copy)
@@ -13,7 +13,7 @@
 JNIEXPORT void JNICALL Java_gnu_java_awt_peer_gtk_FreetypeGlyphVector_dispose (JNIEnv *env, jobject, jlongArray);
 JNIEXPORT jlong JNICALL Java_gnu_java_awt_peer_gtk_FreetypeGlyphVector_getNativeFontPointer (JNIEnv *env, jobject, jint);
 JNIEXPORT void JNICALL Java_gnu_java_awt_peer_gtk_FreetypeGlyphVector_getGlyphs (JNIEnv *env, jobject, jintArray, jintArray, jlongArray);
-JNIEXPORT jobject JNICALL Java_gnu_java_awt_peer_gtk_FreetypeGlyphVector_getKerning (JNIEnv *env, jobject, jint, jint, jlong);
+JNIEXPORT void JNICALL Java_gnu_java_awt_peer_gtk_FreetypeGlyphVector_getKerning (JNIEnv *env, jobject, jint, jint, jlong, jfloatArray);
 JNIEXPORT jdoubleArray JNICALL Java_gnu_java_awt_peer_gtk_FreetypeGlyphVector_getMetricsNative (JNIEnv *env, jobject, jint, jlong);
 JNIEXPORT jobject JNICALL Java_gnu_java_awt_peer_gtk_FreetypeGlyphVector_getGlyphOutlineNative (JNIEnv *env, jobject, jint, jlong);

Index: native/jni/gtk-peer/gnu_java_awt_peer_gtk_FreetypeGlyphVector.c
===================================================================
--- native/jni/gtk-peer/gnu_java_awt_peer_gtk_FreetypeGlyphVector.c	(revision 141575)
+++ native/jni/gtk-peer/gnu_java_awt_peer_gtk_FreetypeGlyphVector.c	(working copy)
@@ -169,9 +169,10 @@
   (*env)->ReleaseLongArrayElements (env, fonts, fontArray, 0);
 }

-JNIEXPORT jobject JNICALL
+JNIEXPORT void JNICALL
 Java_gnu_java_awt_peer_gtk_FreetypeGlyphVector_getKerning
-(JNIEnv *env, jobject obj __attribute__((unused)), jint rightGlyph, jint leftGlyph, jlong fnt)
+  (JNIEnv *env, jobject obj __attribute__((unused)), jint rightGlyph,
+   jint leftGlyph, jlong fnt, jfloatArray p)
 {
   FT_Face ft_face;
   FT_Vector kern;
@@ -187,12 +188,10 @@

   pango_fc_font_unlock_face( font );

-  values[0].d = (jdouble)kern.x/64.0;
-  values[1].d = (jdouble)kern.y/64.0;
-
-  cls = (*env)->FindClass (env, "java/awt/geom/Point2D$Double");
-  method = (*env)->GetMethodID (env, cls, "<init>", "(DD)V");
-  return (*env)->NewObjectA(env, cls, method, values);
+  jfloat *pelements = (*env)->GetPrimitiveArrayCritical(env, p, NULL);
+  pelements[0] = (jfloat)kern.x/64.0;
+  pelements[1] = (jfloat)kern.y/64.0;
+  (*env)->ReleasePrimitiveArrayCritical (env, p, pelements, 0);
 }

 JNIEXPORT jdoubleArray JNICALL



More information about the Classpath-patches mailing list