fix: TD-26789 nchar match slow

This commit is contained in:
factosea 2024-04-17 18:04:33 +08:00
parent a34b644640
commit 8411f25a2b
1 changed files with 36 additions and 12 deletions

View File

@ -24,6 +24,7 @@
#include "tutil.h" #include "tutil.h"
#include "types.h" #include "types.h"
#include "osString.h" #include "osString.h"
#include <pthread.h>
int32_t setChkInBytes1(const void *pLeft, const void *pRight) { int32_t setChkInBytes1(const void *pLeft, const void *pRight) {
return NULL != taosHashGet((SHashObj *)pRight, pLeft, 1) ? 1 : 0; return NULL != taosHashGet((SHashObj *)pRight, pLeft, 1) ? 1 : 0;
@ -1203,28 +1204,52 @@ int32_t comparestrRegexNMatch(const void *pLeft, const void *pRight) {
return comparestrRegexMatch(pLeft, pRight) ? 0 : 1; return comparestrRegexMatch(pLeft, pRight) ? 0 : 1;
} }
static regex_t *threadGetRegComp(const char *pPattern) {
static __thread regex_t pRegex;
static __thread char *pOldPattern;
if (NULL != pOldPattern) {
if( strcmp(pOldPattern, pPattern) == 0) {
return &pRegex;
} else {
regfree(&pRegex);
taosMemoryFree(pOldPattern);
pOldPattern == NULL;
}
}
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);
regfree(&pRegex);
return NULL;
}
return &pRegex;
}
static int32_t doExecRegexMatch(const char *pString, const char *pPattern) { static int32_t doExecRegexMatch(const char *pString, const char *pPattern) {
int32_t ret = 0; int32_t ret = 0;
regex_t regex;
char msgbuf[256] = {0}; char msgbuf[256] = {0};
regex_t *regex = threadGetRegComp(pPattern);
int32_t cflags = REG_EXTENDED; if (regex == NULL) {
if ((ret = regcomp(&regex, pPattern, cflags)) != 0) {
regerror(ret, &regex, msgbuf, tListLen(msgbuf));
uError("Failed to compile regex pattern %s. reason %s", pPattern, msgbuf);
regfree(&regex);
return 1; return 1;
} }
regmatch_t pmatch[1]; regmatch_t pmatch[1];
ret = regexec(&regex, pString, 1, pmatch, 0); ret = regexec(regex, pString, 1, pmatch, 0);
if (ret != 0 && ret != REG_NOMATCH) { if (ret != 0 && ret != REG_NOMATCH) {
regerror(ret, &regex, msgbuf, sizeof(msgbuf)); regerror(ret, regex, msgbuf, sizeof(msgbuf));
uDebug("Failed to match %s with pattern %s, reason %s", pString, pPattern, msgbuf) uDebug("Failed to match %s with pattern %s, reason %s", pString, pPattern, msgbuf)
} }
regfree(&regex);
return (ret == 0) ? 0 : 1; return (ret == 0) ? 0 : 1;
} }
@ -1245,7 +1270,6 @@ int32_t comparestrRegexMatch(const void *pLeft, const void *pRight) {
taosMemoryFree(pattern); taosMemoryFree(pattern);
return (ret == 0) ? 0 : 1; return (ret == 0) ? 0 : 1;
;
} }
int32_t comparewcsRegexMatch(const void *pString, const void *pPattern) { int32_t comparewcsRegexMatch(const void *pString, const void *pPattern) {