use direct pointer

This commit is contained in:
xsren 2024-08-05 14:42:30 +08:00
parent cc4dccfd91
commit a61149047f
2 changed files with 63 additions and 8 deletions

View File

@ -1296,6 +1296,7 @@ UsingRegex **getRegComp(const char *pPattern) {
(*ppUsingRegex)->lastUsedTime = taosGetTimestampSec(); (*ppUsingRegex)->lastUsedTime = taosGetTimestampSec();
return ppUsingRegex; return ppUsingRegex;
} }
printf("getRegComp , ...");
UsingRegex *pUsingRegex = taosMemoryMalloc(sizeof(UsingRegex)); UsingRegex *pUsingRegex = taosMemoryMalloc(sizeof(UsingRegex));
if (pUsingRegex == NULL) { if (pUsingRegex == NULL) {
terrno = TSDB_CODE_OUT_OF_MEMORY; terrno = TSDB_CODE_OUT_OF_MEMORY;
@ -1340,12 +1341,15 @@ void releaseRegComp(UsingRegex **regex){
taosHashRelease(sRegexCache.regexHash, regex); taosHashRelease(sRegexCache.regexHash, regex);
} }
static threadlocal UsingRegex ** ppRegex; static threadlocal UsingRegex ** ppUsingRegex;
static threadlocal regex_t * pRegex;
static threadlocal char *pOldPattern = NULL; static threadlocal char *pOldPattern = NULL;
void DestoryThreadLocalRegComp() { void DestoryThreadLocalRegComp() {
if (NULL != pOldPattern) { if (NULL != pOldPattern) {
releaseRegComp(ppRegex); releaseRegComp(ppUsingRegex);
taosMemoryFree(pOldPattern); taosMemoryFree(pOldPattern);
ppUsingRegex = NULL;
pRegex = NULL;
pOldPattern = NULL; pOldPattern = NULL;
} }
} }
@ -1353,15 +1357,15 @@ void DestoryThreadLocalRegComp() {
int32_t threadGetRegComp(regex_t **regex, const char *pPattern) { int32_t threadGetRegComp(regex_t **regex, const char *pPattern) {
if (NULL != pOldPattern) { if (NULL != pOldPattern) {
if (strcmp(pOldPattern, pPattern) == 0) { if (strcmp(pOldPattern, pPattern) == 0) {
*regex = &(*ppRegex)->pRegex; *regex = pRegex;
return 0; return 0;
} else { } else {
DestoryThreadLocalRegComp(); DestoryThreadLocalRegComp();
} }
} }
UsingRegex **pUsingRegex = getRegComp(pPattern); UsingRegex **ppRegex = getRegComp(pPattern);
if (pUsingRegex == NULL) { if (ppRegex == NULL) {
return 1; return 1;
} }
pOldPattern = (char *)taosMemoryMalloc(strlen(pPattern) + 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; return TSDB_CODE_OUT_OF_MEMORY;
} }
strcpy(pOldPattern, pPattern); strcpy(pOldPattern, pPattern);
ppRegex = pUsingRegex; ppUsingRegex = ppRegex;
*regex = &(*pUsingRegex)->pRegex; pRegex = &((*ppUsingRegex)->pRegex);
*regex = &(*ppRegex)->pRegex;
return 0; return 0;
} }

View File

@ -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); 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) { TEST(testCase, regexCacheTest5) {
int times = 10000; int times = 10000;
int count = 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); 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);
}
*/ */