From 7454ee0382c4727492af04c0c0a10e8dcbf380b2 Mon Sep 17 00:00:00 2001 From: const_zpc Date: Mon, 10 Jun 2024 15:56:23 +0800 Subject: [PATCH] =?UTF-8?q?=F0=9F=8E=88=20perf:=20=E5=88=A0=E9=99=A4?= =?UTF-8?q?=E6=8C=89=E5=AD=97=E7=AC=A6=E4=B8=B2=E6=9F=A5=E6=89=BE=E5=8F=82?= =?UTF-8?q?=E6=95=B0=EF=BC=8C=E5=90=8C=E6=97=B6=E6=94=AF=E6=8C=81=E6=96=87?= =?UTF-8?q?=E6=9C=ACID?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 1. 按字符串查找参数,字符串不唯一 --- cot_param.c | 43 --------------------------------- cot_param.h | 7 +++--- cot_param_type.h | 9 ++++++- examples/cmd_shell/param_demo.c | 22 ++++++++--------- test/param_test.c | 15 +++++------- 5 files changed, 29 insertions(+), 67 deletions(-) diff --git a/cot_param.c b/cot_param.c index 6f2ff86..d3bd503 100644 --- a/cot_param.c +++ b/cot_param.c @@ -353,49 +353,6 @@ cotParamInfo_t *cotParam_IterateList(const cotParamManager_t *pManager, size_t * return p; } -#if ( COT_PARAM_NAME_MAX_LENGTH > 1) -static cotParamInfo_t *FindParamByName(const cotParamManager_t *pManager, const char *pszName) -{ - for (uint16_t i = 0; i < pManager->count; ++i) - { - if (strcmp(pManager->pParamTable[i].pszName, pszName) == 0) - { - return &pManager->pParamTable[i]; - } - } - - return NULL; -} -#endif - -/** - * @brief 根据参数名称查找参数信息 - * - * @attention 无可读权限时会查找失败 - * @param pManager 参数表管理句柄 - * @param pszName 参数名称 - * @return 参数信息 - */ -const cotParamInfo_t *cotParam_FindParamByName(const cotParamManager_t *pManager, const char *pszName) -{ -#if ( COT_PARAM_NAME_MAX_LENGTH > 1) - cotParamInfo_t *pInfo; - - if (pManager != NULL) - { - pInfo = FindParamByName(pManager, pszName); - - if (pInfo->attr & COT_PARAM_ATTR_READ) - { - return pInfo; - } - } - -#endif - return NULL; -} - - static cotParamInfo_t *FindParamByID(const cotParamManager_t *pManager, uint16_t id) { for (uint16_t i = 0; i < pManager->count; ++i) diff --git a/cot_param.h b/cot_param.h index 4a57dc4..2130a21 100644 --- a/cot_param.h +++ b/cot_param.h @@ -82,7 +82,8 @@ #if COT_PARAM_USE_CUSTOM_CHECK #if (COT_PARAM_NAME_MAX_LENGTH > 1) #define COT_PARAM_ITEM_BIND_NUM_DEF_RANGE_IMPL(_id, _name, _variable, _type, _length, _attr, _defVal, _minVal, _maxVal, _check) { \ - .pszName = _name, .id = _id, .type = _type, .length = _length, .attr = _attr, \ + .name = {.textId = (size_t)(_name), .pTextString = (char*)(_name)}, \ + .id = _id, .type = _type, .length = _length, .attr = _attr, \ .unCurValuePtr.pVoid = _variable, .unDefValuePtr.pVoid = _defVal, \ .unMinValuePtr.pVoid = _minVal, .unMaxValuePtr.pVoid = _maxVal, .pfnParamCheck = _check} #else @@ -95,7 +96,8 @@ #else #if (COT_PARAM_NAME_MAX_LENGTH > 1) #define COT_PARAM_ITEM_BIND_NUM_DEF_RANGE_IMPL(_id, _name, _variable, _type, _length, _attr, _defVal, _minVal, _maxVal, _check) { \ - .pszName = _name, .id = _id, .type = _type, .length = _length, .attr = _attr, \ + .name = {.textId = (size_t)(_name), .pTextString = (char*)(_name)}, \ + .id = _id, .type = _type, .length = _length, .attr = _attr, \ .unCurValuePtr.pVoid = _variable, .unDefValuePtr.pVoid = _defVal, \ .unMinValuePtr.pVoid = _minVal, .unMaxValuePtr.pVoid = _maxVal} #else @@ -148,7 +150,6 @@ extern int cotParam_Deserialization(const cotParamManager_t* pManager, const uin extern cotParamInfo_t *cotParam_IterateList(const cotParamManager_t *pManager, size_t *psIdx); -extern const cotParamInfo_t *cotParam_FindParamByName(const cotParamManager_t *pManager, const char *pszName); extern const cotParamInfo_t *cotParam_FindParamByID(const cotParamManager_t* pManager, uint16_t id); extern const cotParamInfo_t *cotParam_FindParamByParamPtr(const cotParamManager_t* pManager, const void *pCurParam); diff --git a/cot_param_type.h b/cot_param_type.h index 1898ed8..836d1e6 100644 --- a/cot_param_type.h +++ b/cot_param_type.h @@ -117,6 +117,13 @@ typedef union { typedef int (*cotParamCheck_f)(const void *pCurParam); +typedef union +{ + size_t textId; /*!< 文本ID */ + + char *pTextString; /*!< 文本字符串 */ +} cotParamDsecStr_u; + /** * @brief 定义无内存的参数结构体信息 * @@ -124,7 +131,7 @@ typedef int (*cotParamCheck_f)(const void *pCurParam); typedef struct stuParamInfo { #if ( COT_PARAM_NAME_MAX_LENGTH > 1) - const char *pszName; /*!< 名称 */ + cotParamDsecStr_u name; /*!< 名称 */ #endif uint16_t id; /*!< 唯一ID */ uint8_t type; /*!< 类型, @enum ParamType_e */ diff --git a/examples/cmd_shell/param_demo.c b/examples/cmd_shell/param_demo.c index 7f80a89..fe13e8e 100644 --- a/examples/cmd_shell/param_demo.c +++ b/examples/cmd_shell/param_demo.c @@ -235,7 +235,7 @@ void ShowSingleParam(const cotParamInfo_t *paramInfo) switch (paramInfo->type) { case COT_PARAM_INT8: - printf(" %-4d %-24s %-10s %-6d %-8s %-10d ", paramInfo->id, paramInfo->pszName, + printf(" %-4d %-24s %-10s %-6d %-8s %-10d ", paramInfo->id, paramInfo->name.pTextString, "int8_t", paramInfo->length, Attr(paramInfo->attr), *paramInfo->unCurValuePtr.pInt8); if (paramInfo->attr & COT_PARAM_ATTR_RESET) @@ -255,7 +255,7 @@ void ShowSingleParam(const cotParamInfo_t *paramInfo) } break; case COT_PARAM_INT16: - printf(" %-4d %-24s %-10s %-6d %-8s %-10d ", paramInfo->id, paramInfo->pszName, + printf(" %-4d %-24s %-10s %-6d %-8s %-10d ", paramInfo->id, paramInfo->name.pTextString, "int16_t", paramInfo->length, Attr(paramInfo->attr), *paramInfo->unCurValuePtr.pInt16); if (paramInfo->attr & COT_PARAM_ATTR_RESET) @@ -275,7 +275,7 @@ void ShowSingleParam(const cotParamInfo_t *paramInfo) } break; case COT_PARAM_INT32: - printf(" %-4d %-24s %-10s %-6d %-8s %-10d ", paramInfo->id, paramInfo->pszName, + printf(" %-4d %-24s %-10s %-6d %-8s %-10d ", paramInfo->id, paramInfo->name.pTextString, "int32_t", paramInfo->length, Attr(paramInfo->attr), *paramInfo->unCurValuePtr.pInt32); if (paramInfo->attr & COT_PARAM_ATTR_RESET) @@ -295,7 +295,7 @@ void ShowSingleParam(const cotParamInfo_t *paramInfo) } break; case COT_PARAM_INT64: - printf(" %-4d %-24s %-10s %-6d %-8s %-10ld ", paramInfo->id, paramInfo->pszName, + printf(" %-4d %-24s %-10s %-6d %-8s %-10ld ", paramInfo->id, paramInfo->name.pTextString, "int64_t", paramInfo->length, Attr(paramInfo->attr), *paramInfo->unCurValuePtr.pInt64); if (paramInfo->attr & COT_PARAM_ATTR_RESET) @@ -315,7 +315,7 @@ void ShowSingleParam(const cotParamInfo_t *paramInfo) } break; case COT_PARAM_UINT8: - printf(" %-4d %-24s %-10s %-6d %-8s %-10u ", paramInfo->id, paramInfo->pszName, + printf(" %-4d %-24s %-10s %-6d %-8s %-10u ", paramInfo->id, paramInfo->name.pTextString, "uint8_t", paramInfo->length, Attr(paramInfo->attr), *paramInfo->unCurValuePtr.pUint8); if (paramInfo->attr & COT_PARAM_ATTR_RESET) @@ -335,7 +335,7 @@ void ShowSingleParam(const cotParamInfo_t *paramInfo) } break; case COT_PARAM_UINT16: - printf(" %-4d %-24s %-10s %-6d %-8s %-10u ", paramInfo->id, paramInfo->pszName, + printf(" %-4d %-24s %-10s %-6d %-8s %-10u ", paramInfo->id, paramInfo->name.pTextString, "uint16_t", paramInfo->length, Attr(paramInfo->attr), *paramInfo->unCurValuePtr.pUint16); if (paramInfo->attr & COT_PARAM_ATTR_RESET) @@ -355,7 +355,7 @@ void ShowSingleParam(const cotParamInfo_t *paramInfo) } break; case COT_PARAM_UINT32: - printf(" %-4d %-24s %-10s %-6d %-8s %-10u ", paramInfo->id, paramInfo->pszName, + printf(" %-4d %-24s %-10s %-6d %-8s %-10u ", paramInfo->id, paramInfo->name.pTextString, "uint32_t", paramInfo->length, Attr(paramInfo->attr), *paramInfo->unCurValuePtr.pUint32); if (paramInfo->attr & COT_PARAM_ATTR_RESET) @@ -375,7 +375,7 @@ void ShowSingleParam(const cotParamInfo_t *paramInfo) } break; case COT_PARAM_UINT64: - printf(" %-4d %-24s %-10s %-6d %-8s %-10lu ", paramInfo->id, paramInfo->pszName, + printf(" %-4d %-24s %-10s %-6d %-8s %-10lu ", paramInfo->id, paramInfo->name.pTextString, "uint64_t", paramInfo->length, Attr(paramInfo->attr), *paramInfo->unCurValuePtr.pUint64); if (paramInfo->attr & COT_PARAM_ATTR_RESET) @@ -395,7 +395,7 @@ void ShowSingleParam(const cotParamInfo_t *paramInfo) } break; case COT_PARAM_FLOAT: - printf(" %-4d %-24s %-10s %-6d %-8s %-10f ", paramInfo->id, paramInfo->pszName, + printf(" %-4d %-24s %-10s %-6d %-8s %-10f ", paramInfo->id, paramInfo->name.pTextString, "float", paramInfo->length, Attr(paramInfo->attr), *paramInfo->unCurValuePtr.pFloat); if (paramInfo->attr & COT_PARAM_ATTR_RESET) @@ -415,7 +415,7 @@ void ShowSingleParam(const cotParamInfo_t *paramInfo) } break; case COT_PARAM_DOUBLE: - printf(" %-4d %-24s %-10s %-6d %-8s %-10f ", paramInfo->id, paramInfo->pszName, + printf(" %-4d %-24s %-10s %-6d %-8s %-10f ", paramInfo->id, paramInfo->name.pTextString, "double", paramInfo->length, Attr(paramInfo->attr), *paramInfo->unCurValuePtr.pDouble); if (paramInfo->attr & COT_PARAM_ATTR_RESET) @@ -436,7 +436,7 @@ void ShowSingleParam(const cotParamInfo_t *paramInfo) break; #if COT_PARAM_USE_STRING_TYPE case COT_PARAM_STRING: - printf(" %-4d %-24s %-10s %-6d %-8s %-10s ", paramInfo->id, paramInfo->pszName, + printf(" %-4d %-24s %-10s %-6d %-8s %-10s ", paramInfo->id, paramInfo->name.pTextString, "string", paramInfo->length, Attr(paramInfo->attr), paramInfo->unCurValuePtr.pString); if (paramInfo->attr & COT_PARAM_ATTR_RESET) diff --git a/test/param_test.c b/test/param_test.c index 62fd297..528541b 100644 --- a/test/param_test.c +++ b/test/param_test.c @@ -130,7 +130,7 @@ void test_ParamInit(void) TEST_ASSERT_EQUAL_UINT(sg_tParamManager.pParamTable[0].length, 2); TEST_ASSERT_EQUAL_UINT(sg_tParamManager.pParamTable[0].type, COT_PARAM_INT16); TEST_ASSERT_EQUAL_HEX8(sg_tParamManager.pParamTable[0].attr, COT_PARAM_ATTR_WR); - TEST_ASSERT_EQUAL_STRING(sg_tParamManager.pParamTable[0].pszName, "g_test_1"); + TEST_ASSERT_EQUAL_STRING(sg_tParamManager.pParamTable[0].name.pTextString, "g_test_1"); TEST_ASSERT_EQUAL_PTR(sg_tParamManager.pParamTable[0].unCurValuePtr.pVoid , &g_test_1); TEST_ASSERT_NULL(sg_tParamManager.pParamTable[0].unDefValuePtr.pVoid); TEST_ASSERT_NULL(sg_tParamManager.pParamTable[0].unMinValuePtr.pVoid); @@ -140,7 +140,7 @@ void test_ParamInit(void) TEST_ASSERT_EQUAL_UINT(sg_tParamManager.pParamTable[1].length, 2); TEST_ASSERT_EQUAL_UINT(sg_tParamManager.pParamTable[1].type, COT_PARAM_UINT16); TEST_ASSERT_EQUAL_HEX8(sg_tParamManager.pParamTable[1].attr, COT_PARAM_ATTR_WR | COT_PARAM_ATTR_RESET); - TEST_ASSERT_EQUAL_STRING(sg_tParamManager.pParamTable[1].pszName, "g_test_2"); + TEST_ASSERT_EQUAL_STRING(sg_tParamManager.pParamTable[1].name.pTextString, "g_test_2"); TEST_ASSERT_EQUAL_PTR(sg_tParamManager.pParamTable[1].unCurValuePtr.pVoid , &g_test_2); TEST_ASSERT_NOT_NULL(sg_tParamManager.pParamTable[1].unDefValuePtr.pVoid); TEST_ASSERT_NULL(sg_tParamManager.pParamTable[1].unMinValuePtr.pVoid); @@ -150,7 +150,7 @@ void test_ParamInit(void) TEST_ASSERT_EQUAL_UINT(sg_tParamManager.pParamTable[2].length, 4); TEST_ASSERT_EQUAL_UINT(sg_tParamManager.pParamTable[2].type, COT_PARAM_FLOAT); TEST_ASSERT_EQUAL_HEX8(sg_tParamManager.pParamTable[2].attr, COT_PARAM_ATTR_READ | COT_PARAM_ATTR_RESET | COT_PARAM_ATTR_RANGE); - TEST_ASSERT_EQUAL_STRING(sg_tParamManager.pParamTable[2].pszName, "g_test_float"); + TEST_ASSERT_EQUAL_STRING(sg_tParamManager.pParamTable[2].name.pTextString, "g_test_float"); TEST_ASSERT_EQUAL_PTR(sg_tParamManager.pParamTable[2].unCurValuePtr.pVoid , &g_test_float); TEST_ASSERT_NOT_NULL(sg_tParamManager.pParamTable[2].unDefValuePtr.pVoid); TEST_ASSERT_NOT_NULL(sg_tParamManager.pParamTable[2].unMinValuePtr.pVoid); @@ -162,7 +162,7 @@ void test_ParamInit(void) TEST_ASSERT_EQUAL_UINT(sg_tParamManager.pParamTable[4].length, sizeof(g_test_str)); TEST_ASSERT_EQUAL_UINT(sg_tParamManager.pParamTable[4].type, COT_PARAM_STRING); TEST_ASSERT_EQUAL_HEX8(sg_tParamManager.pParamTable[4].attr, COT_PARAM_ATTR_WR | COT_PARAM_ATTR_RESET | COT_PARAM_ATTR_RANGE); - TEST_ASSERT_EQUAL_STRING(sg_tParamManager.pParamTable[4].pszName, "g_test_str"); + TEST_ASSERT_EQUAL_STRING(sg_tParamManager.pParamTable[4].name.pTextString, "g_test_str"); TEST_ASSERT_EQUAL_PTR(sg_tParamManager.pParamTable[4].unCurValuePtr.pVoid , g_test_str); TEST_ASSERT_NOT_NULL(sg_tParamManager.pParamTable[4].unDefValuePtr.pVoid); TEST_ASSERT_NOT_NULL(sg_tParamManager.pParamTable[4].unMinValuePtr.pVoid); @@ -172,7 +172,7 @@ void test_ParamInit(void) TEST_ASSERT_EQUAL_UINT(sg_tParamManager.pParamTable[3].length, 10); TEST_ASSERT_EQUAL_UINT(sg_tParamManager.pParamTable[3].type, COT_PARAM_STRING); TEST_ASSERT_EQUAL_HEX8(sg_tParamManager.pParamTable[3].attr, COT_PARAM_ATTR_WR | COT_PARAM_ATTR_RESET | COT_PARAM_ATTR_RANGE); - TEST_ASSERT_EQUAL_STRING(sg_tParamManager.pParamTable[3].pszName, "g_test_str"); + TEST_ASSERT_EQUAL_STRING(sg_tParamManager.pParamTable[3].name.pTextString, "g_test_str"); TEST_ASSERT_EQUAL_PTR(sg_tParamManager.pParamTable[3].unCurValuePtr.pVoid , COT_PARAM_STR_CUR_VALUE(g_test_str)); TEST_ASSERT_NOT_NULL(sg_tParamManager.pParamTable[3].unDefValuePtr.pVoid); TEST_ASSERT_NOT_NULL(sg_tParamManager.pParamTable[3].unMinValuePtr.pVoid); @@ -379,10 +379,7 @@ void test_IterateParam(void) void test_FindParam(void) { const cotParamInfo_t *paramInfo; -#if COT_PARAM_USE_64_BIT_LENGTH - paramInfo = cotParam_FindParamByName(&sg_tParamManager, "g_test_double"); - TEST_ASSERT_EQUAL_PTR(&sg_ParamTable[3], paramInfo); -#endif + paramInfo = cotParam_FindParamByID(&sg_tParamManager, 3); TEST_ASSERT_EQUAL_PTR(&sg_ParamTable[2], paramInfo);