[Haskell-cafe] CPS versus Pattern Matching performance

Tony Finch dot at dotat.at
Wed Jul 11 06:14:20 EDT 2007


I've recently been wondering (in a more general context than just haskell)
about optimisations along the lines of

foo x y z = if complicated thing then Cons1 a b c
            else if other complicated thing then Cons2 d e
            else Cons3 f

bar some args = case foo perhaps different args of
                Cons1 a b c -> code for case one
                Cons2 a b -> code for case two
                Cons3 a -> code for case three

into

foo x y z k1 k2 k3 =
            if complicated thing then k1 a b c
            else if other complicated thing then k2 d e
            else k3 f

bar some args = foo perhaps different args
                 (\ a b c -> code for case one)
                 (\ a b -> code for case two)
                 (\ a -> code for case three)

Such that you save a cons (unless the compiler can return the value in
registers or on the stack) and a case analysis branch, but a normal
function return (predictable by the CPU) is replaced by a less-predictable
indirect jump. Does anyone have references to a paper that discusses an
optimisation like this for any language, not just Haskell?

Tony.
-- 
f.a.n.finch  <dot at dotat.at>  http://dotat.at/
SHANNON ROCKALL: VARIABLE 3, BECOMING SOUTH OR SOUTHEAST 4 OR 5, OCCASIONALLY
6, VEERING WEST LATER. MODERATE OR ROUGH. RAIN WITH FOG PATCHES, SHOWERS
LATER. MODERATE OR GOOD, OCCASIONALLY VERY POOR.


More information about the Haskell-Cafe mailing list