【问题标题】:Unboxing boxed value in vector of four tuples在四个元组的向量中取消装箱值
【发布时间】:2013-06-05 17:51:33
【问题描述】:

作为更复杂代码的一部分,我正在尝试调试一个性能问题。我用来创建动态的、可增长的(Int,Int,Int,Int) 向量的append 函数似乎导致元组中的Int 之一在写入向量之前被装箱和拆箱。我编写了一个更简单的代码来重现该问题 - 它似乎仅在我在 append 函数中添加矢量增长功能时才会发生 - 下面的示例代码(除了重现问题之外它没有做太多有用的工作),然后是 sn- ps of core 显示被装箱和拆箱的值:

{-# LANGUAGE BangPatterns #-}
module Test
where
import Data.Vector.Unboxed.Mutable as MU
import Data.Vector.Unboxed as U hiding (mapM_)
import Control.Monad.ST as ST
import Control.Monad.Primitive (PrimState)
import Control.Monad (when)
import GHC.Float.RealFracMethods (int2Float)
import Data.STRef (newSTRef, writeSTRef, readSTRef)
import Data.Word

type MVI1 s  = MVector (PrimState (ST s)) Int
type MVI4 s  = MVector (PrimState (ST s)) (Int,Int,Int,Int)
data Snakev s = S {-# UNPACK #-}!Int
                                !(MVI4 s)

newVI1 :: Int -> Int -> ST s (MVI1 s)
newVI1 n x = do
          a <- new n
          mapM_ (\i -> MU.unsafeWrite a i x) [0..n-1]
          return a

-- Growable array - we always append an element. It grows by factor of 1.5 if more capacity is needed
append :: Snakev s -> (Int,Int,Int,Int) -> ST s (Snakev s)
append (S i v) x = do
   if i < MU.length v then MU.unsafeWrite v i x >> return (S (i+1) v)
   else MU.unsafeGrow v (floor $! 1.5 * (int2Float $ MU.length v)) >>= (\y -> MU.unsafeWrite y i x >> return (S (i+1) y))

gridWalk :: Vector Word8 -> Vector Word8 -> MVI1 s -> MVI1 s -> Snakev s -> Int -> (Vector Word8 -> Vector Word8 -> Int -> Int ->      Int) -> ST s (Snakev s)
gridWalk a b fp snodes snakesv !k cmp = do
   let offset = 1+U.length a
       xp = offset-k
   snodep <- MU.unsafeRead snodes xp -- get the index of previous snake node in snakev array
   append snakesv (snodep,xp,xp,xp)
{-#INLINABLE gridWalk #-}

GHC 生成一个版本的append 用于gridWalk。该函数的核心是 $wa - 请注意加框的 Int 参数:

$wa
  :: forall s.
     Int#
     -> MVI4 s
     -> Int#
     -> Int#
     -> Int#
     -> Int  ======= Boxed value - one of (Int,Int,Int,Int) is boxed
     -> State# s
     -> (# State# s, Snakev s #)
$wa =
  \ (@ s)
    (ww :: Int#)
    (ww1 :: MVI4 s)
    (ww2 :: Int#)
    (ww3 :: Int#)
    (ww4 :: Int#)
    (ww5 :: Int) === Boxed value
    (w :: State# s) ->

....
....
of ipv12 { __DEFAULT ->
              case (writeIntArray# ipv7 ww ww4 (ipv12 `cast` ...)) `cast` ...
              of ipv13 { __DEFAULT ->
              (# case ww5 of _ { I# x# ->
                 (writeIntArray# ipv10 ww x# (ipv13 `cast` ...)) `cast` ...
                 },
                 S (+# ww 1)
                   ((MV_4
                       (+# y rb)
                       ==== x below unboxed from arg ww5 ======
                       ((MVector 0 x ipv1) `cast` ...) 
                       ((MVector 0 x1 ipv4) `cast` ...)
                       ((MVector 0 x2 ipv7) `cast` ...)
                       ((MVector 0 x3 ipv10) `cast` ...))
                    `cast` ...) #)

gridWalk 在调用append 时将值框起来:

=== function called by gridWalk ======
a :: forall s.
     Vector Word8
     -> Vector Word8
     -> MVI1 s
     -> MVI1 s
     -> Snakev s
     -> Int
     -> (Vector Word8 -> Vector Word8 -> Int -> Int -> Int)
     -> State# s
     -> (# State# s, Snakev s #)
a =
  \ (@ s)
    (a1 :: Vector Word8)
    _
    _
    (snodes :: MVI1 s)
    (snakesv :: Snakev s)
    (k :: Int)
    _
    (eta :: State# s) ->
    case k of _ { I# ipv ->
    case snodes `cast` ... of _ { MVector rb _ rb2 ->
    case a1 `cast` ... of _ { Vector _ rb4 _ ->
    let {
      y :: Int#
      y = -# (+# 1 rb4) ipv } in
    case readIntArray# rb2 (+# rb y) (eta `cast` ...)
    of _ { (# ipv1, ipv2 #) ->
    case snakesv of _ { S ww ww1 ->
    ====== y boxed below before append called ======
    $wa ww ww1 ipv2 y y (I# y) (ipv1 `cast` ...) 
    }
    }
    }
    }
    }

因此,效果似乎是在插入(Int,Int,Int,Int) 的向量之前将gridWalk 中的值装箱并在append 中取消装箱。标记 append INLINE 不会改变行为 - 那些装箱的值只是在 gridWalk 的函数体中移动。

我会很感激关于如何使这个值取消装箱的指针。我希望在重构时保留append 的功能(即,在超出容量时处理向量增长)。

GHC 版本是7.6.1。矢量版是0.10

【问题讨论】:

  • 我不知道为什么它被装箱了,我也不想在这个夜晚和啤酒的时候找出答案,但append (S i v) !x@(_,_,_,!_) = ... 也得到了最后一个未装箱的东西。看起来肯定有问题,但是,可能值得开票。
  • @DanielFischer,是的,你说得对,用严格的模式拆箱。我会得到一个更简单的案例来重现 GHC trac。

标签: performance haskell vector ghc unboxing


【解决方案1】:

这只是一个评论。我想我会摆脱元组参数(调整gridWalkappend 的使用),但结果是(仅)最后一个 Int 参数必须被 bang'd 以使所有内容都拆箱,这似乎奇怪:

append :: Snakev s -> Int -> Int -> Int -> Int -> ST s (Snakev s)
append (S i v) a b c !d = do
   if i < len then do MU.unsafeWrite v i (a,b,c,d)
                      return $ S (i+1) v
              else do y <- MU.unsafeGrow v additional        
                      MU.unsafeWrite y i (a,b,c,d) 
                      return $ S (i+1) y
  where len = MU.length v           
        additional = floor (1.5 * int2Float len) -- this seems kind of bizarre 
                                        -- by the way; can't you stay inside Int?
                                        --  3 * (len `div` 2) or something

另外,编辑,如果您将S (i+1) 的应用程序移到 do 块之外,所有内容都会被取消装箱,但我不确定这是否让我们更接近采石场...:

append :: Snakev s -> Int -> Int -> Int -> Int -> ST s (Snakev s)
append (S i v) a b c d = do
       if i < len then liftM (S (i+1)) $ do MU.unsafeWrite v i (a,b,c,d)
                                            return v
                  else liftM ( S (i+1)) $ do y <- MU.unsafeGrow v zzz         
                                             MU.unsafeWrite y i (a,b,c,d) 
                                             return  y
      where len = MU.length v           
            zzz = floor (1.5 * int2Float len)     

但是,如果liftMfmap 替换,我们将回到单独的未装箱状态。如果liftM (S (1+i) fmap (S (i+1) 一直移到前面,事情就会顺利进行:

append (S i v) a b c d = S (i+1) <$> do ...

【讨论】:

  • 是的,在元组模式中强制 bang 也可以解决问题。我会提交一张票,看看 GHC 总部怎么说。
  • 感谢提醒。我已经提交了矢量库的错误票。由于 liftM 解决方法,接受您的评论作为答案。
  • @marc_s,这里的“采石场”诗意地指正在追求的目标,就像被猎杀的动物一样。这不是错误。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-01-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-06-20
相关资源
最近更新 更多