region-utils: Reduce temporary allocations

This applies the optimizations from 0c55e87d8f to serveral
similar places in region-utils.

Part-of: <https://gitlab.gnome.org/GNOME/mutter/-/merge_requests/1565>
This commit is contained in:
Robert Mader 2020-11-16 13:43:52 +01:00
parent a50a5944a7
commit 91c9416259

View File

@ -26,6 +26,15 @@
#include <math.h> #include <math.h>
#define META_REGION_MAX_STACK_RECTS 256
#define META_REGION_CREATE_RECTANGLE_ARRAY_SCOPED(n_rects, rects) \
g_autofree cairo_rectangle_int_t *G_PASTE(__n, __LINE__) = NULL; \
if (n_rects < META_REGION_MAX_STACK_RECTS) \
rects = g_newa (cairo_rectangle_int_t, n_rects); \
else \
rects = G_PASTE(__n, __LINE__) = g_new (cairo_rectangle_int_t, n_rects);
/* MetaRegionBuilder */ /* MetaRegionBuilder */
/* Various algorithms in this file require unioning together a set of rectangles /* Various algorithms in this file require unioning together a set of rectangles
@ -191,8 +200,7 @@ meta_region_scale_double (cairo_region_t *region,
return cairo_region_copy (region); return cairo_region_copy (region);
n_rects = cairo_region_num_rectangles (region); n_rects = cairo_region_num_rectangles (region);
META_REGION_CREATE_RECTANGLE_ARRAY_SCOPED (n_rects, rects);
rects = g_malloc (sizeof(cairo_rectangle_int_t) * n_rects);
for (i = 0; i < n_rects; i++) for (i = 0; i < n_rects; i++)
{ {
cairo_region_get_rectangle (region, i, &rects[i]); cairo_region_get_rectangle (region, i, &rects[i]);
@ -203,8 +211,6 @@ meta_region_scale_double (cairo_region_t *region,
scaled_region = cairo_region_create_rectangles (rects, n_rects); scaled_region = cairo_region_create_rectangles (rects, n_rects);
g_free (rects);
return scaled_region; return scaled_region;
} }
@ -219,8 +225,7 @@ meta_region_scale (cairo_region_t *region, int scale)
return cairo_region_copy (region); return cairo_region_copy (region);
n_rects = cairo_region_num_rectangles (region); n_rects = cairo_region_num_rectangles (region);
META_REGION_CREATE_RECTANGLE_ARRAY_SCOPED (n_rects, rects);
rects = g_malloc (sizeof(cairo_rectangle_int_t) * n_rects);
for (i = 0; i < n_rects; i++) for (i = 0; i < n_rects; i++)
{ {
cairo_region_get_rectangle (region, i, &rects[i]); cairo_region_get_rectangle (region, i, &rects[i]);
@ -232,8 +237,6 @@ meta_region_scale (cairo_region_t *region, int scale)
scaled_region = cairo_region_create_rectangles (rects, n_rects); scaled_region = cairo_region_create_rectangles (rects, n_rects);
g_free (rects);
return scaled_region; return scaled_region;
} }
@ -389,8 +392,7 @@ meta_region_transform (const cairo_region_t *region,
return cairo_region_copy (region); return cairo_region_copy (region);
n_rects = cairo_region_num_rectangles (region); n_rects = cairo_region_num_rectangles (region);
META_REGION_CREATE_RECTANGLE_ARRAY_SCOPED (n_rects, rects);
rects = g_new0 (cairo_rectangle_int_t, n_rects);
for (i = 0; i < n_rects; i++) for (i = 0; i < n_rects; i++)
{ {
cairo_region_get_rectangle (region, i, &rects[i]); cairo_region_get_rectangle (region, i, &rects[i]);
@ -404,8 +406,6 @@ meta_region_transform (const cairo_region_t *region,
transformed_region = cairo_region_create_rectangles (rects, n_rects); transformed_region = cairo_region_create_rectangles (rects, n_rects);
g_free (rects);
return transformed_region; return transformed_region;
} }
@ -437,8 +437,7 @@ meta_region_crop_and_scale (cairo_region_t *region,
} }
n_rects = cairo_region_num_rectangles (region); n_rects = cairo_region_num_rectangles (region);
META_REGION_CREATE_RECTANGLE_ARRAY_SCOPED (n_rects, rects);
rects = g_new0 (cairo_rectangle_int_t, n_rects);
for (i = 0; i < n_rects; i++) for (i = 0; i < n_rects; i++)
{ {
cairo_region_get_rectangle (region, i, &rects[i]); cairo_region_get_rectangle (region, i, &rects[i]);
@ -452,7 +451,5 @@ meta_region_crop_and_scale (cairo_region_t *region,
viewport_region = cairo_region_create_rectangles (rects, n_rects); viewport_region = cairo_region_create_rectangles (rects, n_rects);
g_free (rects);
return viewport_region; return viewport_region;
} }