perf: replace per-pixel GdkPixbuf allocation with direct Cairo buffer#688
Open
killerdevildog wants to merge 1 commit intomaoschanz:masterfrom
Open
perf: replace per-pixel GdkPixbuf allocation with direct Cairo buffer#688killerdevildog wants to merge 1 commit intomaoschanz:masterfrom
killerdevildog wants to merge 1 commit intomaoschanz:masterfrom
Conversation
… reads Replace Gdk.pixbuf_get_from_surface() single-pixel calls with direct reads from the Cairo surface buffer via surface.get_data(). This avoids allocating a temporary GdkPixbuf for every pixel read. - Add _read_pixel_rgba() helper that reads BGRA from Cairo ARGB32 buffer, handles alpha un-premultiplication, and returns RGBA bytes - Update utilities_get_rgba_for_xy() to use the new direct read path - Cache buffer data once in utilities_get_magic_path() instead of re-acquiring it per pixel in the ~50K-iteration boundary loop - Fix off-by-one in bounds check (> to >=) Measured ~4.5-5x speedup for pixel reads (3.5 us -> 0.78 us per read).
Author
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
benchmarks.patch
perf: Replace per-pixel GdkPixbuf allocation with direct Cairo buffer reads
What
utilities_get_rgba_for_xy()reads a single pixel's color from a Cairo surface. It's used by the paint bucket, color picker, and eraser tools. The paint bucket'sutilities_get_magic_path()calls it up to 50,000 times per fill operation.The old implementation called
Gdk.pixbuf_get_from_surface(surface, x, y, 1, 1)— which allocates a full GdkPixbuf GObject on the heap just to read one pixel. That allocation overhead dominated the cost.What changed
Replaced the GdkPixbuf allocation with a direct read from the Cairo surface's pixel buffer via
surface.get_data(). A new_read_pixel_rgba()helper handles the BGRA→RGBA byte reorder and alpha un-premultiplication that GdkPixbuf was doing internally.In
utilities_get_magic_path(), the surface buffer is now cached once at function entry instead of being re-acquired on every iteration of the hot loop.Also fixed an off-by-one in the bounds check (
x > width→x >= width).Benchmark results
Measured with a microbenchmark on Python 3.12.3 / Cairo 1.18.0 / GTK 3 / Linux:
No behavioral changes — same visual output, same color values returned.