diff --git a/include/util/tcompare.h b/include/util/tcompare.h index 65457b287a..9694bee92d 100644 --- a/include/util/tcompare.h +++ b/include/util/tcompare.h @@ -83,6 +83,7 @@ int32_t compareLenBinaryVal(const void *pLeft, const void *pRight); int32_t comparestrRegexMatch(const void *pLeft, const void *pRight); int32_t comparestrRegexNMatch(const void *pLeft, const void *pRight); +void DestoryThreadLocalRegComp(); int32_t comparewcsRegexMatch(const void *pLeft, const void *pRight); int32_t comparewcsRegexNMatch(const void *pLeft, const void *pRight); diff --git a/source/util/src/tcompare.c b/source/util/src/tcompare.c index 53fdc96c37..26122a4a29 100644 --- a/source/util/src/tcompare.c +++ b/source/util/src/tcompare.c @@ -1203,28 +1203,57 @@ int32_t comparestrRegexNMatch(const void *pLeft, const void *pRight) { return comparestrRegexMatch(pLeft, pRight) ? 0 : 1; } +static threadlocal regex_t pRegex; +static threadlocal char *pOldPattern = NULL; +static regex_t *threadGetRegComp(const char *pPattern) { + if (NULL != pOldPattern) { + if( strcmp(pOldPattern, pPattern) == 0) { + return &pRegex; + } else { + DestoryThreadLocalRegComp(); + } + } + pOldPattern = taosMemoryMalloc(strlen(pPattern) + 1); + if (NULL == pOldPattern) { + uError("Failed to Malloc when compile regex pattern %s.", pPattern); + return NULL; + } + strcpy(pOldPattern, pPattern); + int32_t cflags = REG_EXTENDED; + int32_t ret = regcomp(&pRegex, pPattern, cflags); + if (ret != 0) { + char msgbuf[256] = {0}; + regerror(ret, &pRegex, msgbuf, tListLen(msgbuf)); + uError("Failed to compile regex pattern %s. reason %s", pPattern, msgbuf); + DestoryThreadLocalRegComp(); + return NULL; + } + return &pRegex; +} + +void DestoryThreadLocalRegComp() { + if (NULL != pOldPattern) { + regfree(&pRegex); + taosMemoryFree(pOldPattern); + pOldPattern = NULL; + } +} + static int32_t doExecRegexMatch(const char *pString, const char *pPattern) { int32_t ret = 0; - regex_t regex; char msgbuf[256] = {0}; - - int32_t cflags = REG_EXTENDED; - if ((ret = regcomp(®ex, pPattern, cflags)) != 0) { - regerror(ret, ®ex, msgbuf, tListLen(msgbuf)); - - uError("Failed to compile regex pattern %s. reason %s", pPattern, msgbuf); - regfree(®ex); + regex_t *regex = threadGetRegComp(pPattern); + if (regex == NULL) { return 1; } regmatch_t pmatch[1]; - ret = regexec(®ex, pString, 1, pmatch, 0); + ret = regexec(regex, pString, 1, pmatch, 0); if (ret != 0 && ret != REG_NOMATCH) { - regerror(ret, ®ex, msgbuf, sizeof(msgbuf)); + regerror(ret, regex, msgbuf, sizeof(msgbuf)); uDebug("Failed to match %s with pattern %s, reason %s", pString, pPattern, msgbuf) } - regfree(®ex); return (ret == 0) ? 0 : 1; } @@ -1245,7 +1274,6 @@ int32_t comparestrRegexMatch(const void *pLeft, const void *pRight) { taosMemoryFree(pattern); return (ret == 0) ? 0 : 1; - ; } int32_t comparewcsRegexMatch(const void *pString, const void *pPattern) { diff --git a/source/util/src/tworker.c b/source/util/src/tworker.c index 0712010458..d2651c1d2c 100644 --- a/source/util/src/tworker.c +++ b/source/util/src/tworker.c @@ -18,6 +18,7 @@ #include "taoserror.h" #include "tgeosctx.h" #include "tlog.h" +#include "tcompare.h" #define QUEUE_THRESHOLD (1000 * 1000) @@ -103,6 +104,7 @@ static void *tQWorkerThreadFp(SQueueWorker *worker) { } destroyThreadLocalGeosCtx(); + DestoryThreadLocalRegComp(); return NULL; }