summaryrefslogtreecommitdiff
path: root/t/op/svleak.t
diff options
context:
space:
mode:
authorDavid Mitchell <davem@iabyn.com>2010-05-03 13:57:58 +0100
committerDavid Mitchell <davem@iabyn.com>2010-05-03 14:29:54 +0100
commit2e64971a6530d2645969bc489f564bfd3ce64993 (patch)
tree8b5ab3ff672755f1ca62bd040b2240dbb5719934 /t/op/svleak.t
parenta23e6e2012eae03dbd049a058d24b0ce29357c76 (diff)
downloadperl-2e64971a6530d2645969bc489f564bfd3ce64993.tar.gz
tries: don't allocate memory at runtime
This is an indirect fix for [perl #74484] Regex causing exponential runtime+mem usage The trie runtime code was doing more SAVETMPS than FREETMPS and was thus growing a large tmps stack on heavy backtracking. Rather than fixing this directly, I rewrote part of the trie code so that it no longer needs to allocate memory in S_regmatch (it still does in find_byclass()). The basic issue is that multiple branches in the trie may trigger an accept state; for example: "abcd" =~ /xyz/abcd.*X|ab.*Y|/ here, words (branches) 2 and 3 are accept states. The original approach was, at run time, to create a list of accepted word numbers and the character positions of the end of each of those words. Then run the rest of the pattern for each word in the list in turn (in word index order). This requires memory for the list to be allocated and freed. The new approach involves creating extra info at compile time; in particular, for each word, a pointer to the previous accepted word (if any) in the state tree. For example for the above pattern, part of the state tree may be q b c d 1 -> 2 -> 3 -> 4 -> 5 (#3) (#2) (e.g. at state 1, if the next char is 'a', we transition to state 2). Here, state 3 is an accept state with word #3, and 5 is an accept state with word #2. So we build a table indexed by word number, which has wordinfo[2] = 3, wordinfo[3] = 0, thus building the word chain 2->3->0. At run time we run the trie to completion, and remember the word associated with the longest accept state (word #2 above). Then by following back the chain of .prev fields, we can produce a list of all accepting words. We then iteratively find the smallest-numbered (ie LH-most) word in the chain, and run with it. On failure and backtrack, we find the next-smallest and so on. Since we are no longer recording the end-position of each word in the string, we have to recalculate this for each backtrack. We initially record the end-position of the shortest accepting word, and given that we know the length of each word, we can calculate the new position each time as an offset from that first word. Depending on unicode and folding, that calculation can be cheap or expensive. This algorithm is optimised for the typical case where there are a small number (<= 2) accepting states. This patch creates a new compile-time array, trie->wordinfo[], indexed by word number, which contains relevant info about each word. This also supersedes the old trie->newword[] array, whose function of recording "overspills" of multiple words per accept state, is now handled as part of the wordinfo[].prev chain.
Diffstat (limited to 't/op/svleak.t')
-rw-r--r--t/op/svleak.t5
1 files changed, 1 insertions, 4 deletions
diff --git a/t/op/svleak.t b/t/op/svleak.t
index 7b1f8f08c7..07c2efcb71 100644
--- a/t/op/svleak.t
+++ b/t/op/svleak.t
@@ -70,7 +70,4 @@ sub STORE { $_[0]->[$_[1]] = $_[2] }
# [perl #74484] repeated tries leaked SVs on the tmps stack
-{
- local $TODO = 'not fixed yet';
- leak_expr(5, 0, q{"YYYYYa" =~ /.+?(a(.+?)|b)/ }, "trie leak");
-}
+leak_expr(5, 0, q{"YYYYYa" =~ /.+?(a(.+?)|b)/ }, "trie leak");