Updating a Big State Fast in Haskell

前端 未结 3 1676
情深已故
情深已故 2021-02-07 20:11

For my vector graphics library in Haskell I must carry around a rather big state: line stroke parameters, colors, clip path etc. I know two ways of doing this. Quoting a comment

3条回答
  •  孤独总比滥情好
    2021-02-07 20:54

    As an aside, you should certainly be improving your data type representation via unboxing, if you are concerned about performance:

    data GfxState = GfxState {
      lineWidth :: {-# UNPACK #-}!Double,
      lineCap   :: {-# UNPACK #-}!Int,
      color     :: {-# UNPACK #-}!Color,
      clip      :: Path,
      ...
    }
    

    By unpacking the constructors, you improve the density of your data, going from a heap structure like this:

    enter image description here

    to the denser, stricter:

    enter image description here

    Now all the atomic types are laid out in consecutive memory slots. Updating this type will be much faster! BTW, 461.. is the Word representation of the pi field, a bug in my viewer library

    You'll also reduce the chance of space leaks.

    The cost of passing such a structure around will be very cheap, as the components will be stored in registers.

提交回复
热议问题