[Haskell-cafe] faster faster faster but not uglier (how to make nice code AND nice core)?
Henning Thielemann
lemming at henning-thielemann.de
Wed May 19 21:12:55 UTC 2021
On Wed, 19 May 2021, Johannes Waldmann wrote:
> I wanted a really fast implementation of a vector addition system
> where the "vector" contains small numbers only.
With integer elements?
> and I want it packed into a machine word
> (and finally put these into Data.IntSet).
Why not using Integer? With a bit of bit tricks you can implement a vector
add via Integer addition.
https://en.wikipedia.org/wiki/SWAR
E.g. like this
vectorAdd x y =
maskOddElements (maskOddElements x + maskOddElements y)
+
maskEvenElements (maskEvenElements x + maskEvenElements y)
But I suspect that larger Integers are not hold in registers.
Chances are better for a record of Word8's, Word16's ...
Maybe if they are put in a compact region:
http://hackage.haskell.org/package/ghc-compact
Or you use the CPU vector types that GHC provides.
http://hackage.haskell.org/package/ghc-prim-0.7.0/docs/GHC-Prim.html#g:35
There seem to be some packages for small vectors:
https://hackage.haskell.org/package/compact-word-vectors
https://hackage.haskell.org/package/smallarray
More information about the Haskell-Cafe
mailing list