Skip to content

Instantly share code, notes, and snippets.

@paraseba
Created June 20, 2018 16:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save paraseba/bee7e8d2f26efbbffc18a027434f9824 to your computer and use it in GitHub Desktop.
Save paraseba/bee7e8d2f26efbbffc18a027434f9824 to your computer and use it in GitHub Desktop.
Uncommenting the type signature produces slower code
import qualified Data.Vector.Generic.Mutable as MV
--mutableEval :: MV.MVector v Int8 => [Op] -> v RealWorld Int8 -> Int -> IO Int
mutableEval [] _ pos = return pos
mutableEval (op:ops) mem pos = case op of
Inc n memOffset ->
(MV.unsafeModify mem (+ fromIntegral n) (pos + coerce memOffset) >> mutableEval ops mem pos)
MRight n -> mutableEval ops mem (pos + coerce n)
{-
Relevant Generated code with the type signature above commented out: (notice for example primitive arithmetic in the MRight case)
case op of {
__DEFAULT ->
case Control.Exception.Base.patError
"src/HBF/Eval.hs:(179,33)-(185,50)|case"#
of wild2 {
};
Inc dt dt1 ->
let {
y :: Int#
y = +# sc1 dt1 } in
case readInt8Array# sc4 (+# sc2 y) (sc `cast` <Co:33>) of
{ (# ipv, ipv1 #) ->
case writeInt8Array#
sc4 (+# sc2 y) (narrow8Int# (+# ipv1 (narrow8Int# dt))) ipv
of s'#
{ __DEFAULT ->
jump $smutableEval
(s'# `cast` <Co:31>) sc1 sc2 sc3 sc4 @~ <Co:9> ops
}
};
MRight dt ->
jump $smutableEval sc (+# sc1 dt) sc2 sc3 sc4 @~ <Co:1> ops
}
Also in the generate -simpl file: this type signature
mutableEval
:: forall (v :: * -> * -> *).
MV.MVector v Int8 =>
[Op] -> v RealWorld Int8 -> Int -> IO Int
mutableEval = mutableEval1 `cast` <Co:23>
If I uncomment the type signature, which is the same the -simple file is showing, I get this very slow type of code:
MRight dt ->
((mutableEval
$dMVector
ops
ds1
(case pos of { I# x -> ghc-prim-0.5.1.1:GHC.Types.I# (+# x dt) }))
`cast` <Co:2>)
eta;
Notice it's wrapping and unwrapping with I#
-}
-- supporting code
newtype MemOffset =
MemOffset Int
deriving (Generic)
deriving newtype (Show, Eq, Num, Ord)
deriving anyclass (Binary, NFData)
data Op
= Inc !Int
!MemOffset
| MRight !MemOffset
-- ..... more constructors here
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment