Skip to content

Commit 4604f83

Browse files
committed
Suppress unnecessary regex subre nodes in a couple more cases.
This extends the changes made in commit cebc1d3, teaching parseqatom() to generate fewer or cheaper subre nodes in some edge cases. The case of interest here is a quantified atom that is "messy" only because it has greediness opposite to what preceded it (whereas captures and backrefs are intrinsically messy). In this case we don't need an iteration node, since we don't care where the sub-matches of the quantifier are; and we might also not need a second concatenation node. This seems of only marginal real-world use according to my testing, but I wanted to get it in before wrapping up this series of regex performance fixes. Discussion: https://postgr.es/m/1340281.1613018383@sss.pgh.pa.us
1 parent 0c3405c commit 4604f83

File tree

1 file changed

+39
-0
lines changed

1 file changed

+39
-0
lines changed

src/backend/regex/regcomp.c

+39
Original file line numberDiff line numberDiff line change
@@ -1216,6 +1216,23 @@ parseqatom(struct vars *v,
12161216
/* rest of branch can be strung starting from atom->end */
12171217
s2 = atom->end;
12181218
}
1219+
else if (!(atom->flags & (CAP | BACKR)))
1220+
{
1221+
/*
1222+
* If there's no captures nor backrefs in the atom being repeated, we
1223+
* don't really care where the submatches of the iteration are, so we
1224+
* don't need an iteration node. Make a plain DFA node instead.
1225+
*/
1226+
EMPTYARC(s, atom->begin); /* empty prefix */
1227+
repeat(v, atom->begin, atom->end, m, n);
1228+
f = COMBINE(qprefer, atom->flags);
1229+
t = subre(v, '=', f, atom->begin, atom->end);
1230+
NOERR();
1231+
freesubre(v, atom);
1232+
*atomp = t;
1233+
/* rest of branch can be strung starting from t->end */
1234+
s2 = t->end;
1235+
}
12191236
else if (m > 0 && !(atom->flags & BACKR))
12201237
{
12211238
/*
@@ -1271,6 +1288,10 @@ parseqatom(struct vars *v,
12711288
t->flags |= COMBINE(t->flags, t->child->sibling->flags);
12721289
top->flags |= COMBINE(top->flags, t->flags);
12731290

1291+
/* neither t nor top could be directly marked for capture as yet */
1292+
assert(t->capno == 0);
1293+
assert(top->capno == 0);
1294+
12741295
/*
12751296
* At this point both top and t are concatenation (op == '.') subres,
12761297
* and we have top->child = prefix of branch, top->child->sibling = t,
@@ -1291,6 +1312,24 @@ parseqatom(struct vars *v,
12911312
top->child = t->child;
12921313
freesrnode(v, t);
12931314
}
1315+
1316+
/*
1317+
* Otherwise, it's possible that t->child is not messy in itself, but
1318+
* we considered it messy because its greediness conflicts with what
1319+
* preceded it. Then it could be that the combination of t->child and
1320+
* the rest of the branch is also not messy, in which case we can get
1321+
* rid of the child concatenation by merging t->child and the rest of
1322+
* the branch into one plain DFA node.
1323+
*/
1324+
else if (t->child->op == '=' &&
1325+
t->child->sibling->op == '=' &&
1326+
!MESSY(UP(t->child->flags | t->child->sibling->flags)))
1327+
{
1328+
t->op = '=';
1329+
t->flags = COMBINE(t->child->flags, t->child->sibling->flags);
1330+
freesubreandsiblings(v, t->child);
1331+
t->child = NULL;
1332+
}
12941333
}
12951334
else
12961335
{

0 commit comments

Comments
 (0)