I am writing a networking prototype where a Server transmits the state of the World to all of its Clients. I do this using the following workflow:
Server --> World --> show --> ByteString --> GZip.compress --> udp send
Client <-- World <-- read <-- ByteString <-- GZip.decompress <-- udp receive
However, it appears that show and read are performance bottlenecks - taking up the majority of the time. As the World grows, this will only prove a great challenge.
I know that I'll have to stop sending the whole world at some point, but are there any alternatives to using read and show to convert a data structure into a ByteString?
I believe the binary package provides very efficient de-/serialization to/from binary formats.
EDIT: Code was requested, this is the Generic example from the (BSD3) documentation:
{-# LANGUAGE DeriveGeneric #-}
import Data.Binary
import GHC.Generics (Generic)
data Foo = Foo
deriving (Generic)
-- GHC will automatically fill out the instance
instance Binary Foo
Then you can e.g. use encode instead of show and decode instead of read. Note that the package has several other functions that may be useful, and that the format may be customized.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With