Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Most efficient wire format for GHCJS code over websockets

I am working on a Haskell application, running in the browser compiled with GHCJS, which communicates with a server, also written in Haskell, over websockets. Both programs share the same Haskell data type definition, and I “just” have to pick serialization format.

At the moment, for simplicity, the program runs on Read and Show, which works, but is obviously not ideal.

On the other hand, it is unclear if the usual contenders for fast serialization, such as the cereal library, which work on ByteStrings are actually going to be efficient in GHCJS. Furthermore, GHCJS’s API seems to make it hard to let ByteStrings interact with the binary Blob type that the JavaScript bindings to Websockets provide.

Generic code generation (using GHC.Generics) would be nice.

Has anyone solved this problem before? Possibly even benchmarked various serialization variants on GHCJS?

like image 399
Joachim Breitner Avatar asked Dec 06 '16 23:12

Joachim Breitner


1 Answers

We were looking for a fast serializer/deserializer library in Haskell to store data in a reddis cache last year and eventually we ended up using ProtoBuf! That was partially because we already had ProtoBuf implementation of all of the objects that we wanted to serialize, but the performance was also much better compared to cereal/binary. By the time, store was non-existent.

The size and the speed of serialization/deserialization very much depends on your data as well. For instance, if you have lots of smallish (say in the range 1 to 100) 64 bit numbers, protobuf (because of its base 128 variant encoding) or even JSON could be more efficient than cereal or binary (that I guess use a fixed size for numbers regardless of their values).

There is also Typed-Wire that allows you to do serialization across a few languages, but I think it uses JSON as the underlying implementation.

i have no experience with GHCJS, but I'd recommend trying store first. Just make sure client and server have no little/big endianness incompatibility.

like image 108
Hapal Avatar answered Nov 09 '22 07:11

Hapal