Re: Some regular-expression performance hacking
Tom Lane <tgl@sss.pgh.pa.us>
I wrote: > So I rearranged things to allocate arcs out of a common pool, and for > good measure made the state allocation code do the same thing. I was > pretty much blown away by the results: not only is the average-case > space usage about half what it is on HEAD, but the worst-case drops > by well more than a factor of ten. BTW, I was initially a bit baffled by how this could be. Per previous measurements, the average number of arcs per state is around 4; so if the code is allocating ten arcs for each state right off the bat, it's pretty clear how we could have a factor-of-two-or-so bloat problem. And I think that does explain the average-case results. But it can't possibly explain bloats of more than 10x. After further study I think this is what explains it: * The "average number of arcs" is pretty misleading, because in a large NFA some of the states have hundreds of out-arcs, while most have only a couple. * The NFA is not static; the code moves arcs around all the time. There's actually a function (moveouts) that deletes all the out-arcs of a state and creates images of them on another state. That operation can be invoked a lot of times during NFA optimization. * Once a given state has acquired N out-arcs, it keeps that pool of arc storage, even if some or all of those arcs get deleted. Indeed, the state itself could be dropped and later recycled, but it still keeps its arc pool. Unfortunately, even if it does get recycled for re-use, it's likely to be resurrected as a state with only a couple of out-arcs. So I think the explanation for 20x or 30x bloat arises from the optimize pass resulting in having a bunch of states that have large but largely unused arc pools. Getting rid of the per-state arc pools in favor of one common pool fixes that nicely. I realized while looking at this that some cycles could be shaved from moveouts, because there's no longer a reason why it can't just scribble on the arcs in-place (cf. now-obsolete comment on changearctarget()). It's late but I'll see about improving that tomorrow. regards, tom lane
Commits
-
Suppress unnecessary regex subre nodes in a couple more cases.
- 4604f83fdfe0 14.0 landed
-
Improve memory management in regex compiler.
- 0fc1af174cf7 14.0 landed
-
Extend a test case a little
- b3a9e9897ec7 14.0 cited
-
Allow complemented character class escapes within regex brackets.
- 2a0af7fe460e 14.0 landed
-
Suppress compiler warning in new regex match-all detection code.
- 3db05e76f928 14.0 landed
-
Avoid generating extra subre tree nodes for capturing parentheses.
- ea1268f6301c 14.0 landed
-
Convert regex engine's subre tree from binary to N-ary style.
- 581043089472 14.0 landed
-
Fix regex engine to suppress useless concatenation sub-REs.
- cebc1d34e520 14.0 landed
-
Recognize "match-all" NFAs within the regex engine.
- 824bf71902db 14.0 landed
-
Invent "rainbow" arcs within the regex engine.
- 08c0d6ad65f7 14.0 landed
-
Make some minor improvements in the regex code.
- 4e703d67193d 14.0 landed
-
Display the time when the process started waiting for the lock, in pg_locks, take 2
- 46d6e5f56790 14.0 cited
-
README/C-comment: document GiST's NSN value
- 8facf1ea00b7 14.0 cited
-
doc: Mention NO DEPENDS ON EXTENSION in its supported ALTER commands
- 8063d0f6f56e 14.0 cited