Here is a a function that checks if two unboxed vectors have the same length:
module Test where
import qualified Data.Vector.Unboxed as V
sameLength :: V.Vector Int -> V.Vector Int -> Bool
sameLength x y = V.length x == V.length y
I would expect sameLength to compile into code that just pattern matches on its arguments. Unfortunately, what I get is the following Core:
sameLength :: V.Vector Int -> V.Vector Int -> Bool
sameLength =
\ (x_a2P6 :: V.Vector Int) (y_a2P7 :: V.Vector Int) ->
case Test.$slength x_a2P6 of _ { GHC.Types.I# x1_a48j ->
case Test.$slength y_a2P7 of _ { GHC.Types.I# y1_a48n ->
GHC.Prim.tagToEnum# @ Bool (GHC.Prim.==# x1_a48j y1_a48n)
}
}
Test.$slength [InlPrag=[NEVER]] :: V.Vector Int -> Int
[GblId, Arity=1, Caf=NoCafRefs, Str=DmdType <S,1*U(A,U,A)>m]
Test.$slength = ...
For some reason, the $slength function, which computes the length of the vector, is marked with InlPrag=[NEVER]. Hence the sameLength function does lots of unnecessary boxing and unboxing.
In reality, this boxing and unboxing happens inside a tight loop where it causes a ~2x slowdown in my code.
Here is a a function that checks if two unboxed vectors have the same length:
I would expect
sameLengthto compile into code that just pattern matches on its arguments. Unfortunately, what I get is the following Core:For some reason, the
$slengthfunction, which computes the length of the vector, is marked withInlPrag=[NEVER]. Hence thesameLengthfunction does lots of unnecessary boxing and unboxing.In reality, this boxing and unboxing happens inside a tight loop where it causes a ~2x slowdown in my code.