possible improvement
[ikiwiki] / doc / todo / Improving_the_efficiency_of_match__95__glob.mdwn
1 I've been profiling my IkiWiki to try to improve speed (with many pages makes speed even more important) and I've written a patch to improve the speed of match_glob.  This matcher is a good one to improve the speed of, because it gets called so many times.
2
3 Here's my patch - please consider it! -- [[KathrynAndersen]]
4
5 > It seems to me as though changing `glob2re` to return qr/$re/, and calling
6 > `memoize(glob2re)` next to the other memoize calls, would be a less
7 > verbose way to do this? --[[smcv]]
8
9 --------------------------------------------------------------
10 <pre>
11 diff --git a/IkiWiki.pm b/IkiWiki.pm
12 index 08a3d78..c187b98 100644
13 --- a/IkiWiki.pm
14 +++ b/IkiWiki.pm
15 @@ -2482,6 +2482,8 @@ sub derel ($$) {
16         return $path;
17  }
18  
19 +my %glob_cache;
20 +
21  sub match_glob ($$;@) {
22         my $page=shift;
23         my $glob=shift;
24 @@ -2489,8 +2491,15 @@ sub match_glob ($$;@) {
25         
26         $glob=derel($glob, $params{location});
27  
28 -       my $regexp=IkiWiki::glob2re($glob);
29 -       if ($page=~/^$regexp$/i) {
30 +       # Instead of converting the glob to a regex every time,
31 +       # cache the compiled regex to save time.
32 +       if (!exists $glob_cache{$glob}
33 +           or !defined $glob_cache{$glob})
34 +       {
35 +           my $re=IkiWiki::glob2re($glob);
36 +           $glob_cache{$glob} = qr/^$re$/i;
37 +       }
38 +       if ($page =~ $glob_cache{$glob}) {
39                 if (! IkiWiki::isinternal($page) || $params{internal}) {
40                         return IkiWiki::SuccessReason->new("$glob matches $page");
41                 }
42 </pre>
43 --------------------------------------------------------------