I’m optimizing my plugin, which has many animated components. The most time I spend in any one function, about 5%, is in CGContextGetClipBoundingBox. This is always called by either CoreGraphicsContext::isClipEmpty() or CoreGraphicsContext::getClipBounds(). I wouldn’t have guessed that this API call would be so slow.
CoreGraphicsContext.mm is lean and to the point—I hate to think about mucking it up. But in my case, a little bit of extra crud here to prevent CGContextGetClipBoundingBox being called can be a big win.
One idea would be to calculate the clip bounding box locally when clipToRectangle, clipToRectangleList, and excludeClipRectangle are called. When the context is created, the clip bounds is in a known simple state covering the entire context. When one of these three calls is made, it’s easy to calculate the resulting bounds. So we can return our local version of the clip bounds and avoid the expensive call.
When clipToPath or clipToImageAlpha are called, we would set a flag called something like clipRegionIsComplex, indicating that CGContextGetClipBoundingBox needs to be called. Calculating the boundary of the added clip area here would be possible but starts to look like more added code for less win.
When saveState() is called the clipRegionIsComplex flag would get saved with that state. This makes it possible to get back to a simple state without adding any new methods.
I’m going to work on other stuff for a bit and let Jules and the community ponder this idea. If the above seems a reasonable patch concept after that I’ll go ahead and try it.