From 922dfe227e0a157f895c025b8f8590e2bfc59f23 Mon Sep 17 00:00:00 2001 From: Ben Pfaff Date: Sat, 29 Jan 2022 10:52:49 -0800 Subject: [PATCH] math: Avoid unneeded extra allocations for fixed-size data structures. This seems generally a bit cleaner to me. --- src/math/percentiles.c | 4 +--- src/math/percentiles.h | 2 ++ src/math/trimmed-mean.c | 4 +--- src/math/trimmed-mean.h | 2 ++ src/math/tukey-hinges.c | 7 ++----- src/math/tukey-hinges.h | 1 + 6 files changed, 9 insertions(+), 11 deletions(-) diff --git a/src/math/percentiles.c b/src/math/percentiles.c index b43fed4616..1b4ff13632 100644 --- a/src/math/percentiles.c +++ b/src/math/percentiles.c @@ -139,8 +139,6 @@ static void destroy (struct statistic *stat) { struct percentile *ptl = UP_CAST (stat, struct percentile, parent.parent); - struct order_stats *os = &ptl->parent; - free (os->k); free (ptl); } @@ -159,7 +157,7 @@ percentile_create (double p, double W) ptl->w = W; os->n_k = 2; - os->k = xcalloc (2, sizeof (*os->k)); + os->k = ptl->k; os->k[0].tc = W * p; os->k[1].tc = (W + 1.0) * p; diff --git a/src/math/percentiles.h b/src/math/percentiles.h index 7af157b230..86fd641add 100644 --- a/src/math/percentiles.h +++ b/src/math/percentiles.h @@ -44,6 +44,8 @@ struct percentile double g2; double g2_star; + + struct k k[2]; }; /* Create the Pth percentile. diff --git a/src/math/trimmed-mean.c b/src/math/trimmed-mean.c index b643651a26..4840fd2d66 100644 --- a/src/math/trimmed-mean.c +++ b/src/math/trimmed-mean.c @@ -44,8 +44,6 @@ static void destroy (struct statistic *s) { struct trimmed_mean *tm = UP_CAST (s, struct trimmed_mean, parent.parent); - struct order_stats *os = &tm->parent; - free (os->k); free (tm); } @@ -57,7 +55,7 @@ trimmed_mean_create (double W, double tail) struct statistic *stat = &os->parent; os->n_k = 2; - os->k = xcalloc (2, sizeof (*os->k)); + os->k = tm->k; assert (tail >= 0); assert (tail <= 1); diff --git a/src/math/trimmed-mean.h b/src/math/trimmed-mean.h index baa84bc24e..8803a127bc 100644 --- a/src/math/trimmed-mean.h +++ b/src/math/trimmed-mean.h @@ -32,6 +32,8 @@ struct trimmed_mean double w; double tail; + + struct k k[2]; }; struct trimmed_mean * trimmed_mean_create (double W, double c_min); diff --git a/src/math/tukey-hinges.c b/src/math/tukey-hinges.c index aaf4881afa..b59b878ba6 100644 --- a/src/math/tukey-hinges.c +++ b/src/math/tukey-hinges.c @@ -64,10 +64,7 @@ static void destroy (struct statistic *s) { struct tukey_hinges *th = UP_CAST (s, struct tukey_hinges, parent.parent); - struct order_stats *os = &th->parent; - - free (os->k); - free (s); + free (th); }; struct tukey_hinges * @@ -81,7 +78,7 @@ tukey_hinges_create (double W, double c_min) assert (c_min >= 0); os->n_k = 3; - os->k = xcalloc (3, sizeof (*os->k)); + os->k = th->k; if (c_min >= 1.0) { diff --git a/src/math/tukey-hinges.h b/src/math/tukey-hinges.h index 4b509da1d4..5a6e079509 100644 --- a/src/math/tukey-hinges.h +++ b/src/math/tukey-hinges.h @@ -25,6 +25,7 @@ struct tukey_hinges { struct order_stats parent; + struct k k[3]; }; struct tukey_hinges * tukey_hinges_create (double W, double c_min); -- 2.30.2