Skip to content

Commit d1cba64

Browse files
committed
Reduce the number of allocs
1 parent da6f26d commit d1cba64

File tree

1 file changed

+8
-8
lines changed

1 file changed

+8
-8
lines changed

kitty/decorations.c

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,7 @@ typedef struct Range {
232232
} Range;
233233

234234
typedef struct Limit { double upper, lower; } Limit;
235+
typedef struct FloatPoint { double x, y; } FloatPoint;
235236

236237
typedef struct Canvas {
237238
uint8_t *mask;
@@ -744,11 +745,10 @@ draw_parametrized_curve_with_derivative_and_antialiasing(
744745
double step = 1.0 / larger_dim;
745746
uint cap = 2 * larger_dim;
746747
const double min_step = step / 1000., max_step = step;
747-
RAII_ALLOC(double, x_samples, malloc(sizeof(double) * cap));
748-
RAII_ALLOC(double, y_samples, malloc(sizeof(double) * cap));
748+
RAII_ALLOC(FloatPoint, samples, malloc(sizeof(FloatPoint) * cap));
749+
if (!samples) fatal("Out of memory");
749750
while (true) {
750-
x_samples[i] = xfunc(curve_data, t) + x_offset;
751-
y_samples[i] = yfunc(curve_data, t) + y_offset;
751+
samples[i] = (FloatPoint){xfunc(curve_data, t) + x_offset, yfunc(curve_data, t) + y_offset};
752752
if (t >= 1.0) break;
753753
// Dynamically adjust step size based on curve's derivative
754754
double dx = x_prime(curve_data, t), dy = y_prime(curve_data, t);
@@ -759,8 +759,8 @@ draw_parametrized_curve_with_derivative_and_antialiasing(
759759
i++;
760760
if (i >= cap) {
761761
cap *= 2;
762-
x_samples = realloc(x_samples, sizeof(x_samples[0]) * cap);
763-
y_samples = realloc(y_samples, sizeof(x_samples[0]) * cap);
762+
samples = realloc(samples, sizeof(samples[0]) * cap);
763+
if (!samples) fatal("Out of memory");
764764
}
765765
}
766766
const uint num_samples = i;
@@ -775,8 +775,8 @@ draw_parametrized_curve_with_derivative_and_antialiasing(
775775

776776
// Find the closest point on the curve to the pixel center by sampling the curve.
777777
for (uint i = 0; i < num_samples; ++i) {
778-
double dx = x_samples[i] - pixel_center_x;
779-
double dy = y_samples[i] - pixel_center_y;
778+
double dx = samples[i].x - pixel_center_x;
779+
double dy = samples[i].y - pixel_center_y;
780780
double dist_sq = dx * dx + dy * dy;
781781
if (min_dist_sq < 0 || dist_sq < min_dist_sq) min_dist_sq = dist_sq;
782782
}

0 commit comments

Comments
 (0)