From dbc8ca1d262be1cd0cb0434e1cadb936a5553204 Mon Sep 17 00:00:00 2001 From: Dean Lee Date: Tue, 2 Jun 2020 05:00:43 +0800 Subject: [PATCH] dmonitoring : use memory cache to avoid malloc/free on every frame (#1599) * use memory cache * use template function to return buffer * inline function * const size_t * use std::vector instead of kj::array old-commit-hash: cdb48cc180bd22fbad75785d07a7f47da959880c --- selfdrive/modeld/models/dmonitoring.cc | 23 ++++++++++++----------- selfdrive/modeld/models/dmonitoring.h | 11 ++++++----- 2 files changed, 18 insertions(+), 16 deletions(-) diff --git a/selfdrive/modeld/models/dmonitoring.cc b/selfdrive/modeld/models/dmonitoring.cc index 21fac61c28..6dc90718d1 100644 --- a/selfdrive/modeld/models/dmonitoring.cc +++ b/selfdrive/modeld/models/dmonitoring.cc @@ -26,6 +26,14 @@ void dmonitoring_init(DMonitoringModelState* s) { s->is_rhd_checked = false; } +template +static inline T *get_buffer(std::vector &buf, const size_t size) { + if (buf.size() < size) { + buf.resize(size); + } + return buf.data(); +} + DMonitoringResult dmonitoring_eval_frame(DMonitoringModelState* s, void* stream_buf, int width, int height) { uint8_t *raw_buf = (uint8_t*) stream_buf; @@ -39,8 +47,7 @@ DMonitoringResult dmonitoring_eval_frame(DMonitoringModelState* s, void* stream_ int resized_width = MODEL_WIDTH; int resized_height = MODEL_HEIGHT; - uint8_t *cropped_buf = new uint8_t[cropped_width*cropped_height*3/2]; - uint8_t *cropped_y_buf = cropped_buf; + uint8_t *cropped_y_buf = get_buffer(s->cropped_buf, cropped_width*cropped_height*3/2); uint8_t *cropped_u_buf = cropped_y_buf + (cropped_width * cropped_height); uint8_t *cropped_v_buf = cropped_u_buf + ((cropped_width/2) * (cropped_height/2)); @@ -53,8 +60,7 @@ DMonitoringResult dmonitoring_eval_frame(DMonitoringModelState* s, void* stream_ } } else { // not tested - uint8_t *premirror_cropped_buf = new uint8_t[cropped_width*cropped_height*3/2]; - uint8_t *premirror_cropped_y_buf = premirror_cropped_buf; + uint8_t *premirror_cropped_y_buf = get_buffer(s->premirror_cropped_buf, cropped_width*cropped_height*3/2); uint8_t *premirror_cropped_u_buf = premirror_cropped_y_buf + (cropped_width * cropped_height); uint8_t *premirror_cropped_v_buf = premirror_cropped_u_buf + ((cropped_width/2) * (cropped_height/2)); for (int r = 0; r < height/2; r++) { @@ -70,10 +76,9 @@ DMonitoringResult dmonitoring_eval_frame(DMonitoringModelState* s, void* stream_ cropped_u_buf, cropped_width/2, cropped_v_buf, cropped_width/2, cropped_width, cropped_height); - delete[] premirror_cropped_buf; } - uint8_t *resized_buf = new uint8_t[resized_width*resized_height*3/2]; + uint8_t *resized_buf = get_buffer(s->resized_buf, resized_width*resized_height*3/2); uint8_t *resized_y_buf = resized_buf; uint8_t *resized_u_buf = resized_y_buf + (resized_width * resized_height); uint8_t *resized_v_buf = resized_u_buf + ((resized_width/2) * (resized_height/2)); @@ -90,8 +95,7 @@ DMonitoringResult dmonitoring_eval_frame(DMonitoringModelState* s, void* stream_ mode); int yuv_buf_len = (MODEL_WIDTH/2) * (MODEL_HEIGHT/2) * 6; // Y|u|v -> y|y|y|y|u|v - - float *net_input_buf = new float[yuv_buf_len]; + float *net_input_buf = get_buffer(s->net_input_buf, yuv_buf_len); // one shot conversion, O(n) anyway // yuvframe2tensor, normalize for (int r = 0; r < MODEL_HEIGHT/2; r++) { @@ -120,10 +124,7 @@ DMonitoringResult dmonitoring_eval_frame(DMonitoringModelState* s, void* stream_ //fwrite(net_input_buf, MODEL_HEIGHT*MODEL_WIDTH*3/2, sizeof(float), dump_yuv_file2); //fclose(dump_yuv_file2); - delete[] cropped_buf; - delete[] resized_buf; s->m->execute(net_input_buf, yuv_buf_len); - delete[] net_input_buf; DMonitoringResult ret = {0}; memcpy(&ret.face_orientation, &s->output[0], sizeof ret.face_orientation); diff --git a/selfdrive/modeld/models/dmonitoring.h b/selfdrive/modeld/models/dmonitoring.h index e8a496dee9..c0a0cf4463 100644 --- a/selfdrive/modeld/models/dmonitoring.h +++ b/selfdrive/modeld/models/dmonitoring.h @@ -1,10 +1,8 @@ -#ifndef DMONITORING_H -#define DMONITORING_H - +#pragma once +#include #include "common/util.h" #include "commonmodel.h" #include "runners/run.h" - #include "messaging.hpp" #ifdef __cplusplus @@ -31,6 +29,10 @@ typedef struct DMonitoringModelState { bool is_rhd; bool is_rhd_checked; float output[OUTPUT_SIZE]; + std::vector resized_buf; + std::vector cropped_buf; + std::vector premirror_cropped_buf; + std::vector net_input_buf; } DMonitoringModelState; void dmonitoring_init(DMonitoringModelState* s); @@ -42,4 +44,3 @@ void dmonitoring_free(DMonitoringModelState* s); } #endif -#endif