Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why are the functions making Vector an instance of Functor, Monad, Applicative, Alternative, Foldable and Traversable slow?

The changelog for version 0.8 of vector lists the following change with a warning:

Functor, Monad, Applicative, Alternative, Foldable and Traversable instances for boxed vectors (WARNING: they tend to be slow and are only provided for completeness).

Could someone explain why this is the case? Is it just the normal cost of typeclass specialization, or something more interesting?

Update: Looking at some particular instances, one sees for example:

instance Foldable.Foldable Vector where
  {-# INLINE foldr #-}
  foldr = foldr

and similarly for the other folds. Does this mean that folding is slow for Vectors in general? If not, what makes a non-specialized fold slow enough to warrant a warning?

like image 354
gspr Avatar asked Sep 28 '11 12:09

gspr


1 Answers

I submitted the original set of these instances to Roman a year and a half ago and have maintained vector-instances since then. (I had to remove these instances from vector-instances once they migrated into vector, and now maintain it solely for the really exotic stuff). His concern was that if folks used these instances polymorphically then the RULES that make Vectors fuse away can't fire unless the polymorphic function gets inlined and monomorphized.

They exist because not every bit of code on the planet is Vector-specific and even then it is nice to sometimes use the common names.

Slow here is relative. The worst case is they perform like anybody else's folds, binds, etc. but Roman takes every single boxed value as a personal insult. :)

like image 173
Edward Kmett Avatar answered Nov 17 '22 16:11

Edward Kmett