[augeas-devel] [PATCH 3 of 4] Don't call regexp_match when we know that there is no match

David Lutterkort dlutter at redhat.com
Fri Aug 8 22:46:25 UTC 2008


3 files changed, 23 insertions(+), 2 deletions(-)
src/put.c    |   13 +++++++++++--
src/regexp.c |    8 ++++++++
src/syntax.h |    4 ++++


# HG changeset patch
# User David Lutterkort <dlutter at redhat.com>
# Date 1218235537 25200
# Node ID 5dfbe6d1739f5478286ef99ea16315a9a6816c3b
# Parent  cf27e53b0a9213ddf03e45603617b09418285474
Don't call regexp_match when we know that there is no match

It is common in the put direction that we try and split when we're already
inside a leaf node. In that case, we know that there is no possible split,
and therefore can save the trouble of calling regexp_match.

diff -r cf27e53b0a92 -r 5dfbe6d1739f src/put.c
--- a/src/put.c	Fri Aug 08 15:43:43 2008 -0700
+++ b/src/put.c	Fri Aug 08 15:45:37 2008 -0700
@@ -174,8 +174,18 @@
     int count = 0;
     struct split *outer = state->split;
     struct re_registers regs;
-    struct split *split = NULL;
+    struct split *split = NULL, *tail = NULL;
     struct regexp *atype = lens->atype;
+
+    /* Fast path for leaf nodes, which will always lead to an empty split */
+    if (outer->tree == NULL && strlen(outer->labels) == 0
+        && regexp_is_empty_pattern(atype)) {
+        for (int i=0; i < lens->nchildren; i++) {
+            tail = split_append(&split, tail, NULL, NULL,
+                                outer->labels, 0, 0);
+        }
+        return split;
+    }
 
     if (atype->re != NULL)
         atype->re->regs_allocated = REGS_UNALLOCATED;
@@ -196,7 +206,6 @@
 
     struct tree *cur = outer->tree;
     int reg = 1;
-    struct split *tail = NULL;
     for (int i=0; i < lens->nchildren; i++) {
         assert(reg < regs.num_regs);
         assert(regs.start[reg] != -1);
diff -r cf27e53b0a92 -r 5dfbe6d1739f src/regexp.c
--- a/src/regexp.c	Fri Aug 08 15:43:43 2008 -0700
+++ b/src/regexp.c	Fri Aug 08 15:45:37 2008 -0700
@@ -67,6 +67,14 @@
         free(regexp->re);
     }
     free(regexp);
+}
+
+int regexp_is_empty_pattern(struct regexp *r) {
+    for (char *s = r->pattern->str; *s; s++) {
+        if (*s != '(' && *s != ')')
+            return 0;
+    }
+    return 1;
 }
 
 struct regexp *make_regexp_literal(struct info *info, const char *text) {
diff -r cf27e53b0a92 -r 5dfbe6d1739f src/syntax.h
--- a/src/syntax.h	Fri Aug 08 15:43:43 2008 -0700
+++ b/src/syntax.h	Fri Aug 08 15:45:37 2008 -0700
@@ -148,6 +148,10 @@
  * of INFO is taken.
  */
 struct regexp *make_regexp(struct info *info, char *pat);
+
+/* Return 1 if R is an empty pattern, i.e. one consisting of nothing but
+   '(' and ')' characters, 0 otherwise */
+int regexp_is_empty_pattern(struct regexp *r);
 
 /* Make a regexp that matches TEXT literally; the string TEXT
  * is not used by the returned rgexp and must be freed by the caller




More information about the augeas-devel mailing list