High CPU usage when reusing Cairo image surfaces via create_similar in low‑traffic workloads
24K reputation · 30 Sept 2022, 21:39 UTC
Goal: Determine if reusing a single Cairo image surface created with cairo_surface_create_similar reduces allocation overhead for workloads that draw infrequently.
Constraints: The reused surface must be updated via direct pixel access or drawing calls, then marked dirty with cairo_surface_mark_dirty before each frame; cairo_surface_finish is required before destroying the surface, adding per‑frame overhead; the approach is only beneficial on backends where surface creation is expensive (e.g., image backend) and not on vector backends; thread‑safety requires external synchronization if the surface is shared.
Uncertainty: It is unclear whether the cost of calling cairo_surface_finish each iteration outweighs the savings from avoiding repeated surface allocation, and whether pattern caching or minimal drawing primitives affect the net benefit.
Questions: Does reusing a single image surface with cairo_surface_create_similar lower overall CPU time compared to allocating a new surface each frame when cairo_surface_finish is called per iteration? How does the per‑frame cost of cairo_surface_finish compare to the allocation cost for infrequent draws? Under what conditions (e.g., frequency of cairo_surface_mark_dirty, pattern reuse, backend type) does the reuse strategy yield measurable performance savings?