Updating a Big State Fast in Haskell

前端 未结 3 1675
情深已故
情深已故 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:44

    First of all I have to ask are you just claiming it's going to be slow or did you profile or at least notice a performance issue? otherwise guessing or making assumptions isn't particuarly useful. Anyway I recommend grouping your data, at the moment it looks like you're just laying out your structure completely flat when you could group related data like the data related to lines into records.

    You might also want to seperate out bits that really need to be in the state monad and others that don't into a reader/writer monad and combine them using monad transformers. Regarding elegance of code I'd recommend using (first-class/higher-order) record libraries like fclabels.

    I've made heavy use of state monads (in a stack of a monad transformer) in a few small projects and I haven't noticed any performance issues yet.

    Lastly you could use modify instead of a get/put pairs.

提交回复
热议问题