• Advertisement
Sign in to follow this  
  • entries
    455
  • comments
    639
  • views
    423274

Freetype2 Take 3: Caching

Sign in to follow this  

125 views

Last night, after adding my fps and update counters to the program I discussed below I happened to mention on IRC how I'd refactored the orignal code to a series of std::for_each() loops at which point Black_Moons said something about not running a for_each() on each character, which sounded odd to me as you'd have todo that to find the correct glyphs and adjust the kerning, however my method didnt use any caching which could potentially be a slow down point.

So, I went back and looked at things today, so see how hard it would be to add a simple caching system to my orignal generator. Turns out it was pretty easy [smile]

The caching is a very simple std::map which uses the glyph index as a key and stores a structure which the glyph bitmap and the metrics of the glyph (used for height adjustments and the like in the inital setup routine).

Then, when the font is rendered the inital setup routine checks the map to see if the glyph has been rendered, if so it extracts the infomation from it, if not renderes like normal and inserts the glyph into the map for later usage.

Having completed these changes I wanted to see what, if any, change it had made in my program with regards to speed.
Now, is FPS a horrible method of seeing how long things take when your rendering at >100fps and currently I'm above 1000fps as I'm not really doing much, so I settle on timing the rendering time over a fixed amount of updates and outputting the average time to render each frame (in ms), the results were.. intresting :

Over 500 updates:
Caching Release:
Render time : 10093
Number of frames : 19037
0.530178ms per frame

Uncached Release:
Render time : 10306
Number of frames : 19680
0.523679ms per frame

Over 5000 updates:
Uncached Release:
Render time : 104062
Number of frames : 184318
0.564579ms per frame

Cached Release:
Render time : 101947
Number of frames : 183289
0.556209ms per frame

The only change between each version was the useage or not of the caching code.
As you can see, I'm rendering pretty quickly anyways so its not like this was a bottle neck, however even at such high speeds the fact that over 500 updates the uncached version was faster is... intresting. Even over 5000, while its slightly quicker to use the cached version it infact rendered ~1000 less, I'm putting this down to the update loop running closer to 50 updates a second the 2nd time.

My current theory for the difference in speed comes down to locality in memory and excess copying. When things are inserted into the std::map the extra memory is request via 'new' but this memory might not be local to each other, as such the system has to work harder to find each character.
The extra copying is down to the simpleness of the caching system, instead of keeping one copy in the cache the pointing the character data at it I'm copying the data across.

This infact is probably increasing memory fragmentation, its not a problem now as the test case is so simple, but it could prove a problem down the line.

I think I'll look into a slightly better caching system, infact iirc freetype2 has its own in build cache system, it might be worth looking at that to see if it can assist in anyway.
Sign in to follow this  


0 Comments


Recommended Comments

There are no comments to display.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Advertisement