474b5a36ee9d08e96e29a85bdce929e0e0f98a00
[ghc-hetmet.git] / ghc / docs / users_guide / sooner.sgml
1 <Chapter id="sooner-faster-quicker">
2 <Title>Advice on: sooner, faster, smaller, thriftier
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 the
37 <Option>-Rghc-timing</Option> option, you'll get a garbage-collector
38 report.  (Again, you can use the cheap-and-nasty <Option>+RTS -Sstderr
39 -RTS</Option> option to 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>-v2</option><indexterm><primary><option>-v</option></primary>
132 </indexterm> option is your 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>Explicit <Literal>import</Literal> declarations:</Term>
145 <ListItem>
146 <Para>
147 Instead of saying <Literal>import Foo</Literal>, say <Literal>import
148 Foo (...stuff I want...)</Literal> You can get GHC to tell you the
149 minimal set of required imports by using the
150 <option>-ddump-minimal-imports</option> option (see <xref
151 linkend="hi-options">).
152 </Para>
153
154 <Para>
155 Truthfully, the reduction on compilation time will be very small.
156 However, judicious use of <Literal>import</Literal> declarations can make a
157 program easier to understand, so it may be a good idea anyway.
158 </Para>
159 </ListItem>
160 </VarListEntry>
161 </VariableList>
162 </Para>
163
164 </Sect1>
165
166 <Sect1 id="faster">
167 <Title>Faster: producing a program that runs quicker
168 </Title>
169
170 <Para>
171 <IndexTerm><Primary>faster programs, how to produce</Primary></IndexTerm>
172 </Para>
173
174 <Para>
175 The key tool to use in making your Haskell program run faster are
176 GHC's profiling facilities, described separately in <XRef LinkEnd="profiling">.  There is <Emphasis>no substitute</Emphasis> for
177 finding where your program's time/space is <Emphasis>really</Emphasis> going, as
178 opposed to where you imagine it is going.
179 </Para>
180
181 <Para>
182 Another point to bear in mind: By far the best way to improve a
183 program's performance <Emphasis>dramatically</Emphasis> is to use better
184 algorithms.  Once profiling has thrown the spotlight on the guilty
185 time-consumer(s), it may be better to re-think your program than to
186 try all the tweaks listed below.
187 </Para>
188
189 <Para>
190 Another extremely efficient way to make your program snappy is to use
191 library code that has been Seriously Tuned By Someone Else.  You
192 <Emphasis>might</Emphasis> be able to write a better quicksort than the one in the
193 HBC library, but it will take you much longer than typing <Literal>import
194 QSort</Literal>.  (Incidentally, it doesn't hurt if the Someone Else is Lennart
195 Augustsson.)
196 </Para>
197
198 <Para>
199 Please report any overly-slow GHC-compiled programs.  The current
200 definition of &ldquo;overly-slow&rdquo; is &ldquo;the HBC-compiled version ran
201 faster&rdquo;&hellip;
202 </Para>
203
204 <Para>
205 <VariableList>
206
207 <VarListEntry>
208 <Term>Optimise, using <Option>-O</Option> or <Option>-O2</Option>:</Term>
209 <ListItem>
210 <Para>
211 This is the most basic way
212 to make your program go faster.  Compilation time will be slower,
213 especially with <Option>-O2</Option>.
214 </Para>
215
216 <Para>
217 At present, <Option>-O2</Option> is nearly indistinguishable from <Option>-O</Option>.
218 </Para>
219 </ListItem>
220 </VarListEntry>
221 <VarListEntry>
222 <Term>Compile via C and crank up GCC:</Term>
223 <ListItem>
224 <Para>
225 The native code-generator is designed to be quick, not mind-bogglingly
226 clever.  Better to let GCC have a go, as it tries much harder on
227 register allocation, etc.</para>
228
229 <para>At the moment, if you turn on <option>-O</option> you get GCC
230 instead.  This may change in the future.</para>
231
232 <Para>
233 So, when we want very fast code, we use: <Option>-O -fvia-C</Option>.
234 </Para>
235 </ListItem>
236 </VarListEntry>
237 <VarListEntry>
238 <Term>Overloaded functions are not your friend:</Term>
239 <ListItem>
240 <Para>
241 Haskell's overloading (using type classes) is elegant, neat, etc.,
242 etc., but it is death to performance if left to linger in an inner
243 loop.  How can you squash it?
244 </Para>
245
246 <Para>
247 <VariableList>
248
249 <VarListEntry>
250 <Term>Give explicit type signatures:</Term>
251 <ListItem>
252 <Para>
253 Signatures are the basic trick; putting them on exported, top-level
254 functions is good software-engineering practice, anyway.  (Tip: using
255 <Option>-fwarn-missing-signatures</Option><IndexTerm><Primary>-fwarn-missing-signatures
256 option</Primary></IndexTerm> can help enforce good signature-practice).
257 </Para>
258
259 <Para>
260 The automatic specialisation of overloaded functions (with <Option>-O</Option>)
261 should take care of overloaded local and/or unexported functions.
262 </Para>
263 </ListItem>
264 </VarListEntry>
265 <VarListEntry>
266 <Term>Use <Literal>SPECIALIZE</Literal> pragmas:</Term>
267 <ListItem>
268 <Para>
269 <IndexTerm><Primary>SPECIALIZE pragma</Primary></IndexTerm>
270 <IndexTerm><Primary>overloading, death to</Primary></IndexTerm>
271 </Para>
272
273 <Para>
274 Specialize the overloading on key functions in your program.  See
275 <XRef LinkEnd="specialize-pragma"> and
276 <XRef LinkEnd="specialize-instance-pragma">.
277 </Para>
278 </ListItem>
279 </VarListEntry>
280 <VarListEntry>
281 <Term>&ldquo;But how do I know where overloading is creeping in?&rdquo;:</Term>
282 <ListItem>
283 <Para>
284 A low-tech way: grep (search) your interface files for overloaded
285 type signatures; e.g.,:
286
287 <ProgramListing>
288 % egrep '^[a-z].*::.*=&#62;' *.hi
289 </ProgramListing>
290
291 </Para>
292 </ListItem>
293 </VarListEntry>
294 </VariableList>
295 </Para>
296 </ListItem>
297 </VarListEntry>
298 <VarListEntry>
299 <Term>Strict functions are your dear friends:</Term>
300 <ListItem>
301 <Para>
302 and, among other things, lazy pattern-matching is your enemy.
303 </Para>
304
305 <Para>
306 (If you don't know what a &ldquo;strict function&rdquo; is, please consult a
307 functional-programming textbook.  A sentence or two of
308 explanation here probably would not do much good.)
309 </Para>
310
311 <Para>
312 Consider these two code fragments:
313
314 <ProgramListing>
315 f (Wibble x y) =  ... # strict
316
317 f arg = let { (Wibble x y) = arg } in ... # lazy
318 </ProgramListing>
319
320 The former will result in far better code.
321 </Para>
322
323 <Para>
324 A less contrived example shows the use of <Literal>cases</Literal> instead
325 of <Literal>lets</Literal> to get stricter code (a good thing):
326
327 <ProgramListing>
328 f (Wibble x y)  # beautiful but slow
329   = let
330         (a1, b1, c1) = unpackFoo x
331         (a2, b2, c2) = unpackFoo y
332     in ...
333
334 f (Wibble x y)  # ugly, and proud of it
335   = case (unpackFoo x) of { (a1, b1, c1) -&#62;
336     case (unpackFoo y) of { (a2, b2, c2) -&#62;
337     ...
338     }}
339 </ProgramListing>
340
341 </Para>
342 </ListItem>
343 </VarListEntry>
344 <VarListEntry>
345 <Term>GHC loves single-constructor data-types:</Term>
346 <ListItem>
347 <Para>
348 It's all the better if a function is strict in a single-constructor
349 type (a type with only one data-constructor; for example, tuples are
350 single-constructor types).
351 </Para>
352 </ListItem>
353 </VarListEntry>
354 <VarListEntry>
355 <Term>Newtypes are better than datatypes:</Term>
356 <ListItem>
357 <Para>
358 If your datatype has a single constructor with a single field, use a
359 <Literal>newtype</Literal> declaration instead of a <Literal>data</Literal> declaration.  The <Literal>newtype</Literal>
360 will be optimised away in most cases.
361 </Para>
362 </ListItem>
363 </VarListEntry>
364 <VarListEntry>
365 <Term>&ldquo;How do I find out a function's strictness?&rdquo;</Term>
366 <ListItem>
367 <Para>
368 Don't guess&mdash;look it up.
369 </Para>
370
371 <Para>
372 Look for your function in the interface file, then for the third field
373 in the pragma; it should say <Literal>&lowbar;&lowbar;S
374 &lt;string&gt;</Literal>.  The <Literal>&lt;string&gt;</Literal> gives
375 the strictness of the function's arguments.  <Function>L</Function> is
376 lazy (bad), <Function>S</Function> and <Function>E</Function> are
377 strict (good), <Function>P</Function> is &ldquo;primitive&rdquo;
378 (good), <Function>U(...)</Function> is strict and
379 &ldquo;unpackable&rdquo; (very good), and <Function>A</Function> is
380 absent (very good).
381 </Para>
382
383 <Para>
384 For an &ldquo;unpackable&rdquo; <Function>U(...)</Function> argument, the info inside
385 tells the strictness of its components.  So, if the argument is a
386 pair, and it says <Function>U(AU(LSS))</Function>, that means &ldquo;the first component of the
387 pair isn't used; the second component is itself unpackable, with three
388 components (lazy in the first, strict in the second \&#38; third).&rdquo;
389 </Para>
390
391 <Para>
392 If the function isn't exported, just compile with the extra flag <Option>-ddump-simpl</Option>;
393 next to the signature for any binder, it will print the self-same
394 pragmatic information as would be put in an interface file.
395 (Besides, Core syntax is fun to look at!)
396 </Para>
397 </ListItem>
398 </VarListEntry>
399 <VarListEntry>
400 <Term>Force key functions to be <Literal>INLINE</Literal>d (esp. monads):</Term>
401 <ListItem>
402 <Para>
403 Placing <Literal>INLINE</Literal> pragmas on certain functions that are used a lot can
404 have a dramatic effect.  See <XRef LinkEnd="inline-pragma">.
405 </Para>
406 </ListItem>
407 </VarListEntry>
408 <VarListEntry>
409 <Term>Explicit <Literal>export</Literal> list:</Term>
410 <ListItem>
411 <Para>
412 If you do not have an explicit export list in a module, GHC must
413 assume that everything in that module will be exported.  This has
414 various pessimising effects.  For example, if a bit of code is actually
415 <Emphasis>unused</Emphasis> (perhaps because of unfolding effects), GHC will not be
416 able to throw it away, because it is exported and some other module
417 may be relying on its existence.
418 </Para>
419
420 <Para>
421 GHC can be quite a bit more aggressive with pieces of code if it knows
422 they are not exported.
423 </Para>
424 </ListItem>
425 </VarListEntry>
426 <VarListEntry>
427 <Term>Look at the Core syntax!</Term>
428 <ListItem>
429 <Para>
430 (The form in which GHC manipulates your code.)  Just run your
431 compilation with <Option>-ddump-simpl</Option> (don't forget the <Option>-O</Option>).
432 </Para>
433
434 <Para>
435 If profiling has pointed the finger at particular functions, look at
436 their Core code.  <Literal>lets</Literal> are bad, <Literal>cases</Literal> are good, dictionaries
437 (<Literal>d.&lt;Class&gt;.&lt;Unique&gt;</Literal>) &lsqb;or anything overloading-ish&rsqb; are bad,
438 nested lambdas are bad, explicit data constructors are good, primitive
439 operations (e.g., <Literal>eqInt&num;</Literal>) are good,&hellip;
440 </Para>
441 </ListItem>
442 </VarListEntry>
443 <VarListEntry>
444 <Term>Use unboxed types (a GHC extension):</Term>
445 <ListItem>
446 <Para>
447 When you are <Emphasis>really</Emphasis> desperate for speed, and you want to get
448 right down to the &ldquo;raw bits.&rdquo;  Please see <XRef LinkEnd="glasgow-unboxed"> for some information about using unboxed
449 types.
450 </Para>
451 </ListItem>
452 </VarListEntry>
453 <VarListEntry>
454 <Term>Use <Literal>foreign import</Literal> (a GHC extension) to plug into fast libraries:</Term>
455 <ListItem>
456 <Para>
457 This may take real work, but&hellip; There exist piles of
458 massively-tuned library code, and the best thing is not
459 to compete with it, but link with it.
460 </Para>
461
462 <Para>
463 <XRef LinkEnd="ffi"> describes the foreign function interface.
464 </Para>
465 </ListItem>
466 </VarListEntry>
467 <VarListEntry>
468 <Term>Don't use <Literal>Float</Literal>s:</Term>
469 <ListItem>
470 <Para>
471 We don't provide specialisations of Prelude functions for <Literal>Float</Literal>
472 (but we do for <Literal>Double</Literal>).  If you end up executing overloaded
473 code, you will lose on performance, perhaps badly.
474 </Para>
475
476 <Para>
477 <Literal>Floats</Literal> (probably 32-bits) are almost always a bad idea, anyway,
478 unless you Really Know What You Are Doing.  Use Doubles.  There's
479 rarely a speed disadvantage&mdash;modern machines will use the same
480 floating-point unit for both.  With <Literal>Doubles</Literal>, you are much less
481 likely to hang yourself with numerical errors.
482 </Para>
483
484 <Para>
485 One time when <Literal>Float</Literal> might be a good idea is if you have a
486 <Emphasis>lot</Emphasis> of them, say a giant array of <Literal>Float</Literal>s.  They take up
487 half the space in the heap compared to <Literal>Doubles</Literal>.  However, this isn't
488 true on a 64-bit machine.
489 </Para>
490 </ListItem>
491 </VarListEntry>
492 <VarListEntry>
493 <Term>Use a bigger heap!</Term>
494 <ListItem>
495 <Para>
496 If your program's GC stats (<Option>-S</Option><IndexTerm><Primary>-S RTS option</Primary></IndexTerm> RTS option)
497 indicate that it's doing lots of garbage-collection (say, more than
498 20&percnt; of execution time), more memory might help&mdash;with the
499 <Option>-M&lt;size&gt;</Option><IndexTerm><Primary>-M&lt;size&gt; RTS option</Primary></IndexTerm> or
500 <Option>-A&lt;size&gt;</Option><IndexTerm><Primary>-A&lt;size&gt; RTS option</Primary></IndexTerm> RTS options (see
501 <XRef LinkEnd="rts-options-gc">).
502 </Para>
503 </ListItem>
504 </VarListEntry>
505 </VariableList>
506 </Para>
507
508 </Sect1>
509
510 <Sect1 id="smaller">
511 <Title>Smaller: producing a program that is smaller
512 </Title>
513
514 <Para>
515 <IndexTerm><Primary>smaller programs, how to produce</Primary></IndexTerm>
516 </Para>
517
518 <Para>
519 Decrease the &ldquo;go-for-it&rdquo; threshold for unfolding smallish
520 expressions.  Give a
521 <Option>-funfolding-use-threshold0</Option><IndexTerm><Primary>-funfolding-use-threshold0
522 option</Primary></IndexTerm> option for the extreme case. (&ldquo;Only unfoldings with
523 zero cost should proceed.&rdquo;)  Warning: except in certain specialised
524 cases (like Happy parsers) this is likely to actually
525 <Emphasis>increase</Emphasis> the size of your program, because unfolding
526 generally enables extra simplifying optimisations to be performed.
527 </Para>
528
529 <Para>
530 Avoid <Function>Read</Function>.
531 </Para>
532
533 <Para>
534 Use <Literal>strip</Literal> on your executables.
535 </Para>
536
537 </Sect1>
538
539 <Sect1 id="thriftier">
540 <Title>Thriftier: producing a program that gobbles less heap space
541 </Title>
542
543 <Para>
544 <IndexTerm><Primary>memory, using less heap</Primary></IndexTerm>
545 <IndexTerm><Primary>space-leaks, avoiding</Primary></IndexTerm>
546 <IndexTerm><Primary>heap space, using less</Primary></IndexTerm>
547 </Para>
548
549 <Para>
550 &ldquo;I think I have a space leak&hellip;&rdquo; Re-run your program
551 with <Option>+RTS -Sstderr</Option>, and remove all doubt!  (You'll
552 see the heap usage get bigger and bigger&hellip;)
553 &lsqb;Hmmm&hellip;this might be even easier with the
554 <Option>-G1</Option> RTS option; so&hellip; <Command>./a.out +RTS
555 -Sstderr -G1</Command>...]
556 <IndexTerm><Primary>-G RTS option</Primary></IndexTerm>
557 <IndexTerm><Primary>-Sstderr RTS option</Primary></IndexTerm>
558 </Para>
559
560 <Para>
561 Once again, the profiling facilities (<XRef LinkEnd="profiling">) are
562 the basic tool for demystifying the space behaviour of your program.
563 </Para>
564
565 <Para>
566 Strict functions are good for space usage, as they are for time, as
567 discussed in the previous section.  Strict functions get right down to
568 business, rather than filling up the heap with closures (the system's
569 notes to itself about how to evaluate something, should it eventually
570 be required).
571 </Para>
572
573 </Sect1>
574
575 </Chapter>
576
577 <!-- Emacs stuff:
578      ;;; Local Variables: ***
579      ;;; mode: sgml ***
580      ;;; sgml-parent-document: ("users_guide.sgml" "book" "chapter") ***
581      ;;; End: ***
582  -->