1# Guidelines for writing well optimized code
3Stream operations are always part of a loop. Usually the loop consists of a
4stream generation or an unfold operation followed by stream transformation
5functions (e.g. map) and then a stream elimination operation or a fold
8The default or most common stream representation used in streamly is
9`StreamD` which is a direct style (compare with CPS representation
10`StreamK`) stream representation. All the direct style operations in
11a loop "fuse" together to form a tight machine loop eliminating any
12intermediate constructors, therefore, reducing allocations and cpu cost.
13This elimination of intermediate constructors in a stream loop is known
16## Writing high performance code using streamly
18This section outlines guidelines for writing code using the available
19combinators. The guidelines for writing new combinators are provided in
22Note that there is no absolute benchmark for performance, most of
23the time even without following the guidelines you may get excellent
24performance for the task at hand. Two important points to keep in mind
27* See if you actually need more performance
28* See if the code you are optimizing is in fast path
32When your performance requirements are stringent you may want to care
33about not breaking stream fusion unnecessarily. Certain operations do
34not fuse and act as a barrier to stream fusion. If these operations are
35part of a loop, the loop won't fuse completely. In some cases these may
36be necessary but in others it may be possible to replace these with
37better fusing operations. These operations include:
39* Avoid unnecessary use of stream append operations in code that should
40 fuse. Append operations in general force CPS style breaking stream
41 fusion. Some direct style append operations can fuse but they won't
42 scale for more than a few appends.
43* Avoid unnecessary use of the stream monad if fusion is important. You
44 may use functor or applicative though.
45* Concurrent stream operations cannot fuse
46* Operations involving exception primitives like catch/throw/mask
47 on elements of the stream cannot fuse.
48* Stream loops involving FFI calls on the elements of the stream
50The haddock documentation includes a note when an operation cannot fuse.
54* Use unfolds especially when higher order operations are involved. For
55 example, `unfoldMany` can fuse completely whereas `concatMap` would
57* Use `outerProduct` in `Unfold` module instead of using the monad instance of
58 streams to fuse nested loops where performance matters. See the unfold
59 benchmarks for an example. `outerProduct` can give you C like nested loop
64To make sure fusion can occur INLINE any operations that are part of a
65stream loop but factored out as separate functions.
69Keep the fold and scan accumulators strict. You could also consider
70using mutable state in a fold accumulator if the state is large. A
71large immutable structure as an accumulator may cause optimization
72issues. See the `WordCount` example for an example of this case.
74## Writing streamly combinators
76To enable stream fusion in a direct style stream all the operations that
77are part of a loop must be inlined:
79* Use an explicit INLINE pragma on any combinator that could be part of a loop
80 to ensure they will be inlined.
81* When a higher order function consumes another function then ensure that the
82 higher order function is inlined in the same phase or before the function it
83 is consuming. Use appropriate inline phases to ensure proper inline ordering
86### Streams and Unfolds
88Stream and Unfold State. Direct style stream and unfold combinators use a
89state data type to represent the internal state of the stream/unfold generator.
91* In some cases we may have to add a `FUSE` annotation on the state type to
92 ensure that all the internal join points created by GHC are inlined with the
93 help of the `fusion-plugin`.
94* In rare cases we may need to use a strictness annotation on the state
95 to allow fusion. `parseMany` and `splitOnSeq` are such examples.
97Step function of a stream or unfold:
99* Do not introduce unnecessary states. If there is only one entry point of a
100 state then perhaps you want to collapse the state into the state from which
101 it is called. More states means more jumps and may affect code locality, and
102 efficiency of low level code (e.g. register allocation) because of
103 independent placement of the code blocks in different states.
105 In some cases we may have to go against the above guideline and introduce a
106 separate state even though it is not necessary. See the GroupConsume state in
107 foldMany for an example.
108* Keep minimum possible data in state. More variables in state may lead
109 to poor performance because the state may not fit into registers and the
110 spill may cause allocations on each iteration of the loop. Mutable state
111 may help in such cases.
112* The step function must be annotated such that it gets inlined after the main
113 combinator (`INLINE_LATE`).
115Multiple yield points or single?:
117* A single yield point is usually desirable, however, not always necessary.
118 In some cases multiple yield points may in fact be needed for fusion,
119 see `splitOnSeq` for an example. Or maybe its fusing because of a
120 direct yield instead of going through an indirect common yielding
123Recursion in step function:
125* In general, we avoid making the step function recursive. Use the
126 `Skip` constructor to remove recursion. Recursive step function can
127 introduce optimization barriers that are harder to remove by the GHC
130 However, in some cases it may be better to have a local recursive
131 loop. A recursive loop can help us avoid threading around some large
132 state values every time. Values that do not change across a loop can
133 be factored in the scope outside the loop (static argument transform),
134 this way we can create a local loop which is more efficient than
135 threading around the state in a larger loop. See the `splitOnSeq`
136 combinator as an example where we use a local recursive loop, it fuses
137 and is significantly efficient compared to using `Skip`.
139 In a local recursive loop use SPEC and annotate even the rest of the
140 loop arguments as strict where needed. We have observed that when the
141 arguments were not strict the loop does not fuse (splitOnSeq).
143### Fold and Parser drivers
145Recursive loop closing operations:
147* When writing recursive looping combinators using StreamD (e.g. foldlM' in
148 StreamD) use a strict SPEC argument in the recursive loops to ensure that
149 "Spec Constructor" optimization removes boxed arguments and reduces
152### Fold and Parser combinators
154State of a Fold or Parser:
156* The accumulator of a fold must be a strict data structure. Use the strict
157 data structures provided in `Streamly.Internal.Data` for this purpose or use
158 explicit bang annotation to make the data strict.
159* In some cases you may need a `FUSE` annotation on the state type to ensure
160 that any internal join points created by GHC are always inlined with the help
161 of the fusion-plugin.
163The step function of a Fold or Parser:
165* Never make the fold step recursive, recursion creates an optimization barrier
166 for the GHC simplifier. Use `Partial` or `Continue` constructors to avoid
169* Sometimes you may need an explicit INLINE on the step function.
171### NOINLINE, isolating the closed loop
173We want the loop iterations to be optimized and the loop stages to be fused to
174generate a tighter loop. However, it is not necessarily optimal to inline the
175whole loop itself into a parent function. For example, consider the following
176function in the `FileSystem.Handle` benchmarks:
179{-# NOINLINE readWriteAfter_Stream #-}
180readWriteAfter_Stream :: Handle -> Handle -> IO ()
181readWriteAfter_Stream inh devNull =
182 let readEx = IP.after_ (hClose inh) (S.unfold FH.read inh)
183 in S.fold (FH.write devNull) readEx
186If this is inlined into a parent benchmark group list, this leads to
187many times performance degradation. That's because inlining the loop
188into a bigger structure interferes with the optimization of the loop
189body itself and it may not fuse. Whereas it is desirable to INLINE all
190the stages of a loop, it is often not useful to inline the whole loop
191itself, in fact we may have to occasionally use a `NOINLINE` so that the
192compiler does not inline it.
196It is not always useful to specialize function calls for all constructors, some
197constructors may just add to code bloat or add overhead of passing unboxed
198values in a loop. In such cases the `NoSpecConstr` annotation can be useful.
199See the `parselMx'` function in `StreamD` module.
201### StreamK operations
203StreamK uses foldr/build fusion to a very limited degree. StreamK is not the
204primary representation in streamly but is used for several operations that
205cannot scale in StreamD representation. StreamK is relatively immune to compiler
206optimizations. In some cases you may need an INLINE pragma to improve the
209### How to debug non-fusing code?
211Strip down the code to a minimal version until it starts fusing and then
212start building it up from there adding more things incrementally. At
213each stage keep checking if the code is fusing. At some point it
214won't fuse. See what we added that made the code not fuse. Go through
215the guidelines above to check if we did something that that is not
216recommended. Or raise an issue for GHC to be fixed if possible.
218# GHC Optimizations In Streamly
220There are three important levels of optimizations used in streamly:
222* CPS style stream representation (StreamK) to direct style (StreamD)
223 conversion and vice-versa using rewrite rules
224* Stream fusion using case-of-case optimization in StreamD
225* foldr/build fusion using rewrite rules in StreamK
229Inlining of functions at the right time is crucial for all these optimization
230to work. A missing inline or inline in an incorrect GHC simplifier phase can
231adversely impact performance. We use three builtin phases of GHC simplifier
232for inlining i.e. phase 0, 1 and 2. We have defined them as follows in
236#define INLINE_EARLY INLINE [2]
237#define INLINE_NORMAL INLINE [1]
238#define INLINE_LATE INLINE [0]
241We also use INLINE [3] in some cases.
243## `fromStreamD/toStreamD` Fusion
245The combinators in `Streamly.Prelude` are defined in terms of combinators in
246`Streamly.Internal.Data.Stream.StreamD` (Direct style streams) or
247`Streamly.Internal.Data.Stream.StreamK` (CPS style streams). We convert the
248stream from `StreamD` to `StreamK` representation or vice versa in some cases.
250Most operations use the StreamD representation, however, stream append
251operations and monad instances use StreamK representation because StreamD would
252not perform well in these cases. We use rewrite rules to convert from one
253representation to another when required. For this reason the combinators in
254Streamly.Prelude are written using fromStreamD/fromStreamK etc.
256In the first inlining phase (INLINE_EARLY or INLINE) we expand the combinators
257in `Streamly.Prelude` into fromStreamD/fromStreamK/toStreamD/toStreamK and
258combinators defined in StreamD or StreamK modules. Once we do that
259fromStreamD/toStreamD get exposed and we can apply rewrite rules to rewrite
260transformations like `fromStreamK . toStreamK` to `id`. A plain `INLINE`
261pragma is usually enough on combinators in `Streamly.Prelude`.
264{-# RULES "fromStreamK/toStreamK fusion"
265 forall s. toStreamK (fromStreamK s) = s #-}
268Also, we have to prevent fromStreamK and toStreamK themselves from inlining in
269this phase so that rewrite rules can be applied on them, therefore, we annotate
270these functions with `INLINE_LATE`.
274In some cases, if the operation could not fuse we want to use a fallback
275rewrite rule in the next phase. For such cases we use the INLINE_EARLY phase
276for the first rewrite and the INLINE_NORMAL phase for the fallback rules.
278The fallback rules make sure that if we could not fuse the direct style
279operations then better use the CPS style operation, because unfused direct
280style would have worse performance than the CPS style ops.
283{-# INLINE_EARLY unfoldr #-}
284unfoldr :: (Monad m, IsStream t) => (b -> Maybe (a, b)) -> b -> t m a
285unfoldr step seed = fromStreamS (S.unfoldr step seed)
286{-# RULES "unfoldr fallback to StreamK" [1]
287 forall a b. S.toStreamK (S.unfoldr a b) = K.unfoldr a b #-}
290## High Level Operation Fusion
292Since each high level combinator in `Streamly.Prelude` is wrapped in
293`fromStreamD/toStreamD` etc. the combinator fusion cannot work unless we have
294removed those and exposed consecutive operations e.g. a `map` followed by
295another `map`. Assuming that redundant `fromStreamK/toStreamK` have been
296removed in the `INLINE_EARLY` phase, we can then apply the combinator fusion
297rules in the `INLINE_NORMAL` phase. For example, we can fuse two `map`
298operations into a single `map` operation. Note that now we have exposed the
299`StreamD/StreamK` implementations of combinators and the rules would apply on
302## Inlining Higher Order Functions
304Note that partially applied functions cannot be inlined. So if we have a code
308concatMap1 src = runStream $ S.concatMap (S.replicate 3) src
311We want to ensure that `concatMap` gets inlined before `replicate` so that
312`replicate` becomes fully applied before it gets inlined. Currently ensuring
313that both of them are inlined in the same phase (`INLINE_NORMAL`) seems to be
314enough to achieve that. In general, we should try to ensure that higher order
315functions are inlined before or in the same phase as the functions they can
316consume as arguments. This means `StreamD` combinators should not be marked
317as `INLINE` or `INLINE_EARLY`, instead they should all be marked as
318`INLINE_NORMAL` because higher order functions like `concatMap`/`map`/`mapM`
319etc are marked as `INLINE_NORMAL`. `StreamD` functions in other modules like
320`Streamly.Data.Array.Foreign` should also follow the same rules.
324In StreamD combinators, inlining the inner step or loop functions too early
325i.e. in the same phase or before the outer function is inlined may block stream
326fusion opportunities. Therefore, the inner step functions and folding loops are
327marked as INLINE_LATE.
331In some cases, the `step` function in `StreamD` does not get specialized when
332inlined unless it is provided with an explicit signature or made a lambda, for
333example, in the `replicate/replicateM` combinator we need the type annotation
334on `i` to get it specialized:
337 {-# INLINE_LATE step #-}
343 return $ Yield x (i - 1)
346`-flate-specialise` also helps in this case.
348## Stream and Fold State Data Structures
350Since state is an internal data structure threaded around in the loop, it is a
351good practice to use strict unboxed fields for state data structures where
352possible. In most cases it is not necessary, but in some cases it may affect
353fusion and make a difference of 10x performance or more. For example, using
354non-strict fields can increase the code size for internal join points and
355functions created during transformations, which can affect the inlining of
356these code blocks which in turn can affect stream fusion.
358See https://gitlab.haskell.org/ghc/ghc/issues/17075 .
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list