]> sipb.mit.edu Git - ikiwiki.git/blob - doc/todo/should_optimise_pagespecs.mdwn
mod_auth_openid
[ikiwiki.git] / doc / todo / should_optimise_pagespecs.mdwn
1 I think there is a problem in my "dependency graph". As an example, 
2 [here](http://poivron.org/~nil/misc/ikiwiki_buggy_index) is the index 
3 ikiwiki generated for [my site](http://poivron.org/~nil/misc/ikiwiki_buggy_index)
4 (note that the site changed since this index was generated).
5
6 Some **HUGE** dependencies appear, clearly non optimal, like
7
8     depends = A| B | A | C | A | D | A | E | A | F | A | G | ....
9
10 or 
11
12     depends= A | B | C | D | A | B | C | D | A | B | C | D | ....
13
14 Couldn't isolate the cause, but some sources for this problem may be:
15
16 * related to the img module
17 * easily observable in my sire because one of my pages includes 80 resized images
18
19 Other special things in my templates and site:
20
21 * a sidebar with \[[!include pages="notes/\*" template=foo]] while notes.mdwn has 
22   a \[[!include pages="notes/*"]] and uses the sidebar; removed it, doesn't change
23 * a template (biblio.tmpl) calling the "img" plugin with a template parameter as the
24   image filename; removed it, doesn't change
25 * some strange games with tags whose page calls a "map" directive to show other tags
26   shile tags are also used in tagclouds (in the sidebar and in the main pages)
27 * ...
28
29 I observed these problems (same *kind*, I didn't check in details) on
30  
31 * ikiwiki 2.00gpa1 + v5.8.4 + Debian 3.1
32 * ikiwiki 2.3 + v5.8.8 + Ubuntu 7.04
33
34 I can think about reducung the size of my wiki source and making it available online for analysis.
35
36 -- NicolasLimare
37
38 > As long as these dependencies don't grow over time (ie, when a page is
39 > edited and nothing changed that should add a dependency), I wouldn't
40 > worry about them. There are many things that can cause non-optimal
41 > dependencies to be recorded. For one thing, if you inline something, ikiwiki
42 > creates a dependency like:
43
44 > (PageSpec) or (file1 or file2 or file3 ...)
45
46 > Where fileN are all the files that the PageSpec currently matches. (This
47 > is ncessary to detect when a currently inlined file is deleted, and know
48 > the inlining page needs an update.) Now consider what it does if you have
49 > a single page with two inline statements, that inline the same set of
50 > stuff twice:
51
52 > ((PageSpec) or (file1 or file2 or file3 ...) or (PageSpec) or (file1 or file2 or file3 ...)
53 >
54 > Clearly non-optimal, indeed.
55
56 > Ikiwiki doesn't bother to simplify complex PageSpecs
57 > because it's difficult to do, and because all they use is some disk
58 > space. Consider what ikiwiki uses these dependencies for.
59 > All it wants to know is: does the PageSpec for this page it's considering
60 > rebuilding match any of the pages that have changed? Determining this is
61 > a simple operation -- the PageSpec is converted to perl code. The perl
62 > code is run.
63
64 > So the total impact of an ugly dependency like this is:
65
66 > 1. Some extra data read/written to disk.
67 > 2. Some extra space in memory.
68 > 3. A bit more data for the PageSpec translation code to handle. But that
69 >    code is quite fast.
70 > 4. Typically one extra function call when the generated perl code is run.
71 >    Ie, when the expression on the left-hand side fails, which typically
72 >    happens after one (inexpensive) function call, it has to check
73 >    the identical expression on the right hand side.
74
75 > So this is at best a wishlist todo item, not a bug. A PageSpec simplifier
76 > (or improved `pagespec_merge()` function) could be written and improve
77 > ikiwiki's memory and disk usage, but would it actually speed it up any?
78 > We'd have to see the code to the simplifier to know.
79
80 > --[[Joey]]
81
82 [[!template id=gitbranch branch=smcv/ready/optimize-depends author="[[smcv]]"]]
83
84 >> I've been looking at optimizing ikiwiki for a site using
85 >> [[plugins/contrib/album]] (which produces a lot of pages) and it seems
86 >> that checking which pages depend on which pages does take a significant
87 >> amount of time. The optimize-depends branch in my git repository
88 >> avoids using `pagespec_merge()` for this (indeed it's no longer used
89 >> at all), and instead represents dependencies as a list of pagespecs
90 >> rather than a single pagespec. This does turn out to be faster, although
91 >> not as much as I'd like. --[[smcv]]
92
93 >>> [[Merged|done]] --[[smcv]]
94
95 >>> I just wanted to note that there is a whole long discussion of dependencies and pagespecs on the [[todo/tracking_bugs_with_dependencies]] page. -- [[Will]]
96
97 >>>> Yeah, I had a look at that (as the only other mention of `pagespec_merge`).
98 >>>> I think I might have solved some of the problems mentioned there,
99 >>>> actually - `pagespec_merge` no longer needs to exist in my branch (although
100 >>>> I haven't actually deleted it), because the "or" operation is now done in
101 >>>> the Perl code, rather than by merging pagespecs and translating. --[[smcv]]
102
103 [[!template id=gitbranch branch=smcv/ready/remove-pagespec-merge author="[[smcv]]"]]
104
105 >>>>> I've now added a patch to the end of that branch that deletes
106 >>>>> `pagespec_merge` almost entirely (we do need to keep a copy around, in
107 >>>>> ikiwiki-transition, but that copy doesn't have to be optimal or support
108 >>>>> future features like [[tracking_bugs_with_dependencies]]). --[[smcv]]
109
110 ---
111
112 Some questions on your optimize-depends branch. --[[Joey]]
113
114 In saveindex it still or'd together the depends list, but the `{depends}`
115 field seems only useful for backwards compatability (ie, ikiwiki-transition
116 uses it still), and otherwise just bloats the index.
117
118 > If it's acceptable to declare that downgrading IkiWiki requires a complete
119 > rebuild, I'm happy with that. I'd prefer to keep the (simple form of the)
120 > transition done automatically during a load/save cycle, rather than
121 > requiring ikiwiki-transition to be run; we should probably say in NEWS
122 > that the performance increase won't fully apply until the next
123 > rebuild. --[[smcv]]
124
125 >> It is acceptable not to support downgrades.
126 >> I don't think we need a NEWS file update since any sort of refresh,
127 >> not just a full rebuild, will cause the indexdb to be loaded and saved,
128 >> enabling the optimisation. --[[Joey]]
129
130 >>> A refresh will load the current dependencies from `{depends}` and save
131 >>> them as-is as a one-element `{dependslist}`; only a rebuild will replace
132 >>> the single complex pagespec with a long list of simpler pagespecs.
133 >>> --[[smcv]]
134
135 Is an array the right data structure? `add_depends` has to loop through the
136 array to avoid dups, it would be better if a hash were used there. Since
137 inline (and other plugins) explicitly add all linked pages, each as a
138 separate item, the list can get rather long, and that single add_depends
139 loop has suddenly become O(N^2) to the number of pages, which is something
140 to avoid..
141
142 > I was also thinking about this (I've been playing with some stuff based on the
143 > `remove-pagespec-merge` branch).  A hash, by itself, is not optimal because
144 > the dependency list holds two things: page names and page specs.  The hash would
145 > work well for the page names, but you'll still need to iterate through the page specs.
146 > I was thinking of keeping a list and a hash.  You use the list for pagespecs
147 > and the hash for individual page names.  To make this work you need to adjust the
148 > API so it knows which you're adding.  -- [[Will]]
149
150 > I wasn't thinking about a lookup hash, just a dedup hash, FWIW.
151 > --[[Joey]]
152
153 >> I was under the impression from previous code review that you preferred
154 >> to represent unordered sets as lists, rather than hashes with dummy
155 >> values. If I was wrong, great, I'll fix that and it'll probably go
156 >> a bit faster. --[[smcv]]
157
158 >>> It depends, really. And it'd certianly make sense to benchmark such a
159 >>> change. --[[Joey]]
160
161 >>>> Benchmarked, below. --[[smcv]]
162
163 Also, since a lot of places are calling add_depends in a loop, it probably
164 makes sense to just make it accept a list of dependencies to add. It'll be
165 marginally faster, probably, and should allow for better optimisation
166 when adding a lot of depends at once.
167
168 > That'd be an API change; perhaps marginally faster, but I don't
169 > see how it would allow better optimisation if we're de-duplicating
170 > anyway? --[[smcv]]
171
172 >> Well, I was thinking that it might be sufficient to build a `%seen`
173 >> hash of dependencies inside `add_depends`, if the places that call
174 >> it lots were changed to just call it once. Of course the only way to
175 >> tell is benchmarking. --[[Joey]]
176
177 >>> It doesn't seem that it significantly affects performance either way.
178 >>> --[[smcv]]
179
180 In Render.pm, we now have a triply nested loop, which is a bit
181 scary for efficiency. It seems there should be a way to
182 rework this code so it can use the optimised `pagespec_match_list`,
183 and/or hoist some of the inner loop calculations (like the `pagename`)
184 out.
185
186 > I don't think the complexity is any greater than it was: I've just
187 > moved one level of "loop" out of the generated Perl, to be
188 > in visible code. I'll see whether some of it can be hoisted, though.
189 > --[[smcv]]
190
191 >> The call to `pagename` is the only part I can see that's clearly
192 >> run more often than before. That function is pretty inexpensive, but..
193 >> --[[Joey]]
194
195 >>> I don't see anything that can be hoisted without significant refactoring,
196 >>> actually. Beware that there are two pagename calls in the loop: one for
197 >>> `$f` (which is the page we might want to rebuild), and one for `$file`
198 >>> (which is the changed page that it might depend on). Note that I didn't
199 >>> choose those names!
200 >>>
201 >>> The three loops are over source files, their lists of dependency pagespecs,
202 >>> and files that might have changed. I see the following things we might be
203 >>> doing redundantly:
204 >>>
205 >>> * If `$file` is considered as a potential dependency for more than
206 >>>   one `$f`, we evaluate `pagename($file)` more than once. Potential fix:
207 >>>   cache them (this turns out to save about half a second on the docwiki,
208 >>>   see below).
209 >>> * If several pages depend on the same pagespec, we evaluate whether each
210 >>>   changed page matches that pagespec more than once: however, we do so
211 >>>   with a different location parameter every time, so repeated calls are,
212 >>>   in the general case, the only correct thing to do. Potential fix:
213 >>>   perhaps special-case "page x depends on page y and nothing else"
214 >>>   (i.e. globs that have no wildcards) into a separate hash? I haven't
215 >>>   done anything in this direction.
216 >>> * Any preparatory work done by pagespec_match (converting the pagespec
217 >>>   into Perl, mostly?) is done in the inner loop; switching to
218 >>>   pagespec_match_list (significant refactoring) saves more than half a
219 >>>   second on the docwiki.
220 >>>
221 >>> --[[smcv]]
222
223 Very good catch on img/meta using the wrong dependency; verified in the wild!
224 (I've cherry-picked those bug fixes.)
225
226 ----
227
228 Benchmarking results: I benchmarked by altering docwiki.setup to switch off
229 verbose, running "make clean && ./Makefile.PL && make", and timing one rebuild
230 of the docwiki followed by three refreshes. Before each refresh I used
231 `touch plugins/*.mdwn` to have something significant to refresh.
232
233 I'm assuming that "user" CPU time is the important thing here (system time was
234 relatively small in all cases, up to 0.35 seconds per run).
235
236 master at the time of rebasing: 14.20s to rebuild, 10.04/12.07/14.01s to
237 refresh. I think you can see the bug clearly here - the pagespecs are getting
238 more complicated every time!
239
240 > I can totally see a bug here, and it's one I didn't think existed. Ie,
241 > I thought that after the first refresh, the pagespec should stabalize,
242 > and what it stabalized to was probably unnecessarily long, but not
243 > growing w/o bounds!
244
245 > a) Explains why ikiwiki.info has been so slow lately. Well that and some
246 >    other things that overloaded the system.
247 > b) Suggests to me we will probably want to force a rebuild on upgrade
248 >    when fixing this (via the mechanism in the postinst).
249 >
250 > I've investigated why the pagespecs keep growing: When page A changes,
251 > its old depends are cleared. Then
252 > page B that inlines A gets rebuilt, and its old depends are also cleared.
253 > But page B also inlines page C; which means C gets re-rendered. And this
254 > happens w/o its old depends being cleared, so C's depends are doubled.
255 > --[[Joey]]
256
257 After the initial optimization: 14.27s to rebuild, 8.26/8.33/8.26 to refresh.
258 Success!
259
260 Not pre-joining dependencies actually took about ~0.2s more; I don't know why.
261 I'm worried that duplicates will just build up (again) in less simple cases,
262 though, so 0.2s is probably a small price to pay for that not happening (it
263 might well be experimental error, for that matter).
264
265 > It's weird that the suggested optimisations to
266 > `add_depends` had no effect. So, the commit message to
267 > b6fcb1cb0ef27e5a63184440675d465fad652acf is actually wrong.. ? --[[Joey]] 
268
269 >> I'll try benchmarking again on the non-public wiki where I had the 4%
270 >> speedup. The docwiki is so small that 4% is hard to measure... --[[smcv]]
271
272 Not saving {depends} to the index, using a hash instead of a list to
273 de-duplicate, and allowing add_depends to take an arrayref instead of a single
274 pagespec had no noticable positive or negative effect on this test.
275
276 > I see e4cd168ebedd95585290c97ff42234344bfed46c is still in your branch
277 > though. I don't like using an arrayref, it could just take `($page, @depends)`.
278 > and I don't see the need to keep it if it doesn't currently help.
279
280 >> I'll drop it. --[[smcv]]
281
282 > Is there any reason to keep 7227c2debfeef94b35f7d81f42900aa01820caa3
283 > if it doesn't improve speed?
284 > --[[Joey]] 
285
286 >> I'll try benchmarking on a more complex wiki and see whether it has a
287 >> positive or negative effect. It does avoid being O(n**2) in number of
288 >> dependencies. --[[smcv]]
289
290 Memoizing the results of pagename brought the rebuild time down to 14.06s
291 and the refresh time down to 7.96/7.92/7.92, a significant win.
292
293 > Ok, that seems safe to memoize. (It's a real function and it isn't
294 > called with a great many inputs.) Why did you chose to memoize it
295 > explicitly rather than adding it to the memoize list at the top?
296
297 >> It does depend on global variables, so using Memoize seemed like asking for
298 >> trouble. I suppose what I did is equivalent to Memoize though... --[[smcv]]
299
300 Refactoring to use pagespec_match_list looks more risky from a code churn
301 point of view; rebuild now takes 14.35s, but refresh is only 7.30/7.29/7.28,
302 another significant win.
303
304 --[[smcv]]
305
306 > I had mostly convinced myself that
307 > `pagespec_match_list` would not lead to a speed gain here. My reasoning
308 > was that you want to stop after finding one match, while `pagespec_match_list`
309 > checks all pages for matches. So what we're seeing is that
310 > on a rebuild, `@changed` is all pages, and not short-circuiting leads
311 > to unnecessary work. OTOH, on refresh, `@changed` is small and I suppose
312 > `pagespec_match_list`'s other slight efficiencies win out somehow.
313
314 > Welcome to the "I made ikiwiki twice as fast
315 > and all I got was this lousy git sha1sum" club BTW :-) --[[Joey]] 
316
317 [[!tag wishlist patch patch/core]]