make match_glob faster - patch!
[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 --------------------------------------------------------------
6 <pre>
7 diff --git a/IkiWiki.pm b/IkiWiki.pm
8 index 08a3d78..c187b98 100644
9 --- a/IkiWiki.pm
10 +++ b/IkiWiki.pm
11 @@ -2482,6 +2482,8 @@ sub derel ($$) {
12         return $path;
13  }
14  
15 +my %glob_cache;
16 +
17  sub match_glob ($$;@) {
18         my $page=shift;
19         my $glob=shift;
20 @@ -2489,8 +2491,15 @@ sub match_glob ($$;@) {
21         
22         $glob=derel($glob, $params{location});
23  
24 -       my $regexp=IkiWiki::glob2re($glob);
25 -       if ($page=~/^$regexp$/i) {
26 +       # Instead of converting the glob to a regex every time,
27 +       # cache the compiled regex to save time.
28 +       if (!exists $glob_cache{$glob}
29 +           or !defined $glob_cache{$glob})
30 +       {
31 +           my $re=IkiWiki::glob2re($glob);
32 +           $glob_cache{$glob} = qr/^$re$/i;
33 +       }
34 +       if ($page =~ $glob_cache{$glob}) {
35                 if (! IkiWiki::isinternal($page) || $params{internal}) {
36                         return IkiWiki::SuccessReason->new("$glob matches $page");
37                 }
38 </pre>
39 --------------------------------------------------------------