[cairo] Reducing CPU usage in News Ticker Implementation

Behdad Esfahbod behdad at behdad.org
Fri Aug 31 13:45:03 PDT 2007

On Fri, 2007-08-31 at 16:17 -0400, Chris Wilson wrote:
> Darwin Bautista (djclue917 at gmail.com) said: 
> > I just want to know if there's a more efficient way of implementing a
> > news ticker. Currently, I'm using my own "Cairo-fied" version of the
> > ticker widget used in pyt!ck (
> > http://whatah.nyunderground.net/code/pytick/ ).
> As pointed out, caching the layout in a surface pattern is the standard
> response to such speed issues.
> > The problem is that
> > the ticker consumes a lot of CPU when the Pango layout is relatively
> > big (377 character-long text, DejaVu Serif Semi-Condensed 40) since X
> > performs a lot of redrawing every second (e.g., 50 redraws/second if
> > the scroll timeout is set to 20ms).
> The question arises as to identify the bottleneck and is it worth
> fixing. In this case, the issue appears to be that we are causing X to
> render glyphs that will never reach the widget. On the Cairo side we
> have a few opportunities to cull glyphs that will never be visible. As a
> demonstration, the attached patch culls the glyphs before adding them to
> the Xlib glyph stream - as it appears to be the easiest place to perform
> the culling.
> Applying this patch causes the CPU usage to become negligible, a drop
> from 20% on my machine. (I've also attached the updated ticker.py that I
> was running as my benchmark.) However, it remains to be seen whether it
> brings sufficient benefit across the desktop to justify its overhead.

A way to make it very fast is to keep *per scaled font* the bounding box
of all glyphs used off it ever all combined.  That will give you a safe
limit for glyph bounding box so you don't have to access the glyphs at
all.  Then you can move it up to gstate.  I was more thinking along the

box = cr.clip_extents()
box.x1 == scaled_font->max_x1
box.y1 == scaled_font->max_y1
box.x2 == scaled_font->max_x2
box.y2 == scaled_font->max_y2

for glyph in glyphs:
	if box.x1 < glyph.x < box.x2 and box.y1 < glyph.y < box.y2:
		# add glyph...

> --
> Chris Wilson


"Those who would give up Essential Liberty to purchase a little
 Temporary Safety, deserve neither Liberty nor Safety."
        -- Benjamin Franklin, 1759

More information about the cairo mailing list