[project @ 2000-06-08 14:36:13 by rrt]
[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;&lowbar;S
379 &lt;string&gt;</Literal>.  The <Literal>&lt;string&gt;</Literal> gives
380 the strictness of the function's arguments.  <Function>L</Function> is
381 lazy (bad), <Function>S</Function> and <Function>E</Function> are
382 strict (good), <Function>P</Function> is &ldquo;primitive&rdquo;
383 (good), <Function>U(...)</Function> is strict and
384 &ldquo;unpackable&rdquo; (very good), and <Function>A</Function> is
385 absent (very good).
386 </Para>
387
388 <Para>
389 For an &ldquo;unpackable&rdquo; <Function>U(...)</Function> argument, the info inside
390 tells the strictness of its components.  So, if the argument is a
391 pair, and it says <Function>U(AU(LSS))</Function>, that means &ldquo;the first component of the
392 pair isn't used; the second component is itself unpackable, with three
393 components (lazy in the first, strict in the second \&#38; third).&rdquo;
394 </Para>
395
396 <Para>
397 If the function isn't exported, just compile with the extra flag <Option>-ddump-simpl</Option>;
398 next to the signature for any binder, it will print the self-same
399 pragmatic information as would be put in an interface file.
400 (Besides, Core syntax is fun to look at!)
401 </Para>
402 </ListItem>
403 </VarListEntry>
404 <VarListEntry>
405 <Term>Force key functions to be <Literal>INLINE</Literal>d (esp. monads):</Term>
406 <ListItem>
407 <Para>
408 Placing <Literal>INLINE</Literal> pragmas on certain functions that are used a lot can
409 have a dramatic effect.  See <XRef LinkEnd="inline-pragma">.
410 </Para>
411 </ListItem>
412 </VarListEntry>
413 <VarListEntry>
414 <Term>Explicit <Literal>export</Literal> list:</Term>
415 <ListItem>
416 <Para>
417 If you do not have an explicit export list in a module, GHC must
418 assume that everything in that module will be exported.  This has
419 various pessimising effects.  For example, if a bit of code is actually
420 <Emphasis>unused</Emphasis> (perhaps because of unfolding effects), GHC will not be
421 able to throw it away, because it is exported and some other module
422 may be relying on its existence.
423 </Para>
424
425 <Para>
426 GHC can be quite a bit more aggressive with pieces of code if it knows
427 they are not exported.
428 </Para>
429 </ListItem>
430 </VarListEntry>
431 <VarListEntry>
432 <Term>Look at the Core syntax!</Term>
433 <ListItem>
434 <Para>
435 (The form in which GHC manipulates your code.)  Just run your
436 compilation with <Option>-ddump-simpl</Option> (don't forget the <Option>-O</Option>).
437 </Para>
438
439 <Para>
440 If profiling has pointed the finger at particular functions, look at
441 their Core code.  <Literal>lets</Literal> are bad, <Literal>cases</Literal> are good, dictionaries
442 (<Literal>d.&lt;Class&gt;.&lt;Unique&gt;</Literal>) &lsqb;or anything overloading-ish&rsqb; are bad,
443 nested lambdas are bad, explicit data constructors are good, primitive
444 operations (e.g., <Literal>eqInt&num;</Literal>) are good,&hellip;
445 </Para>
446 </ListItem>
447 </VarListEntry>
448 <VarListEntry>
449 <Term>Use unboxed types (a GHC extension):</Term>
450 <ListItem>
451 <Para>
452 When you are <Emphasis>really</Emphasis> desperate for speed, and you want to get
453 right down to the &ldquo;raw bits.&rdquo;  Please see <XRef LinkEnd="glasgow-unboxed"> for some information about using unboxed
454 types.
455 </Para>
456 </ListItem>
457 </VarListEntry>
458 <VarListEntry>
459 <Term>Use <Literal>foreign import</Literal> (a GHC extension) to plug into fast libraries:</Term>
460 <ListItem>
461 <Para>
462 This may take real work, but&hellip; There exist piles of
463 massively-tuned library code, and the best thing is not
464 to compete with it, but link with it.
465 </Para>
466
467 <Para>
468 <XRef LinkEnd="sec-ffi"> describes the foreign calling interface.
469 </Para>
470 </ListItem>
471 </VarListEntry>
472 <VarListEntry>
473 <Term>Don't use <Literal>Float</Literal>s:</Term>
474 <ListItem>
475 <Para>
476 We don't provide specialisations of Prelude functions for <Literal>Float</Literal>
477 (but we do for <Literal>Double</Literal>).  If you end up executing overloaded
478 code, you will lose on performance, perhaps badly.
479 </Para>
480
481 <Para>
482 <Literal>Floats</Literal> (probably 32-bits) are almost always a bad idea, anyway,
483 unless you Really Know What You Are Doing.  Use Doubles.  There's
484 rarely a speed disadvantage&mdash;modern machines will use the same
485 floating-point unit for both.  With <Literal>Doubles</Literal>, you are much less
486 likely to hang yourself with numerical errors.
487 </Para>
488
489 <Para>
490 One time when <Literal>Float</Literal> might be a good idea is if you have a
491 <Emphasis>lot</Emphasis> of them, say a giant array of <Literal>Float</Literal>s.  They take up
492 half the space in the heap compared to <Literal>Doubles</Literal>.  However, this isn't
493 true on a 64-bit machine.
494 </Para>
495 </ListItem>
496 </VarListEntry>
497 <VarListEntry>
498 <Term>Use a bigger heap!</Term>
499 <ListItem>
500 <Para>
501 If your program's GC stats (<Option>-S</Option><IndexTerm><Primary>-S RTS option</Primary></IndexTerm> RTS option)
502 indicate that it's doing lots of garbage-collection (say, more than
503 20&percnt; of execution time), more memory might help&mdash;with the
504 <Option>-M&lt;size&gt;</Option><IndexTerm><Primary>-M&lt;size&gt; RTS option</Primary></IndexTerm> or
505 <Option>-A&lt;size&gt;</Option><IndexTerm><Primary>-A&lt;size&gt; RTS option</Primary></IndexTerm> RTS options (see
506 <XRef LinkEnd="rts-options-gc">).
507 </Para>
508 </ListItem>
509 </VarListEntry>
510 </VariableList>
511 </Para>
512
513 </Sect1>
514
515 <Sect1 id="smaller">
516 <Title>Smaller: producing a program that is smaller
517 </Title>
518
519 <Para>
520 <IndexTerm><Primary>smaller programs, how to produce</Primary></IndexTerm>
521 </Para>
522
523 <Para>
524 Decrease the &ldquo;go-for-it&rdquo; threshold for unfolding smallish
525 expressions.  Give a
526 <Option>-funfolding-use-threshold0</Option><IndexTerm><Primary>-funfolding-use-threshold0
527 option</Primary></IndexTerm> option for the extreme case. (&ldquo;Only unfoldings with
528 zero cost should proceed.&rdquo;)  Warning: except in certain specialiised
529 cases (like Happy parsers) this is likely to actually
530 <Emphasis>increase</Emphasis> the size of your program, because unfolding
531 generally enables extra simplifying optimisations to be performed.
532 </Para>
533
534 <Para>
535 Avoid <Function>Read</Function>.
536 </Para>
537
538 <Para>
539 Use <Literal>strip</Literal> on your executables.
540 </Para>
541
542 </Sect1>
543
544 <Sect1 id="stingier">
545 <Title>Stingier: producing a program that gobbles less heap space
546 </Title>
547
548 <Para>
549 <IndexTerm><Primary>memory, using less heap</Primary></IndexTerm>
550 <IndexTerm><Primary>space-leaks, avoiding</Primary></IndexTerm>
551 <IndexTerm><Primary>heap space, using less</Primary></IndexTerm>
552 </Para>
553
554 <Para>
555 &ldquo;I think I have a space leak&hellip;&rdquo; Re-run your program
556 with <Option>+RTS -Sstderr</Option>, and remove all doubt!  (You'll
557 see the heap usage get bigger and bigger&hellip;)
558 &lsqb;Hmmm&hellip;this might be even easier with the
559 <Option>-G1</Option> RTS option; so&hellip; <Command>./a.out +RTS
560 -Sstderr -G1</Command>...]
561 <IndexTerm><Primary>-G RTS option</Primary></IndexTerm>
562 <IndexTerm><Primary>-Sstderr RTS option</Primary></IndexTerm>
563 </Para>
564
565 <Para>
566 Once again, the profiling facilities (<XRef LinkEnd="profiling">) are
567 the basic tool for demystifying the space behaviour of your program.
568 </Para>
569
570 <Para>
571 Strict functions are good for space usage, as they are for time, as
572 discussed in the previous section.  Strict functions get right down to
573 business, rather than filling up the heap with closures (the system's
574 notes to itself about how to evaluate something, should it eventually
575 be required).
576 </Para>
577
578 </Sect1>
579
580 </Chapter>
581
582 <!-- Emacs stuff:
583      ;;; Local Variables: ***
584      ;;; mode: sgml ***
585      ;;; sgml-parent-document: ("users_guide.sgml" "book" "chapter") ***
586      ;;; End: ***
587  -->