diff --git a/source/util/src/tcompare.c b/source/util/src/tcompare.c index 90c1441af5..af2205c590 100644 --- a/source/util/src/tcompare.c +++ b/source/util/src/tcompare.c @@ -1296,6 +1296,7 @@ UsingRegex **getRegComp(const char *pPattern) { (*ppUsingRegex)->lastUsedTime = taosGetTimestampSec(); return ppUsingRegex; } + printf("getRegComp , ..."); UsingRegex *pUsingRegex = taosMemoryMalloc(sizeof(UsingRegex)); if (pUsingRegex == NULL) { terrno = TSDB_CODE_OUT_OF_MEMORY; @@ -1340,12 +1341,15 @@ void releaseRegComp(UsingRegex **regex){ taosHashRelease(sRegexCache.regexHash, regex); } -static threadlocal UsingRegex ** ppRegex; +static threadlocal UsingRegex ** ppUsingRegex; +static threadlocal regex_t * pRegex; static threadlocal char *pOldPattern = NULL; void DestoryThreadLocalRegComp() { if (NULL != pOldPattern) { - releaseRegComp(ppRegex); + releaseRegComp(ppUsingRegex); taosMemoryFree(pOldPattern); + ppUsingRegex = NULL; + pRegex = NULL; pOldPattern = NULL; } } @@ -1353,15 +1357,15 @@ void DestoryThreadLocalRegComp() { int32_t threadGetRegComp(regex_t **regex, const char *pPattern) { if (NULL != pOldPattern) { if (strcmp(pOldPattern, pPattern) == 0) { - *regex = &(*ppRegex)->pRegex; + *regex = pRegex; return 0; } else { DestoryThreadLocalRegComp(); } } - UsingRegex **pUsingRegex = getRegComp(pPattern); - if (pUsingRegex == NULL) { + UsingRegex **ppRegex = getRegComp(pPattern); + if (ppRegex == NULL) { return 1; } pOldPattern = (char *)taosMemoryMalloc(strlen(pPattern) + 1); @@ -1370,8 +1374,9 @@ int32_t threadGetRegComp(regex_t **regex, const char *pPattern) { return TSDB_CODE_OUT_OF_MEMORY; } strcpy(pOldPattern, pPattern); - ppRegex = pUsingRegex; - *regex = &(*pUsingRegex)->pRegex; + ppUsingRegex = ppRegex; + pRegex = &((*ppUsingRegex)->pRegex); + *regex = &(*ppRegex)->pRegex; return 0; } diff --git a/source/util/test/regexTest.cpp b/source/util/test/regexTest.cpp index 26857bce88..3cd8f5c88c 100644 --- a/source/util/test/regexTest.cpp +++ b/source/util/test/regexTest.cpp @@ -205,7 +205,8 @@ TEST(testCase, regexCacheTest4) { printf("'%s' and '%s' take place by turn(per %d count) regex(new) %d times:%" PRIu64 " us.\n", s1, s2, count, times, t3 - t2); } -/* + +/* It is not a good idea to test this case, because it will take a long time. TEST(testCase, regexCacheTest5) { int times = 10000; int count = 10000; @@ -254,4 +255,53 @@ TEST(testCase, regexCacheTest5) { printf("'%s' and '%s' take place by turn(per %d count) regex(new) %d times:%" PRIu64 " us.\n", s1, s2, count, times, t3 - t2); } + +TEST(testCase, regexCacheTest6) { + int times = 10000; + int count = 1000; + char s1[] = "abc%*"; + char s2[] = "abc"; + auto start = std::chrono::high_resolution_clock::now(); + + uint64_t t0 = taosGetTimestampUs(); + for (int i = 0; i < times; i++) { + for (int j = 0; j < count; ++j) { + UsingRegex** rex = getRegComp(s1); + } + for (int j = 0; j < count; ++j) { + UsingRegex** rex = getRegComp(s2); + } + } + uint64_t t1 = taosGetTimestampUs(); + + printf("'%s' and '%s' take place by turn(per %d count) regex(current) %d times:%" PRIu64 " us.\n", s1, s2, count, times, t1 - t0); + + uint64_t t2 = taosGetTimestampUs(); + for (int i = 0; i < times; i++) { + for (int j = 0; j < count; ++j) { + regex_t* rex = threadGetRegComp1(s1); + } + for (int j = 0; j < count; ++j) { + regex_t* rex = threadGetRegComp1(s2); + } + } + uint64_t t3 = taosGetTimestampUs(); + + printf("'%s' and '%s' take place by turn(per %d count) regex(before) %d times:%" PRIu64 " us.\n", s1, s2, count, times, t3 - t2); + + t2 = taosGetTimestampUs(); + for (int i = 0; i < times; i++) { + for (int j = 0; j < count; ++j) { + regex_t* rex = NULL; + (void)threadGetRegComp(&rex, s1); + } + for (int j = 0; j < count; ++j) { + regex_t* rex = NULL; + (void)threadGetRegComp(&rex, s2); + } + } + t3 = taosGetTimestampUs(); + + printf("'%s' and '%s' take place by turn(per %d count) regex(new) %d times:%" PRIu64 " us.\n", s1, s2, count, times, t3 - t2); +} */