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
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:
to the denser, stricter:
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.