Skip to content

Commit

Permalink
(Perf improvement) Keep dynamically allocated memory in filterPeaks().
Browse files Browse the repository at this point in the history
Allocating and freeing memory added an extra ~10% time in the example
model included for testing.

So, now it allocates on first use or if it needs a larger buffers.
It never deallocates, but the memory consumption penalty is worth
the runtime performance improvement, as we expect the model to be
constantly running in the bacground.
  • Loading branch information
microbit-carlos committed Oct 29, 2024
1 parent c220651 commit 9a57a68
Showing 1 changed file with 24 additions and 12 deletions.
36 changes: 24 additions & 12 deletions mlrunner/mldataprocessor.c
Original file line number Diff line number Diff line change
Expand Up @@ -90,13 +90,28 @@ MldpReturn_t filterPeaks(const float *data_in, const int in_size, float *data_ou
return MLDP_ERROR_CONFIG;
}

// micro:bit allocator panics if there is not enough memory, no need to check
float *signals = (float *)malloc(in_size * sizeof(float));
float *filtered_y = (float *)malloc(in_size * sizeof(float));
float *avg_filter = (float *)malloc(in_size * sizeof(float));
float *std_filter = (float *)malloc(in_size * sizeof(float));
static float *signals = NULL;
static float *filtered_y = NULL;
static float *avg_filter = NULL;
static float *std_filter = NULL;
static int arrays_alloc_size = 0;
float lead_in[lag];

// Keep memory allocated between calls to avoid malloc/free overhead
if (arrays_alloc_size < in_size) {
free(signals); free(filtered_y); free(avg_filter); free(std_filter);
signals = (float *)malloc(in_size * sizeof(float));
filtered_y = (float *)malloc(in_size * sizeof(float));
avg_filter = (float *)malloc(in_size * sizeof(float));
std_filter = (float *)malloc(in_size * sizeof(float));
if (signals == NULL || filtered_y == NULL || avg_filter == NULL|| std_filter == NULL) {
free(signals); free(filtered_y); free(avg_filter); free(std_filter);
signals = NULL; filtered_y = NULL; avg_filter = NULL; std_filter = NULL;
arrays_alloc_size = 0;
return MLDP_ERROR_ALLOC;
}
arrays_alloc_size = in_size;
}
memset(signals, 0, in_size * sizeof(float));
memcpy(filtered_y, data_in, lag * sizeof(float));
memcpy(lead_in, data_in, lag * sizeof(float));
Expand All @@ -113,8 +128,10 @@ MldpReturn_t filterPeaks(const float *data_in, const int in_size, float *data_ou

int peaksCounter = 0;
for (int i = lag; i < in_size; i++) {
if (fabsf(data_in[i] - avg_filter[i - 1]) > 0.1f &&
fabsf(data_in[i] - avg_filter[i - 1]) > threshold * std_filter[i - 1]
const float diff = fabsf(data_in[i] - avg_filter[i - 1]);
if (
diff > 0.1f &&
diff > threshold * std_filter[i - 1]
) {
if (data_in[i] > avg_filter[i - 1]) {
signals[i] = +1; // positive signal
Expand All @@ -141,11 +158,6 @@ MldpReturn_t filterPeaks(const float *data_in, const int in_size, float *data_ou
}
*data_out = peaksCounter;

free(signals);
free(filtered_y);
free(avg_filter);
free(std_filter);

return MLDP_SUCCESS;
}

Expand Down

0 comments on commit 9a57a68

Please sign in to comment.