Commit 58735947 authored by Tom Lane's avatar Tom Lane

Avoid repeated creation/freeing of per-subre DFAs during regex search.

In nested sub-regex trees, lower-level nodes created DFAs and then
destroyed them again before exiting, which is a bit dumb considering that
the recursive search is likely to call those nodes again later.  Instead
cache each created DFA until the end of pg_regexec().  This is basically a
space for time tradeoff, in that it might increase the maximum memory
usage.  However, in most regex patterns there are not all that many subre
nodes, so not that many DFAs --- and in any case, the peak usage occurs
when reaching the bottom recursion level, and except for alternation cases
that's going to be the same anyway.
parent 3cbfe485
This diff is collapsed.
...@@ -409,7 +409,7 @@ struct subre ...@@ -409,7 +409,7 @@ struct subre
#define PREF(f) ((f)&LOCAL) #define PREF(f) ((f)&LOCAL)
#define PREF2(f1, f2) ((PREF(f1) != 0) ? PREF(f1) : PREF(f2)) #define PREF2(f1, f2) ((PREF(f1) != 0) ? PREF(f1) : PREF(f2))
#define COMBINE(f1, f2) (UP((f1)|(f2)) | PREF2(f1, f2)) #define COMBINE(f1, f2) (UP((f1)|(f2)) | PREF2(f1, f2))
short id; /* ID of subre (1..ntree) */ short id; /* ID of subre (1..ntree-1) */
int subno; /* subexpression number (for 'b' and '(') */ int subno; /* subexpression number (for 'b' and '(') */
short min; /* min repetitions for iteration or backref */ short min; /* min repetitions for iteration or backref */
short max; /* max repetitions for iteration or backref */ short max; /* max repetitions for iteration or backref */
...@@ -446,7 +446,7 @@ struct guts ...@@ -446,7 +446,7 @@ struct guts
size_t nsub; /* copy of re_nsub */ size_t nsub; /* copy of re_nsub */
struct subre *tree; struct subre *tree;
struct cnfa search; /* for fast preliminary search */ struct cnfa search; /* for fast preliminary search */
int ntree; int ntree; /* number of subre's, less one */
struct colormap cmap; struct colormap cmap;
int FUNCPTR(compare, (const chr *, const chr *, size_t)); int FUNCPTR(compare, (const chr *, const chr *, size_t));
struct subre *lacons; /* lookahead-constraint vector */ struct subre *lacons; /* lookahead-constraint vector */
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment