[project @ 2000-04-03 12:47:08 by simonmar]
[ghc-hetmet.git] / ghc / docs / users_guide / sooner.sgml
1 <Chapter id="sooner-faster-quicker">
2 <Title>Advice on: sooner, faster, smaller, stingier
3 </Title>
4
5 <Para>
6 Please advise us of other &ldquo;helpful hints&rdquo; that should go here!
7 </Para>
8
9 <Sect1 id="sooner">
10 <Title>Sooner: producing a program more quickly
11 </Title>
12
13 <Para>
14 <IndexTerm><Primary>compiling faster</Primary></IndexTerm>
15 <IndexTerm><Primary>faster compiling</Primary></IndexTerm>
16 <VariableList>
17
18 <VarListEntry>
19 <Term>Don't use <Option>-O</Option> or (especially) <Option>-O2</Option>:</Term>
20 <ListItem>
21 <Para>
22 By using them, you are telling GHC that you are willing to suffer
23 longer compilation times for better-quality code.
24 </Para>
25
26 <Para>
27 GHC is surprisingly zippy for normal compilations without <Option>-O</Option>!
28 </Para>
29 </ListItem>
30 </VarListEntry>
31 <VarListEntry>
32 <Term>Use more memory:</Term>
33 <ListItem>
34 <Para>
35 Within reason, more memory for heap space means less garbage
36 collection for GHC, which means less compilation time.  If you use
37 the <Option>-Rgc-stats</Option> option, you'll get a garbage-collector report.
38 (Again, you can use the cheap-and-nasty <Option>-optCrts-Sstderr</Option> option to
39 send the GC stats straight to standard error.)
40 </Para>
41
42 <Para>
43 If it says you're using more than 20&percnt; of total time in garbage
44 collecting, then more memory would help.
45 </Para>
46
47 <Para>
48 If the heap size is approaching the maximum (64M by default), and you
49 have lots of memory, try increasing the maximum with the
50 <Option>-M&lt;size&gt;</Option><IndexTerm><Primary>-M&lt;size&gt; option</Primary></IndexTerm> option, e.g.: <Command>ghc -c -O
51 -M1024m Foo.hs</Command>.
52 </Para>
53
54 <Para>
55 Increasing the default allocation area size used by the compiler's RTS
56 might also help: use the <Option>-A&lt;size&gt;</Option><IndexTerm><Primary>-A&lt;size&gt; option</Primary></IndexTerm>
57 option.
58 </Para>
59
60 <Para>
61 If GHC persists in being a bad memory citizen, please report it as a
62 bug.
63 </Para>
64 </ListItem>
65 </VarListEntry>
66 <VarListEntry>
67 <Term>Don't use too much memory!</Term>
68 <ListItem>
69 <Para>
70 As soon as GHC plus its &ldquo;fellow citizens&rdquo; (other processes on your
71 machine) start using more than the <Emphasis>real memory</Emphasis> on your
72 machine, and the machine starts &ldquo;thrashing,&rdquo; <Emphasis>the party is
73 over</Emphasis>.  Compile times will be worse than terrible!  Use something
74 like the csh-builtin <Command>time</Command> command to get a report on how many page
75 faults you're getting.
76 </Para>
77
78 <Para>
79 If you don't know what virtual memory, thrashing, and page faults are,
80 or you don't know the memory configuration of your machine,
81 <Emphasis>don't</Emphasis> try to be clever about memory use: you'll just make
82 your life a misery (and for other people, too, probably).
83 </Para>
84 </ListItem>
85 </VarListEntry>
86 <VarListEntry>
87 <Term>Try to use local disks when linking:</Term>
88 <ListItem>
89 <Para>
90 Because Haskell objects and libraries tend to be large, it can take
91 many real seconds to slurp the bits to/from a remote filesystem.
92 </Para>
93
94 <Para>
95 It would be quite sensible to <Emphasis>compile</Emphasis> on a fast machine using
96 remotely-mounted disks; then <Emphasis>link</Emphasis> on a slow machine that had
97 your disks directly mounted.
98 </Para>
99 </ListItem>
100 </VarListEntry>
101 <VarListEntry>
102 <Term>Don't derive/use <Function>Read</Function> unnecessarily:</Term>
103 <ListItem>
104 <Para>
105 It's ugly and slow.
106 </Para>
107 </ListItem>
108 </VarListEntry>
109 <VarListEntry>
110 <Term>GHC compiles some program constructs slowly:</Term>
111 <ListItem>
112 <Para>
113 Deeply-nested list comprehensions seem to be one such; in the past,
114 very large constant tables were bad, too.
115 </Para>
116
117 <Para>
118 We'd rather you reported such behaviour as a bug, so that we can try
119 to correct it.
120 </Para>
121
122 <Para>
123 The part of the compiler that is occasionally prone to wandering off
124 for a long time is the strictness analyser.  You can turn this off
125 individually with <Option>-fno-strictness</Option>.
126 <IndexTerm><Primary>-fno-strictness anti-option</Primary></IndexTerm>
127 </Para>
128
129 <Para>
130 To figure out which part of the compiler is badly behaved, the
131 <Option>-dshow-passes</Option><IndexTerm><Primary>-dshow-passes option</Primary></IndexTerm> option is your
132 friend.
133 </Para>
134
135 <Para>
136 If your module has big wads of constant data, GHC may produce a huge
137 basic block that will cause the native-code generator's register
138 allocator to founder.  Bring on <Option>-fvia-C</Option><IndexTerm><Primary>-fvia-C option</Primary></IndexTerm>
139 (not that GCC will be that quick about it, either).
140 </Para>
141 </ListItem>
142 </VarListEntry>
143 <VarListEntry>
144 <Term>Avoid the consistency-check on linking:</Term>
145 <ListItem>
146 <Para>
147 Use <Option>-no-link-chk</Option><IndexTerm><Primary>-no-link-chk</Primary></IndexTerm>; saves effort.  This is
148 probably safe in a I-only-compile-things-one-way setup.
149 </Para>
150 </ListItem>
151 </VarListEntry>
152 <VarListEntry>
153 <Term>Explicit <Literal>import</Literal> declarations:</Term>
154 <ListItem>
155 <Para>
156 Instead of saying <Literal>import Foo</Literal>, say <Literal>import Foo (...stuff I want...)</Literal>.
157 </Para>
158
159 <Para>
160 Truthfully, the reduction on compilation time will be very small.
161 However, judicious use of <Literal>import</Literal> declarations can make a
162 program easier to understand, so it may be a good idea anyway.
163 </Para>
164 </ListItem>
165 </VarListEntry>
166 </VariableList>
167 </Para>
168
169 </Sect1>
170
171 <Sect1 id="faster">
172 <Title>Faster: producing a program that runs quicker
173 </Title>
174
175 <Para>
176 <IndexTerm><Primary>faster programs, how to produce</Primary></IndexTerm>
177 </Para>
178
179 <Para>
180 The key tool to use in making your Haskell program run faster are
181 GHC's profiling facilities, described separately in <XRef LinkEnd="profiling">.  There is <Emphasis>no substitute</Emphasis> for
182 finding where your program's time/space is <Emphasis>really</Emphasis> going, as
183 opposed to where you imagine it is going.
184 </Para>
185
186 <Para>
187 Another point to bear in mind: By far the best way to improve a
188 program's performance <Emphasis>dramatically</Emphasis> is to use better
189 algorithms.  Once profiling has thrown the spotlight on the guilty
190 time-consumer(s), it may be better to re-think your program than to
191 try all the tweaks listed below.
192 </Para>
193
194 <Para>
195 Another extremely efficient way to make your program snappy is to use
196 library code that has been Seriously Tuned By Someone Else.  You
197 <Emphasis>might</Emphasis> be able to write a better quicksort than the one in the
198 HBC library, but it will take you much longer than typing <Literal>import
199 QSort</Literal>.  (Incidentally, it doesn't hurt if the Someone Else is Lennart
200 Augustsson.)
201 </Para>
202
203 <Para>
204 Please report any overly-slow GHC-compiled programs.  The current
205 definition of &ldquo;overly-slow&rdquo; is &ldquo;the HBC-compiled version ran
206 faster&rdquo;&hellip;
207 </Para>
208
209 <Para>
210 <VariableList>
211
212 <VarListEntry>
213 <Term>Optimise, using <Option>-O</Option> or <Option>-O2</Option>:</Term>
214 <ListItem>
215 <Para>
216 This is the most basic way
217 to make your program go faster.  Compilation time will be slower,
218 especially with <Option>-O2</Option>.
219 </Para>
220
221 <Para>
222 At present, <Option>-O2</Option> is nearly indistinguishable from <Option>-O</Option>.
223 </Para>
224 </ListItem>
225 </VarListEntry>
226 <VarListEntry>
227 <Term>Compile via C and crank up GCC:</Term>
228 <ListItem>
229 <Para>
230 Even with <Option>-O</Option>, GHC tries to
231 use a native-code generator, if available.  But the native
232 code-generator is designed to be quick, not mind-bogglingly clever.
233 Better to let GCC have a go, as it tries much harder on register
234 allocation, etc.
235 </Para>
236
237 <Para>
238 So, when we want very fast code, we use: <Option>-O -fvia-C -O2-for-C</Option>.
239 </Para>
240 </ListItem>
241 </VarListEntry>
242 <VarListEntry>
243 <Term>Overloaded functions are not your friend:</Term>
244 <ListItem>
245 <Para>
246 Haskell's overloading (using type classes) is elegant, neat, etc.,
247 etc., but it is death to performance if left to linger in an inner
248 loop.  How can you squash it?
249 </Para>
250
251 <Para>
252 <VariableList>
253
254 <VarListEntry>
255 <Term>Give explicit type signatures:</Term>
256 <ListItem>
257 <Para>
258 Signatures are the basic trick; putting them on exported, top-level
259 functions is good software-engineering practice, anyway.  (Tip: using
260 <Option>-fwarn-missing-signatures</Option><IndexTerm><Primary>-fwarn-missing-signatures
261 option</Primary></IndexTerm> can help enforce good signature-practice).
262 </Para>
263
264 <Para>
265 The automatic specialisation of overloaded functions (with <Option>-O</Option>)
266 should take care of overloaded local and/or unexported functions.
267 </Para>
268 </ListItem>
269 </VarListEntry>
270 <VarListEntry>
271 <Term>Use <Literal>SPECIALIZE</Literal> pragmas:</Term>
272 <ListItem>
273 <Para>
274 <IndexTerm><Primary>SPECIALIZE pragma</Primary></IndexTerm>
275 <IndexTerm><Primary>overloading, death to</Primary></IndexTerm>
276 </Para>
277
278 <Para>
279 Specialize the overloading on key functions in your program.  See
280 <XRef LinkEnd="specialize-pragma"> and
281 <XRef LinkEnd="specialize-instance-pragma">.
282 </Para>
283 </ListItem>
284 </VarListEntry>
285 <VarListEntry>
286 <Term>&ldquo;But how do I know where overloading is creeping in?&rdquo;:</Term>
287 <ListItem>
288 <Para>
289 A low-tech way: grep (search) your interface files for overloaded
290 type signatures; e.g.,:
291
292 <ProgramListing>
293 % egrep '^[a-z].*::.*=&#62;' *.hi
294 </ProgramListing>
295
296 </Para>
297 </ListItem>
298 </VarListEntry>
299 </VariableList>
300 </Para>
301 </ListItem>
302 </VarListEntry>
303 <VarListEntry>
304 <Term>Strict functions are your dear friends:</Term>
305 <ListItem>
306 <Para>
307 and, among other things, lazy pattern-matching is your enemy.
308 </Para>
309
310 <Para>
311 (If you don't know what a &ldquo;strict function&rdquo; is, please consult a
312 functional-programming textbook.  A sentence or two of
313 explanation here probably would not do much good.)
314 </Para>
315
316 <Para>
317 Consider these two code fragments:
318
319 <ProgramListing>
320 f (Wibble x y) =  ... # strict
321
322 f arg = let { (Wibble x y) = arg } in ... # lazy
323 </ProgramListing>
324
325 The former will result in far better code.
326 </Para>
327
328 <Para>
329 A less contrived example shows the use of <Literal>cases</Literal> instead
330 of <Literal>lets</Literal> to get stricter code (a good thing):
331
332 <ProgramListing>
333 f (Wibble x y)  # beautiful but slow
334   = let
335         (a1, b1, c1) = unpackFoo x
336         (a2, b2, c2) = unpackFoo y
337     in ...
338
339 f (Wibble x y)  # ugly, and proud of it
340   = case (unpackFoo x) of { (a1, b1, c1) -&#62;
341     case (unpackFoo y) of { (a2, b2, c2) -&#62;
342     ...
343     }}
344 </ProgramListing>
345
346 </Para>
347 </ListItem>
348 </VarListEntry>
349 <VarListEntry>
350 <Term>GHC loves single-constructor data-types:</Term>
351 <ListItem>
352 <Para>
353 It's all the better if a function is strict in a single-constructor
354 type (a type with only one data-constructor; for example, tuples are
355 single-constructor types).
356 </Para>
357 </ListItem>
358 </VarListEntry>
359 <VarListEntry>
360 <Term>Newtypes are better than datatypes:</Term>
361 <ListItem>
362 <Para>
363 If your datatype has a single constructor with a single field, use a
364 <Literal>newtype</Literal> declaration instead of a <Literal>data</Literal> declaration.  The <Literal>newtype</Literal>
365 will be optimised away in most cases.
366 </Para>
367 </ListItem>
368 </VarListEntry>
369 <VarListEntry>
370 <Term>&ldquo;How do I find out a function's strictness?&rdquo;</Term>
371 <ListItem>
372 <Para>
373 Don't guess&mdash;look it up.
374 </Para>
375
376 <Para>
377 Look for your function in the interface file, then for the third field
378 in the pragma; it should say <Literal>&lowbar;S&lowbar; &lt;string&gt;</Literal>.  The <Literal>&lt;string&gt;</Literal>
379 gives the strictness of the function's arguments.  <Function>L</Function> is lazy
380 (bad), <Function>S</Function> and <Function>E</Function> are strict (good), <Function>P</Function> is &ldquo;primitive&rdquo; (good),
381 <Function>U(...)</Function> is strict and
382 &ldquo;unpackable&rdquo; (very good), and <Function>A</Function> is absent (very good).
383 </Para>
384
385 <Para>
386 For an &ldquo;unpackable&rdquo; <Function>U(...)</Function> argument, the info inside
387 tells the strictness of its components.  So, if the argument is a
388 pair, and it says <Function>U(AU(LSS))</Function>, that means &ldquo;the first component of the
389 pair isn't used; the second component is itself unpackable, with three
390 components (lazy in the first, strict in the second \&#38; third).&rdquo;
391 </Para>
392
393 <Para>
394 If the function isn't exported, just compile with the extra flag <Option>-ddump-simpl</Option>;
395 next to the signature for any binder, it will print the self-same
396 pragmatic information as would be put in an interface file.
397 (Besides, Core syntax is fun to look at!)
398 </Para>
399 </ListItem>
400 </VarListEntry>
401 <VarListEntry>
402 <Term>Force key functions to be <Literal>INLINE</Literal>d (esp. monads):</Term>
403 <ListItem>
404 <Para>
405 Placing <Literal>INLINE</Literal> pragmas on certain functions that are used a lot can
406 have a dramatic effect.  See <XRef LinkEnd="inline-pragma">.
407 </Para>
408 </ListItem>
409 </VarListEntry>
410 <VarListEntry>
411 <Term>Explicit <Literal>export</Literal> list:</Term>
412 <ListItem>
413 <Para>
414 If you do not have an explicit export list in a module, GHC must
415 assume that everything in that module will be exported.  This has
416 various pessimising effects.  For example, if a bit of code is actually
417 <Emphasis>unused</Emphasis> (perhaps because of unfolding effects), GHC will not be
418 able to throw it away, because it is exported and some other module
419 may be relying on its existence.
420 </Para>
421
422 <Para>
423 GHC can be quite a bit more aggressive with pieces of code if it knows
424 they are not exported.
425 </Para>
426 </ListItem>
427 </VarListEntry>
428 <VarListEntry>
429 <Term>Look at the Core syntax!</Term>
430 <ListItem>
431 <Para>
432 (The form in which GHC manipulates your code.)  Just run your
433 compilation with <Option>-ddump-simpl</Option> (don't forget the <Option>-O</Option>).
434 </Para>
435
436 <Para>
437 If profiling has pointed the finger at particular functions, look at
438 their Core code.  <Literal>lets</Literal> are bad, <Literal>cases</Literal> are good, dictionaries
439 (<Literal>d.&lt;Class&gt;.&lt;Unique&gt;</Literal>) &lsqb;or anything overloading-ish&rsqb; are bad,
440 nested lambdas are bad, explicit data constructors are good, primitive
441 operations (e.g., <Literal>eqInt&num;</Literal>) are good,&hellip;
442 </Para>
443 </ListItem>
444 </VarListEntry>
445 <VarListEntry>
446 <Term>Use unboxed types (a GHC extension):</Term>
447 <ListItem>
448 <Para>
449 When you are <Emphasis>really</Emphasis> desperate for speed, and you want to get
450 right down to the &ldquo;raw bits.&rdquo;  Please see <XRef LinkEnd="glasgow-unboxed"> for some information about using unboxed
451 types.
452 </Para>
453 </ListItem>
454 </VarListEntry>
455 <VarListEntry>
456 <Term>Use <Function>&lowbar;ccall&lowbar;s</Function> (a GHC extension) to plug into fast libraries:</Term>
457 <ListItem>
458 <Para>
459 This may take real work, but&hellip; There exist piles of
460 massively-tuned library code, and the best thing is not
461 to compete with it, but link with it.
462 </Para>
463
464 <Para>
465 <XRef LinkEnd="glasgow-ccalls"> says a little about how to use C calls.
466 </Para>
467 </ListItem>
468 </VarListEntry>
469 <VarListEntry>
470 <Term>Don't use <Literal>Float</Literal>s:</Term>
471 <ListItem>
472 <Para>
473 We don't provide specialisations of Prelude functions for <Literal>Float</Literal>
474 (but we do for <Literal>Double</Literal>).  If you end up executing overloaded
475 code, you will lose on performance, perhaps badly.
476 </Para>
477
478 <Para>
479 <Literal>Floats</Literal> (probably 32-bits) are almost always a bad idea, anyway,
480 unless you Really Know What You Are Doing.  Use Doubles.  There's
481 rarely a speed disadvantage&mdash;modern machines will use the same
482 floating-point unit for both.  With <Literal>Doubles</Literal>, you are much less
483 likely to hang yourself with numerical errors.
484 </Para>
485
486 <Para>
487 One time when <Literal>Float</Literal> might be a good idea is if you have a
488 <Emphasis>lot</Emphasis> of them, say a giant array of <Literal>Float</Literal>s.  They take up
489 half the space in the heap compared to <Literal>Doubles</Literal>.  However, this isn't
490 true on a 64-bit machine.
491 </Para>
492 </ListItem>
493 </VarListEntry>
494 <VarListEntry>
495 <Term>Use a bigger heap!</Term>
496 <ListItem>
497 <Para>
498 If your program's GC stats (<Option>-S</Option><IndexTerm><Primary>-S RTS option</Primary></IndexTerm> RTS option)
499 indicate that it's doing lots of garbage-collection (say, more than
500 20&percnt; of execution time), more memory might help&mdash;with the
501 <Option>-M&lt;size&gt;</Option><IndexTerm><Primary>-M&lt;size&gt; RTS option</Primary></IndexTerm> or
502 <Option>-A&lt;size&gt;</Option><IndexTerm><Primary>-A&lt;size&gt; RTS option</Primary></IndexTerm> RTS options (see
503 <XRef LinkEnd="rts-options-gc">).
504 </Para>
505 </ListItem>
506 </VarListEntry>
507 </VariableList>
508 </Para>
509
510 </Sect1>
511
512 <Sect1 id="smaller">
513 <Title>Smaller: producing a program that is smaller
514 </Title>
515
516 <Para>
517 <IndexTerm><Primary>smaller programs, how to produce</Primary></IndexTerm>
518 </Para>
519
520 <Para>
521 Decrease the &ldquo;go-for-it&rdquo; threshold for unfolding smallish
522 expressions.  Give a
523 <Option>-funfolding-use-threshold0</Option><IndexTerm><Primary>-funfolding-use-threshold0
524 option</Primary></IndexTerm> option for the extreme case. (&ldquo;Only unfoldings with
525 zero cost should proceed.&rdquo;)  Warning: except in certain specialiised
526 cases (like Happy parsers) this is likely to actually
527 <Emphasis>increase</Emphasis> the size of your program, because unfolding
528 generally enables extra simplifying optimisations to be performed.
529 </Para>
530
531 <Para>
532 Avoid <Function>Read</Function>.
533 </Para>
534
535 <Para>
536 Use <Literal>strip</Literal> on your executables.
537 </Para>
538
539 </Sect1>
540
541 <Sect1 id="stingier">
542 <Title>Stingier: producing a program that gobbles less heap space
543 </Title>
544
545 <Para>
546 <IndexTerm><Primary>memory, using less heap</Primary></IndexTerm>
547 <IndexTerm><Primary>space-leaks, avoiding</Primary></IndexTerm>
548 <IndexTerm><Primary>heap space, using less</Primary></IndexTerm>
549 </Para>
550
551 <Para>
552 &ldquo;I think I have a space leak&hellip;&rdquo; Re-run your program
553 with <Option>+RTS -Sstderr</Option>, and remove all doubt!  (You'll
554 see the heap usage get bigger and bigger&hellip;)
555 &lsqb;Hmmm&hellip;this might be even easier with the
556 <Option>-G1</Option> RTS option; so&hellip; <Command>./a.out +RTS
557 -Sstderr -G1</Command>...]
558 <IndexTerm><Primary>-G RTS option</Primary></IndexTerm>
559 <IndexTerm><Primary>-Sstderr RTS option</Primary></IndexTerm>
560 </Para>
561
562 <Para>
563 Once again, the profiling facilities (<XRef LinkEnd="profiling">) are
564 the basic tool for demystifying the space behaviour of your program.
565 </Para>
566
567 <Para>
568 Strict functions are good for space usage, as they are for time, as
569 discussed in the previous section.  Strict functions get right down to
570 business, rather than filling up the heap with closures (the system's
571 notes to itself about how to evaluate something, should it eventually
572 be required).
573 </Para>
574
575 </Sect1>
576
577 </Chapter>