freetype
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Freetype] Freetype is very slow at opening large Postscript Asian fonts


From: Pedriana, Paul
Subject: [Freetype] Freetype is very slow at opening large Postscript Asian fonts...
Date: Thu, 31 Oct 2002 17:29:48 -0800

Freetype is very slow at opening large PostScript Asian fonts. I have a font with 8000 characters in it and Freetype takes upwards of 20 seconds just to return from FT_New_Face() on a PIII 1GHz PC. The time is compounded by the fact that I have to do this multiple times for different fonts and variations. I traced the slowness and it comes down to the PS_Table_Add function. In particular, these lines of code:
   while ( new_size < table->cursor + length )
     new_size += 1024;
   error = reallocate_t1_table( table, new_size );
 
The problem is that with large fonts, this reallocation can happen thousands of times. I tried changing the code to use new_size *= 2 instead of new_size += 1024 and the FT_NewFace() function now goes about 10 times as fast. It would be even faster if the reallocation code didn't do all those tests and instead the caller of PS_Table_Add allocated the required amount up front. I'm not sure what the best solution is, but I tend to think that new_size += 1024 is not it. Doing *= 2 and then scaling the allocation back to minimum when done is one way to get the speed but not use any extra memory.
 
Until I hear back from somebody, I'll just change my version of the code to new_size *= 2 so I can stop getting bug reports that my app has hung. :)
 
The code below is from Freetype 2.12 (the most recent as of this writing).
  /*************************************************************************/
  /*                                                                       */
  /* <Function>                                                            */
  /*    PS_Table_Add                                                       */
  /*                                                                       */
  /* <Description>                                                         */
  /*    Adds an object to a PS_Table, possibly growing its memory block.   */
  /*                                                                       */
  /* <InOut>                                                               */
  /*    table  :: The target table.                                        */
  /*                                                                       */
  /* <Input>                                                               */
  /*    idx  :: The index of the object in the table.                      */
  /*                                                                       */
  /*    object :: The address of the object to copy in memory.             */
  /*                                                                       */
  /*    length :: The length in bytes of the source object.                */
  /*                                                                       */
  /* <Return>                                                              */
  /*    FreeType error code.  0 means success.  An error is returned if a  */
  /*    reallocation fails.                                                */
  /*                                                                       */
  FT_LOCAL_DEF( FT_Error )
  PS_Table_Add( PS_Table  table,
                FT_Int    idx,
                void*     object,
                FT_Int    length )
  {
    if ( idx < 0 || idx > table->max_elems )
    {
      FT_ERROR(( "PS_Table_Add: invalid index\n" ));
      return PSaux_Err_Invalid_Argument;
    }
    /* grow the base block if needed */
    if ( table->cursor + length > table->capacity )
    {
      FT_Error   error;
      FT_Offset  new_size  = table->capacity;
      FT_Long    in_offset;

      in_offset = (FT_Long)((FT_Byte*)object - table->block);
      if ( (FT_ULong)in_offset >= table->capacity )
        in_offset = -1;
      while ( new_size < table->cursor + length )
        new_size += 1024;
      error = reallocate_t1_table( table, new_size );
      if ( error )
        return error;
      if ( in_offset >= 0 )
        object = table->block + in_offset;
    }
    /* add the object to the base block and adjust offset */
    table->elements[idx] = table->block + table->cursor;
    table->lengths [idx] = length;
    FT_MEM_COPY( table->block + table->cursor, object, length );
    table->cursor += length;
    return PSaux_Err_Ok;
  }
 
 
 

reply via email to

[Prev in Thread] Current Thread [Next in Thread]