Tweaks and optimizations

This commit is contained in:
Bruce Hill 2021-07-29 12:45:37 -07:00
parent db969f34a9
commit bc813df3d7
5 changed files with 20 additions and 26 deletions

View File

@ -186,7 +186,7 @@ void destroy_file(file_t **f)
} }
} }
for (allocated_pat_t *next; (*f)->pats; (*f)->pats = next) { for (pat_t *next; (*f)->pats; (*f)->pats = next) {
next = (*f)->pats->next; next = (*f)->pats->next;
xfree(&(*f)->pats); xfree(&(*f)->pats);
} }

View File

@ -10,14 +10,14 @@
#define file_err(f, ...) do { fprint_line(stderr, f, __VA_ARGS__); exit(EXIT_FAILURE); } while(false) #define file_err(f, ...) do { fprint_line(stderr, f, __VA_ARGS__); exit(EXIT_FAILURE); } while(false)
struct allocated_pat_s; // declared in types.h struct pat_s; // declared in types.h
typedef struct file_s { typedef struct file_s {
struct file_s *next; struct file_s *next;
const char *filename; const char *filename;
char *memory, **lines, *start, *end; char *memory, **lines, *start, *end;
size_t nlines; size_t nlines;
struct allocated_pat_s *pats; struct pat_s *pats;
bool mmapped:1; bool mmapped:1;
} file_t; } file_t;

View File

@ -497,14 +497,14 @@ static match_t *match(def_t *defs, file_t *f, const char *str, pat_t *pat, bool
size_t len = (size_t)(m1->end - m1->start); size_t len = (size_t)(m1->end - m1->start);
pat_t *backref = new_pat(f, m1->start, m1->end, len, (ssize_t)len, BP_STRING); pat_t *backref = new_pat(f, m1->start, m1->end, len, (ssize_t)len, BP_STRING);
backref->args.string = m1->start; backref->args.string = m1->start;
def_t *defs2 = with_def(defs, m1->pat->args.capture.namelen, m1->pat->args.capture.name, backref);
def_t *defs2 = with_def(defs, m1->pat->args.capture.namelen, m1->pat->args.capture.name, backref);
++m1->refcount; { ++m1->refcount; {
m2 = match(defs2, f, m1->end, pat->args.multiple.second, ignorecase); m2 = match(defs2, f, m1->end, pat->args.multiple.second, ignorecase);
if (!m2) { // No need to keep the backref in memory if it didn't match if (!m2) { // No need to keep the backref in memory if it didn't match
for (struct allocated_pat_s **rem = &f->pats; *rem; rem = &(*rem)->next) { for (pat_t **rem = &f->pats; *rem; rem = &(*rem)->next) {
if (&(*rem)->pat == backref) { if ((*rem) == backref) {
struct allocated_pat_s *tmp = *rem; pat_t *tmp = *rem;
*rem = (*rem)->next; *rem = (*rem)->next;
free(tmp); free(tmp);
break; break;

View File

@ -34,16 +34,18 @@ static pat_t *bp_simplepattern(file_t *f, const char *str);
pat_t *new_pat(file_t *f, const char *start, const char *end, size_t minlen, ssize_t maxlen, enum pattype_e type) pat_t *new_pat(file_t *f, const char *start, const char *end, size_t minlen, ssize_t maxlen, enum pattype_e type)
{ {
static size_t next_pat_id = 1; static size_t next_pat_id = 1;
allocated_pat_t *tracker = new(allocated_pat_t); pat_t *pat = new(pat_t);
tracker->next = f->pats; *pat = (pat_t){
f->pats = tracker; .next = f->pats,
tracker->pat.type = type; .type = type,
tracker->pat.start = start; .start = start,
tracker->pat.end = end; .end = end,
tracker->pat.min_matchlen = minlen; .min_matchlen = minlen,
tracker->pat.max_matchlen = maxlen; .max_matchlen = maxlen,
tracker->pat.id = next_pat_id++; .id = next_pat_id++,
return &tracker->pat; };
f->pats = pat;
return pat;
} }
// //

10
types.h
View File

@ -46,6 +46,7 @@ struct match_s; // forward declared to resolve circular struct defs
// A struct reperesenting a BP virtual machine operation // A struct reperesenting a BP virtual machine operation
// //
typedef struct pat_s { typedef struct pat_s {
struct pat_s *next;
enum pattype_e type; enum pattype_e type;
const char *start, *end; const char *start, *end;
// The bounds of the match length (used for backtracking) // The bounds of the match length (used for backtracking)
@ -126,14 +127,5 @@ typedef struct def_s {
struct def_s *next; struct def_s *next;
} def_t; } def_t;
//
// Structure used for tracking allocated patterns, which must be freed when the
// file is freed.
//
typedef struct allocated_pat_s {
struct allocated_pat_s *next;
pat_t pat;
} allocated_pat_t;
#endif #endif
// vim: ts=4 sw=0 et cino=L2,l1,(0,W4,m1 // vim: ts=4 sw=0 et cino=L2,l1,(0,W4,m1