[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [Nano-devel] WIP: Use caching for regex
From: |
Devin Hussey |
Subject: |
Re: [Nano-devel] WIP: Use caching for regex |
Date: |
Sat, 3 Nov 2018 19:46:01 -0400 |
I'm sorry, I got carried away. I guess this isn't the right solution.
And yeah, I did exaggerate about the strlen stuff.
Anyways, I did get a benefit from this, as it does reduce the constant
freeing and reallocating.
Regex is still compiled twice, though, as nregcomp is stupid. I
considered storing the result from nregcomp, but memory usage went way
up.
diff --git a/src/color.c b/src/color.c
index 6605a713..0c5cea62 100644
--- a/src/color.c
+++ b/src/color.c
@@ -141,17 +141,16 @@ void color_init(void)
bool found_in_list(regexlisttype *head, const char *shibboleth)
{
regexlisttype *item;
- regex_t rgx;
for (item = head; item != NULL; item = item->next) {
- regcomp(&rgx, fixbounds(item->full_regex), NANO_REG_EXTENDED);
+ if (item->regex == NULL) {
+ item->regex = (regex_t *)nmalloc(sizeof(regex_t));
+ regcomp(item->regex, fixbounds(item->full_regex), NANO_R
EG_EXTENDED);
+ }
- if (regexec(&rgx, shibboleth, 0, NULL, 0) == 0) {
- regfree(&rgx);
+ if (regexec(item->regex, shibboleth, 0, NULL, 0) == 0) {
return TRUE;
}
-
- regfree(&rgx);
}
return FALSE;
diff --git a/src/nano.h b/src/nano.h
index 3007881d..5fb770ca 100644
--- a/src/nano.h
+++ b/src/nano.h
@@ -214,6 +219,8 @@ typedef struct regexlisttype {
/* A regex string to match things that imply a certain
syntax. */
struct regexlisttype *next;
/* The next regex. */
+ regex_t *regex;
+ /* The compiled regex. Will be NULL if not initialized. */
} regexlisttype;
diff --git a/src/rcfile.c b/src/rcfile.c
index d8a2d1fe..c09b8ea9 100644
--- a/src/rcfile.c
+++ b/src/rcfile.c
@@ -831,6 +831,7 @@ void grab_and_store(const char *kind, char *ptr,
regexlisttype **storage)
newthing = (regexlisttype *)nmalloc(sizeof(regexlisttype));
newthing->full_regex = mallocstrcpy(NULL, regexstring);
newthing->next = NULL;
+ newthing->regex = NULL;
if (lastthing == NULL)
*storage = newthing;