From 3606a1af3a88cf3a2317873ab84f6763af4e6569 Mon Sep 17 00:00:00 2001 From: Bomin Zhang Date: Mon, 17 Mar 2025 10:39:47 +0800 Subject: [PATCH] enh: improve performance of buildChildTableName (#30159) --- source/common/src/tname.c | 40 +++++++++++++++++---------------------- source/util/src/tutil.c | 14 ++++++-------- 2 files changed, 23 insertions(+), 31 deletions(-) diff --git a/source/common/src/tname.c b/source/common/src/tname.c index 6bd64fb5e6..c06a990704 100644 --- a/source/common/src/tname.c +++ b/source/common/src/tname.c @@ -16,7 +16,6 @@ #define _DEFAULT_SOURCE #include "tname.h" #include "tcommon.h" -#include "tstrbuild.h" #define VALID_NAME_TYPE(x) ((x) == TSDB_DB_NAME_T || (x) == TSDB_TABLE_NAME_T) @@ -234,43 +233,38 @@ static int compareKv(const void* p1, const void* p2) { * use stable name and tags to grearate child table name */ int32_t buildChildTableName(RandTableName* rName) { - SStringBuilder sb = {0}; - taosStringBuilderAppendStringLen(&sb, rName->stbFullName, rName->stbFullNameLen); - if (sb.buf == NULL) { - return TSDB_CODE_OUT_OF_MEMORY; - } - taosArraySort(rName->tags, compareKv); + + T_MD5_CTX context; + tMD5Init(&context); + + // add stable name + tMD5Update(&context, (uint8_t*)rName->stbFullName, rName->stbFullNameLen); + + // add tags for (int j = 0; j < taosArrayGetSize(rName->tags); ++j) { - taosStringBuilderAppendChar(&sb, ','); SSmlKv* tagKv = taosArrayGet(rName->tags, j); if (tagKv == NULL) { return TSDB_CODE_SML_INVALID_DATA; } - taosStringBuilderAppendStringLen(&sb, tagKv->key, tagKv->keyLen); - taosStringBuilderAppendChar(&sb, '='); + tMD5Update(&context, (uint8_t*)",", 1); + tMD5Update(&context, (uint8_t*)tagKv->key, tagKv->keyLen); + tMD5Update(&context, (uint8_t*)"=", 1); + if (IS_VAR_DATA_TYPE(tagKv->type)) { - taosStringBuilderAppendStringLen(&sb, tagKv->value, tagKv->length); + tMD5Update(&context, (uint8_t*)tagKv->value, tagKv->length); } else { - taosStringBuilderAppendStringLen(&sb, (char*)(&(tagKv->value)), tagKv->length); + tMD5Update(&context, (uint8_t*)(&(tagKv->value)), tagKv->length); } } - size_t len = 0; - char* keyJoined = taosStringBuilderGetResult(&sb, &len); - T_MD5_CTX context; - tMD5Init(&context); - tMD5Update(&context, (uint8_t*)keyJoined, (uint32_t)len); tMD5Final(&context); - char temp[8] = {0}; rName->ctbShortName[0] = 't'; rName->ctbShortName[1] = '_'; - for (int i = 0; i < 16; i++) { - (void)sprintf(temp, "%02x", context.digest[i]); - (void)strcat(rName->ctbShortName, temp); - } - taosStringBuilderDestroy(&sb); + taosByteArrayToHexStr(context.digest, 16, rName->ctbShortName + 2); + rName->ctbShortName[34] = 0; + return TSDB_CODE_SUCCESS; } diff --git a/source/util/src/tutil.c b/source/util/src/tutil.c index cdcef4b807..e8f1dd61fd 100644 --- a/source/util/src/tutil.c +++ b/source/util/src/tutil.c @@ -329,9 +329,9 @@ char *strbetween(char *string, char *begin, char *end) { return result; } -int32_t tintToHex(uint64_t val, char hex[]) { - const char hexstr[16] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'}; +static const char hexstr[16] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'}; +int32_t tintToHex(uint64_t val, char hex[]) { int32_t j = 0, k = 0; if (val == 0) { hex[j++] = hexstr[0]; @@ -355,13 +355,12 @@ int32_t titoa(uint64_t val, size_t radix, char str[]) { return 0; } - const char *s = "0123456789abcdef"; char buf[65] = {0}; int32_t i = 0; uint64_t v = val; do { - buf[i++] = s[v % radix]; + buf[i++] = hexstr[v % radix]; v /= radix; } while (v > 0); @@ -373,13 +372,12 @@ int32_t titoa(uint64_t val, size_t radix, char str[]) { return i; } -int32_t taosByteArrayToHexStr(char bytes[], int32_t len, char hexstr[]) { +int32_t taosByteArrayToHexStr(char bytes[], int32_t len, char str[]) { int32_t i; - char hexval[16] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'}; for (i = 0; i < len; i++) { - hexstr[i * 2] = hexval[((bytes[i] >> 4u) & 0xF)]; - hexstr[(i * 2) + 1] = hexval[(bytes[i]) & 0x0F]; + str[i * 2] = hexstr[((bytes[i] >> 4u) & 0xF)]; + str[(i * 2) + 1] = hexstr[(bytes[i]) & 0x0F]; } return 0;