Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a better performing alternative to read and show in Haskell?

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?

like image 740
sdasdadas Avatar asked Aug 26 '14 01:08

sdasdadas


1 Answers

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.

like image 77
Ørjan Johansen Avatar answered Nov 13 '22 03:11

Ørjan Johansen