Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

why polymorphism is so costly in haskell(GHC)?

I am asking this question with refernce to this SO question. Accepted answer by Don stewart : First line says "Your code is highly polymorphic change all float vars to Double .." and it gives 4X performance improvement.

I am interested in doing matrix computations in Haskell, should I make it a habit of writing highly monomorphic code?

But some languages make good use of ad-hoc polymorphism to generate fast code, why GHC won't or can't? (read C++ or D)

why can't we have something like blitz++ or eigen for Haskell? I don't understand how typeclasses and (ad-hoc)polymorphism in GHC work.

like image 342
fedvasu Avatar asked Sep 17 '13 16:09

fedvasu


2 Answers

With polymorphic code, there is usually a tradeoff between code size and code speed. Either you produce a separate version of the same code for each type that it will operate on, which results in larger code, or you produce a single version that can operate on multiple types, which will be slower.

C++ implementations of templates choose in favor of increasing code speed at the cost of increasing code size. By default, GHC takes the opposite tradeoff. However, it is possible to get GHC to produce separate versions for different types using the SPECIALIZE and INLINABLE pragmas. This will result in polymorphic code that has speed similar to monomorphic code.

like image 191
Dirk Holsopple Avatar answered Sep 19 '22 07:09

Dirk Holsopple


I want to supplement Dirk's answer by saying that INLINABLE is usually recommended over SPECIALIZE. An INLINABLE annotation on a function guarantees that the module exports the original source code of the function so that it can be specialized at the point of usage. This usually removes the need to provide separate SPECIALIZE pragmas for every single use case.

Unlike INLINE, INLINABLE does not change GHC's optimization heuristics. It just says "Please export the source code".

like image 32
Gabriella Gonzalez Avatar answered Sep 21 '22 07:09

Gabriella Gonzalez