]> sipb.mit.edu Git - ikiwiki.git/blob - doc/todo/Improving_the_efficiency_of_match__95__glob.mdwn
theory about differences in speed of memoize and non-memoize patches
[ikiwiki.git] / 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 >> I think so, yeah. Anyway, do you have any benchmark results handy,
10 >> Kathryn?  --[[Joey]] 
11
12 >>> See below.
13 >>> Also, would it make more sense for glob2re to return qr/^$re$/i rather than qr/$re/?  Everything that uses glob2re seems to use
14         $foo =~ /^$re$/i
15 >>> rather than /$re/ so I think that would make sense.
16 >>> -- [[KathrynAndersen]]
17
18 >>>> Git branch `smcv/ka-glob-cache` has Kathryn's patch. Git
19 >>>> branch `smcv/memoize-glob2re` does as I suggested, which
20 >>>> is less verbose than Kathryn's patch but also not as
21 >>>> fast; I'm not sure why, tbh. --[[smcv]]
22
23 >>>>> I think it's because my patch focuses on match_glob while the memoize patch focuses on `glob2re`, and `glob2re` is called in `filecheck`, `meta` and `po` as well as in `match_glob` and `match_user`; thus the memoized `glob2re` is dealing with a bigger set of globs to look up, and thus could be just that little bit slower. -- [[KathrynAndersen]]
24
25 --------------------------------------------------------------
26 Benchmarks done with Devel::Profile on the same testbed IkiWiki setup.  I'm just showing the start of the profile output, since that's what's relevant.
27
28 Before:
29 <pre>
30 time elapsed (wall):   27.4173
31 time running program:  22.5909  (82.40%)
32 time profiling (est.): 4.8264  (17.60%)
33 number of calls:       1314729
34 number of exceptions:  65
35
36 %Time    Sec.     #calls   sec/call  F  name
37 11.05    2.4969    62333   0.000040     IkiWiki::PageSpec::match_glob
38  4.10    0.9261      679   0.001364     Text::Balanced::_match_tagged
39  2.72    0.6139    59812   0.000010     IkiWiki::SuccessReason::merge_influences
40 </pre>
41
42 After:
43 <pre>
44 time elapsed (wall):   26.1843
45 time running program:  21.5673  (82.37%)
46 time profiling (est.): 4.6170  (17.63%)
47 number of calls:       1252433
48 number of exceptions:  65
49
50 %Time    Sec.     #calls   sec/call  F  name
51  7.66    1.6521    62333   0.000027     IkiWiki::PageSpec::match_glob
52  4.33    0.9336      679   0.001375     Text::Balanced::_match_tagged
53  2.81    0.6057    59812   0.000010     IkiWiki::SuccessReason::merge_influences
54 </pre>
55
56 Note that the seconds per call for match_glob in the "after" case has gone down by about a third.
57
58 K.A.
59
60 --------------------------------------------------------------
61
62 A second set of benchmarks, done by rebuilding the docwiki at commit f942c2db05e4
63 like so:
64
65     perl -Iblib/lib -d:Profile ikiwiki.in -setup docwiki.setup --no-verbose
66
67 The docwiki appears to use fewer glob matches than Kathryn's wiki.
68
69 With master:
70
71     time elapsed (wall):   29.6970
72     time running program:  24.6930  (83.15%)
73     time profiling (est.): 5.0041  (16.85%)
74     number of calls:       1359180
75     number of exceptions:  13
76
77     %Time    Sec.     #calls   sec/call  F  name
78     13.62    3.3629     3406   0.000987     Text::Balanced::_match_tagged
79     10.84    2.6773    79442   0.000034     IkiWiki::PageSpec::match_glob
80      3.08    0.7598    59454   0.000013     <anon>:IkiWiki/Plugin/inline.pm:223
81      3.07    0.7593    29830   0.000025     IkiWiki::bestlink
82      2.99    0.7378    10231   0.000072     IkiWiki::PageSpec::match_link
83
84 With my `smcv/memoize-glob2re` branch:
85
86     time elapsed (wall):   30.4931
87     time running program:  25.1248  (82.39%)
88     time profiling (est.): 5.3683  (17.61%)
89     number of calls:       1439943
90     number of exceptions:  13
91
92     %Time    Sec.     #calls   sec/call  F  name
93     13.19    3.3146     3406   0.000973     Text::Balanced::_match_tagged
94      8.41    2.1123    79442   0.000027     IkiWiki::PageSpec::match_glob
95      3.97    0.9979    86905   0.000011     Memoize::_memoizer
96      3.05    0.7654    59454   0.000013     <anon>:IkiWiki/Plugin/inline.pm:223
97      3.02    0.7576    29830   0.000025     IkiWiki::bestlink
98
99 and in a repeated run:
100
101      8.40    2.0905    79442   0.000026     IkiWiki::PageSpec::match_glob
102
103 With Kathryn's patch as seen in my `smcv/ka-glob-cache` branch:
104
105     time elapsed (wall):   27.7567
106     time running program:  22.9941  (82.84%)
107     time profiling (est.): 4.7627  (17.16%)
108     number of calls:       1279946
109     number of exceptions:  13
110
111     %Time    Sec.     #calls   sec/call  F  name
112     14.29    3.2867     3406   0.000965     Text::Balanced::_match_tagged
113      7.89    1.8136    79442   0.000023     IkiWiki::PageSpec::match_glob
114      3.30    0.7577    59454   0.000013     <anon>:IkiWiki/Plugin/inline.pm:223
115      3.24    0.7461    29830   0.000025     IkiWiki::bestlink
116      3.19    0.7332      143   0.005127  ?  IkiWiki::pagespec_match_list
117
118 and in a repeated run:
119
120      7.84    1.8253    79442   0.000023     IkiWiki::PageSpec::match_glob
121
122 --[[smcv]]
123
124 --------------------------------------------------------------
125
126
127 <pre>
128 diff --git a/IkiWiki.pm b/IkiWiki.pm
129 index 08a3d78..c187b98 100644
130 --- a/IkiWiki.pm
131 +++ b/IkiWiki.pm
132 @@ -2482,6 +2482,8 @@ sub derel ($$) {
133         return $path;
134  }
135  
136 +my %glob_cache;
137 +
138  sub match_glob ($$;@) {
139         my $page=shift;
140         my $glob=shift;
141 @@ -2489,8 +2491,15 @@ sub match_glob ($$;@) {
142         
143         $glob=derel($glob, $params{location});
144  
145 -       my $regexp=IkiWiki::glob2re($glob);
146 -       if ($page=~/^$regexp$/i) {
147 +       # Instead of converting the glob to a regex every time,
148 +       # cache the compiled regex to save time.
149 +       if (!exists $glob_cache{$glob}
150 +           or !defined $glob_cache{$glob})
151 +       {
152 +           my $re=IkiWiki::glob2re($glob);
153 +           $glob_cache{$glob} = qr/^$re$/i;
154 +       }
155 +       if ($page =~ $glob_cache{$glob}) {
156                 if (! IkiWiki::isinternal($page) || $params{internal}) {
157                         return IkiWiki::SuccessReason->new("$glob matches $page");
158                 }
159 </pre>
160 --------------------------------------------------------------