[cairo] Performance difference between rgb and rgba with alpha 1.0 ?

Benjamin Otte otte at redhat.com
Wed May 12 22:47:35 PDT 2010

On Wed, 2010-05-12 at 11:37 -0700, Stuart Axon wrote:
> Is it worth me doing this:
>     if self._strokecolor[3] == 1:
>         cairo_ctx.set_source_rgb(*self._strokecolor[0:3])
>     else:
>         cairo_ctx.set_source_rgba(*self._strokecolor)
> or is it a waste of python cycles because set_source_rgba will notice that alpha is set to 1.0
> and run the faster path anyway ?
Optimizations belong in Cairo and not in the applications that use them.

We hink it is Cairo's job to be smart enough to do such optimizations
itself and spend a lot of time ensuring that we detect such things and
optimize them. So it were a bug if your code would ever need to do that.
In general it doesn't hurt to run a benchmark to make sure we're not
doing something stupid, but I'm very sure that in this case we are doing
the right thing.

Other examples from the top of my head where Cairo is smart enough to
- Gradients with no or only one color are treated like solid fills.
- cairo_paint_with_alpha (1.0) is treated like cairo_paint().
- A solid mask is ignored.
... and I'm sure other developers can name a lot of other ones.


More information about the cairo mailing list