[cairo] Creating A Cairo Context Seems Quite Quick

Behdad Esfahbod behdad.esfahbod at gmail.com
Sun Apr 19 01:37:50 PDT 2015


On 15-04-18 03:30 AM, Lawrence D'Oliveiro wrote:
> I just added methods to my Path and Path.Segment types in Qahirah
> <https://github.com/ldo/qahirah> to compute extents and return
> flattened versions. These create a temporary Cairo context so I can use
> its append_path and copy_path_flat calls to do all the work. The
> overhead of this doesn’t seem too bad.
> 
> As a baseline (on an already-created Cairo context), the Python sequence
> 
>     ctx.new_path().append_path(path).copy_path_flat()
> 
> takes 140µs for a simple rectangular path on my Core i7 (averaged over
> 100,000 iterations using the Python timeit module), while
> 
>     ctx.new_path().append_path(path).path_extents
> 
> takes 59µs. For the new methods I have added, which create a new context
> on every call,
> 
>     path.flatten()
> 
> takes 162µs, while
> 
>     path.extents
> 
> takes 83µs. So the overhead in the former is about 16%, in the latter
> about 40%. Well short of the order-of-magnitude-or-more I was
> expecting/fearing...

Yes, cairo optimizes for that by caching freed context objects for reuse so
there's no malloc() overhead.  All that is left is clearing the object.


More information about the cairo mailing list