This is a more general question on how to write code for Cairo with performance in mind.
What gives better performance:
Or is there no significant difference? Or maybe only for paint but not stroke/fill? Does it depend on the number of drawn items?
A real world example (high level code), the calculations abc are simple:
save cr
set_source_color cr rgb1
loop 100 times
do calculations abc
draw box
end loop
fill cr
restore cr
set_source_color cr rgb2
loop 100 times
do calculations abc
draw arc
end loop
fill cr
restore cr
set_source_color cr rgb3
loop 100 times
do calculations abc
draw rectangle
end loop
fill cr
restore cr
or
loop 100 times
do calculations abc
set_source_color cr rgb1
draw box
fill cr
set_source_color cr rgb2
draw arc
fill cr
set_source_color cr rgb3
draw rectangle
fill cr
end loop
Of course this only works if all the boxes ect have the same color. Otherwise option 2 has to be used.
This is meant as a more general question, the given example is just an illustration to make my example scenario clear. Do not hesitate to answer in depth/technical.
When you draw on a window, most drawing operations happen on GPU and i guess your calculations on CPU. Each processing unit has, dependent on the operation, throughput and processing time. To avoid waiting much for synchronization, you should mix your calculations and drawings to let the units work in parallel.
In case of an image surface as target, everything would happen on CPU, but internal multithreading might result in a parallel rendering pipeline as well.
I recommend to use your first approach.