在Haskell中读取和显示是否有更好的替代方案?

sda*_*das 14 serialization haskell

我正在编写一个网络原型,其中Server传输World所有的状态Clients.我使用以下工作流程执行此操作:

Server --> World --> show --> ByteString --> GZip.compress   --> udp send
Client <-- World <-- read <-- ByteString <-- GZip.decompress <-- udp receive
Run Code Online (Sandbox Code Playgroud)

然而,它似乎是show并且read是性能瓶颈 - 占据了大部分时间.随着World增长,这只会是一个巨大的挑战.

我知道,我将不得不停止在某一时刻发送了整个世界,但没有任何替代品使用readshow一个数据结构转换成ByteString

Ørj*_*sen 16

我相信二进制包为二进制格式提供非常有效的去/序列化.

编辑:请求代码,这是Generic(BSD3)文档中的示例:

{-# LANGUAGE DeriveGeneric #-}

import Data.Binary
import GHC.Generics (Generic)

data Foo = Foo
         deriving (Generic)

-- GHC will automatically fill out the instance
instance Binary Foo
Run Code Online (Sandbox Code Playgroud)

然后你可以使用encode代替showdecode不是代替read.请注意,该包具有其他一些可能有用的功能,并且可以自定义该格式.