1 %************************************************************************
3 <sect>Advice on: sooner, faster, smaller, stingier
4 <label id="sooner-faster-quicker">
7 %************************************************************************
9 Please advise us of other ``helpful hints'' that should go here!
11 %************************************************************************
13 <sect1>Sooner: producing a program more quickly
16 <nidx>compiling faster</nidx>
17 <nidx>faster compiling</nidx>
19 %************************************************************************
22 %----------------------------------------------------------------
23 <tag>Don't use @-O@ or (especially) @-O2@:</tag>
24 By using them, you are telling GHC that you are willing to suffer
25 longer compilation times for better-quality code.
27 GHC is surprisingly zippy for normal compilations without @-O@!
29 %----------------------------------------------------------------
30 <tag>Use more memory:</tag>
31 Within reason, more memory for heap space means less garbage
32 collection for GHC, which means less compilation time. If you use
33 the @-Rgc-stats@ option, you'll get a garbage-collector report.
34 (Again, you can use the cheap-and-nasty @-optCrts-Sstderr@ option to
35 send the GC stats straight to standard error.)
37 If it says you're using more than 20\% of total time in garbage
38 collecting, then more memory would help.
40 If the heap size is approaching the maximum (64M by default), and you
41 have lots of memory, try increasing the maximum with the
42 @-M<size>@<nidx>-M<size> option</nidx> option, e.g.: @ghc -c -O
45 Increasing the default allocation area size used by the compiler's RTS
46 might also help: use the @-A<size>@<nidx>-A<size> option</nidx>
49 If GHC persists in being a bad memory citizen, please report it as a
52 %----------------------------------------------------------------
53 <tag>Don't use too much memory!</tag>
54 As soon as GHC plus its ``fellow citizens'' (other processes on your
55 machine) start using more than the <em>real memory</em> on your
56 machine, and the machine starts ``thrashing,'' <em>the party is
57 over</em>. Compile times will be worse than terrible! Use something
58 like the csh-builtin @time@ command to get a report on how many page
59 faults you're getting.
61 If you don't know what virtual memory, thrashing, and page faults are,
62 or you don't know the memory configuration of your machine,
63 <em>don't</em> try to be clever about memory use: you'll just make
64 your life a misery (and for other people, too, probably).
66 %----------------------------------------------------------------
67 <tag>Try to use local disks when linking:</tag>
68 Because Haskell objects and libraries tend to be large, it can take
69 many real seconds to slurp the bits to/from a remote filesystem.
71 It would be quite sensible to <em>compile</em> on a fast machine using
72 remotely-mounted disks; then <em>link</em> on a slow machine that had
73 your disks directly mounted.
75 %----------------------------------------------------------------
76 <tag>Don't derive/use @Read@ unnecessarily:</tag>
79 %----------------------------------------------------------------
80 <tag>GHC compiles some program constructs slowly:</tag>
81 Deeply-nested list comprehensions seem to be one such; in the past,
82 very large constant tables were bad, too.
84 We'd rather you reported such behaviour as a bug, so that we can try
87 The parts of the compiler that seem most prone to wandering off for a
88 long time are the abstract interpreters (strictness and update
89 analysers). You can turn these off individually with
90 @-fno-strictness@<nidx>-fno-strictness anti-option</nidx> and
91 @-fno-update-analysis@.<nidx>-fno-update-analysis anti-option</nidx>
93 To figure out which part of the compiler is badly behaved, the
94 @-dshow-passes@<nidx>-dshow-passes option</nidx> option is your
97 If your module has big wads of constant data, GHC may produce a huge
98 basic block that will cause the native-code generator's register
99 allocator to founder. Bring on @-fvia-C@<nidx>-fvia-C option</nidx>
100 (not that GCC will be that quick about it, either).
102 %----------------------------------------------------------------
103 <tag>Avoid the consistency-check on linking:</tag>
105 Use @-no-link-chk@<nidx>-no-link-chk</nidx>; saves effort. This is
106 probably safe in a I-only-compile-things-one-way setup.
108 %----------------------------------------------------------------
109 <tag>Explicit @import@ declarations:</tag>
111 Instead of saying @import Foo@, say @import Foo (...stuff I want...)@.
113 Truthfully, the reduction on compilation time will be very small.
114 However, judicious use of @import@ declarations can make a
115 program easier to understand, so it may be a good idea anyway.
118 %************************************************************************
120 <sect1>Faster: producing a program that runs quicker
123 <nidx>faster programs, how to produce</nidx>
125 %************************************************************************
127 The key tool to use in making your Haskell program run faster are
128 GHC's profiling facilities, described separately in Section <ref
129 name="Profiling" id="profiling">. There is <em>no substitute</em> for
130 finding where your program's time/space is <em>really</em> going, as
131 opposed to where you imagine it is going.
133 Another point to bear in mind: By far the best way to improve a
134 program's performance <em>dramatically</em> is to use better
135 algorithms. Once profiling has thrown the spotlight on the guilty
136 time-consumer(s), it may be better to re-think your program than to
137 try all the tweaks listed below.
139 Another extremely efficient way to make your program snappy is to use
140 library code that has been Seriously Tuned By Someone Else. You
141 <em>might</em> be able to write a better quicksort than the one in the
142 HBC library, but it will take you much longer than typing @import
143 QSort@. (Incidentally, it doesn't hurt if the Someone Else is Lennart
146 Please report any overly-slow GHC-compiled programs. The current
147 definition of ``overly-slow'' is ``the HBC-compiled version ran
151 %----------------------------------------------------------------
152 <tag>Optimise, using @-O@ or @-O2@:</tag> This is the most basic way
153 to make your program go faster. Compilation time will be slower,
154 especially with @-O2@.
156 At present, @-O2@ is nearly indistinguishable from @-O@.
158 %----------------------------------------------------------------
159 <tag>Compile via C and crank up GCC:</tag> Even with @-O@, GHC tries to
160 use a native-code generator, if available. But the native
161 code-generator is designed to be quick, not mind-bogglingly clever.
162 Better to let GCC have a go, as it tries much harder on register
165 So, when we want very fast code, we use: @-O -fvia-C -O2-for-C@.
167 %----------------------------------------------------------------
168 <tag>Overloaded functions are not your friend:</tag>
169 Haskell's overloading (using type classes) is elegant, neat, etc.,
170 etc., but it is death to performance if left to linger in an inner
171 loop. How can you squash it?
174 <tag>Give explicit type signatures:</tag>
175 Signatures are the basic trick; putting them on exported, top-level
176 functions is good software-engineering practice, anyway. (Tip: using
177 @-fwarn-missing-signatures@<nidx>-fwarn-missing-signatures
178 option</nidx> can help enforce good signature-practice).
180 The automatic specialisation of overloaded functions (with @-O@)
181 should take care of overloaded local and/or unexported functions.
183 <tag>Use @SPECIALIZE@ pragmas:</tag>
184 <nidx>SPECIALIZE pragma</nidx>
185 <nidx>overloading, death to</nidx>
187 Specialize the overloading on key functions in your program. See
188 Sections <ref name="SPECIALIZE pragmas" id="specialize-pragma"> and
189 <ref name="SPECIALIZE instance pragmas" id="specialize-instance-pragma">
191 % See also: overlapping instances, in Section <ref name="``HBC-ish''
192 % extensions implemented by GHC" id="glasgow-hbc-exts">. They are to
193 % @SPECIALIZE instance@ pragmas what @= blah@ hacks are to @SPECIALIZE@
196 %ToDo: there's nothing like this at the moment: --SDM
198 % <tag>``How do I know what's happening with specialisations?'':</tag>
200 % The @-fshow-specialisations@<nidx>-fshow-specialisations option</nidx>
201 % will show the specialisations that actually take place.
203 % The @-fshow-import-specs@<nidx>-fshow-import-specs option</nidx> will
204 % show the specialisations that GHC <em>wished</em> were available, but
205 % were not. You can add the relevant pragmas to your code if you wish.
207 % You're a bit stuck if the desired specialisation is of a Prelude
208 % function. If it's Really Important, you can just snap a copy of the
209 % Prelude code, rename it, and then SPECIALIZE that to your heart's
212 <tag>``But how do I know where overloading is creeping in?'':</tag>
214 A low-tech way: grep (search) your interface files for overloaded
215 type signatures; e.g.,:
217 % egrep '^[a-z].*::.*=>' *.hi
221 %----------------------------------------------------------------
222 <tag>Strict functions are your dear friends:</tag>
223 and, among other things, lazy pattern-matching is your enemy.
225 (If you don't know what a ``strict function'' is, please consult a
226 functional-programming textbook. A sentence or two of
227 explanation here probably would not do much good.)
229 Consider these two code fragments:
231 f (Wibble x y) = ... # strict
233 f arg = let { (Wibble x y) = arg } in ... # lazy
235 The former will result in far better code.
237 A less contrived example shows the use of @cases@ instead
238 of @lets@ to get stricter code (a good thing):
240 f (Wibble x y) # beautiful but slow
242 (a1, b1, c1) = unpackFoo x
243 (a2, b2, c2) = unpackFoo y
246 f (Wibble x y) # ugly, and proud of it
247 = case (unpackFoo x) of { (a1, b1, c1) ->
248 case (unpackFoo y) of { (a2, b2, c2) ->
253 %----------------------------------------------------------------
254 <tag>GHC loves single-constructor data-types:</tag>
256 It's all the better if a function is strict in a single-constructor
257 type (a type with only one data-constructor; for example, tuples are
258 single-constructor types).
260 %----------------------------------------------------------------
261 <tag>Newtypes are better than datatypes:</tag>
263 If your datatype has a single constructor with a single field, use a
264 @newtype@ declaration instead of a @data@ declaration. The @newtype@
265 will be optimised away in most cases.
267 %----------------------------------------------------------------
268 <tag>``How do I find out a function's strictness?''</tag>
270 Don't guess---look it up.
272 Look for your function in the interface file, then for the third field
273 in the pragma; it should say @_S_ <string>@. The @<string>@
274 gives the strictness of the function's arguments. @L@ is lazy
275 (bad), @S@ and @E@ are strict (good), @P@ is ``primitive'' (good),
276 @U(...)@ is strict and
277 ``unpackable'' (very good), and @A@ is absent (very good).
279 For an ``unpackable'' @U(...)@ argument, the info inside
280 tells the strictness of its components. So, if the argument is a
281 pair, and it says @U(AU(LSS))@, that means ``the first component of the
282 pair isn't used; the second component is itself unpackable, with three
283 components (lazy in the first, strict in the second \& third).''
285 If the function isn't exported, just compile with the extra flag @-ddump-simpl@;
286 next to the signature for any binder, it will print the self-same
287 pragmatic information as would be put in an interface file.
288 (Besides, Core syntax is fun to look at!)
290 %----------------------------------------------------------------
291 <tag>Force key functions to be @INLINE@d (esp. monads):</tag>
293 Placing @INLINE@ pragmas on certain functions that are used a lot can
294 have a dramatic effect. See Section <ref name="INLINE pragmas"
297 %----------------------------------------------------------------
298 <tag>Explicit @export@ list:</tag>
299 If you do not have an explicit export list in a module, GHC must
300 assume that everything in that module will be exported. This has
301 various pessimising effects. For example, if a bit of code is actually
302 <em>unused</em> (perhaps because of unfolding effects), GHC will not be
303 able to throw it away, because it is exported and some other module
304 may be relying on its existence.
306 GHC can be quite a bit more aggressive with pieces of code if it knows
307 they are not exported.
309 %----------------------------------------------------------------
310 <tag>Look at the Core syntax!</tag>
311 (The form in which GHC manipulates your code.) Just run your
312 compilation with @-ddump-simpl@ (don't forget the @-O@).
314 If profiling has pointed the finger at particular functions, look at
315 their Core code. @lets@ are bad, @cases@ are good, dictionaries
316 (@d.<Class>.<Unique>@) [or anything overloading-ish] are bad,
317 nested lambdas are bad, explicit data constructors are good, primitive
318 operations (e.g., @eqInt#@) are good, ...
320 %----------------------------------------------------------------
321 <tag>Use unboxed types (a GHC extension):</tag>
322 When you are <em>really</em> desperate for speed, and you want to get
323 right down to the ``raw bits.'' Please see Section <ref name="Unboxed
324 types" id="glasgow-unboxed"> for some information about using unboxed
327 %----------------------------------------------------------------
328 <tag>Use @_ccall_s@ (a GHC extension) to plug into fast libraries:</tag>
329 This may take real work, but... There exist piles of
330 massively-tuned library code, and the best thing is not
331 to compete with it, but link with it.
333 Section <ref name="Calling~C directly from Haskell" id="glasgow-ccalls"> says a little about how to use C calls.
335 %----------------------------------------------------------------
336 <tag>Don't use @Float@s:</tag>
337 We don't provide specialisations of Prelude functions for @Float@
338 (but we do for @Double@). If you end up executing overloaded
339 code, you will lose on performance, perhaps badly.
341 @Floats@ (probably 32-bits) are almost always a bad idea, anyway,
342 unless you Really Know What You Are Doing. Use Doubles. There's
343 rarely a speed disadvantage---modern machines will use the same
344 floating-point unit for both. With @Doubles@, you are much less
345 likely to hang yourself with numerical errors.
347 One time when @Float@ might be a good idea is if you have a
348 <em>lot</em> of them, say a giant array of @Float@s. They take up
349 half the space in the heap compared to @Doubles@. However, this isn't
350 true on a 64-bit machine.
352 %----------------------------------------------------------------
353 <tag>Use a bigger heap!</tag>
355 If your program's GC stats (@-S@<nidx>-S RTS option</nidx> RTS option)
356 indicate that it's doing lots of garbage-collection (say, more than
357 20\% of execution time), more memory might help---with the
358 @-M<size>@<nidx>-M<size> RTS option</nidx> or
359 @-A<size>@<nidx>-A<size> RTS option</nidx> RTS options (see
360 Section <ref name="RTS options to control the garbage-collector"
361 id="rts-options-gc">).
364 %************************************************************************
366 <sect1>Smaller: producing a program that is smaller
369 <nidx>smaller programs, how to produce</nidx>
371 %************************************************************************
373 Decrease the ``go-for-it'' threshold for unfolding smallish
375 @-funfolding-use-threshold0@<nidx>-funfolding-use-threshold0
376 option</nidx> option for the extreme case. (``Only unfoldings with
377 zero cost should proceed.'') Warning: except in certain specialiised
378 cases (like Happy parsers) this is likely to actually
379 <em>increase</em> the size of your program, because unfolding
380 generally enables extra simplifying optimisations to be performed.
384 Use @strip@ on your executables.
386 %************************************************************************
388 <sect1>Stingier: producing a program that gobbles less heap space
389 <label id="stingier">
391 <nidx>memory, using less heap</nidx>
392 <nidx>space-leaks, avoiding</nidx>
393 <nidx>heap space, using less</nidx>
395 %************************************************************************
397 ``I think I have a space leak...'' Re-run your program with
398 @+RTS -Sstderr@,<nidx>-Sstderr RTS option</nidx> and remove all doubt!
399 (You'll see the heap usage get bigger and bigger...) [Hmmm... this
400 might be even easier with the @-F2s@<nidx>-F2s RTS option</nidx> RTS
401 option; so... @./a.out +RTS -Sstderr -F2s@...]
403 Once again, the profiling facilities (Section <ref name="Profiling"
404 id="profiling">) are the basic tool for demystifying the space
405 behaviour of your program.
407 Strict functions are good for space usage, as they are for time, as
408 discussed in the previous section. Strict functions get right down to
409 business, rather than filling up the heap with closures (the system's
410 notes to itself about how to evaluate something, should it eventually