haskell - Does order of constructors / cases / guards / if-then-else matter to performance? -


i saw comment in containers/data/set/base.hs

-- [note: order of constructors] -- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -- order of constructors of set matters when considering performance. -- in ghc 7.0, when type has 2 constructors, forward conditional -- jump made when matching second constructor. successful match -- of first constructor results in forward jump not taken. -- on ghc 7.0, reordering constructors tip | bin bin | tip -- improves benchmark 10% on x86. 

where else order have tiny measurable impacts on performance? in particular wonder case statements many options.

it depends. order of constructors does, unfortunately, make difference. means order of patterns type not. whether write

foo (bin x y) = ... foo tip = ... 

or

foo tip = ... foo (bin x y) = ... 

makes no difference, because reordered constructor order immediately in "desugaring" process. order of matching multiple patterns semantically left-to-right, argument order can matter if use multiple patterns (you can around case). ghc feels free reorganize code, , ill. instance, if write

foo :: int -> bool foo x = x == 5 || x == 0 || x == 7 || x == 4 

ghc smash (essentially)

foo = \x -> case x of               0 -> true               4 -> true               5 -> true               7 -> true               _ -> false 

and sort of binary search of these possibilities. not optimal in many cases @ all, , annoying if happen know x==5 particularly likely. that's how now, , changing make things perform badly in situations without doing lot of work.


Comments

Popular posts from this blog

python - mat is not a numerical tuple : openCV error -

c# - MSAA finds controls UI Automation doesn't -

wordpress - .htaccess: RewriteRule: bad flag delimiters -