Merge: Intro MessagePack serialization engines
authorJean Privat <jean@pryen.org>
Fri, 22 Sep 2017 18:30:20 +0000 (14:30 -0400)
committerJean Privat <jean@pryen.org>
Fri, 22 Sep 2017 18:30:20 +0000 (14:30 -0400)
commit595d9c4dbc3ea53445c7c26b105f5b54fa9e3174
tree6b70ec3a6589e7d636a496c84b603e6c9b9d6ec1
parentc53f3221eaf849e7f09da8cb683e3f10b64d3a7d
parent80f6b1c180f6c29b6397f0f792f8b1e6fbd41cbe
Merge: Intro MessagePack serialization engines

MessagePack is a portable data serialization format. It differs from JSON by its binary format, compressing common types, support for custom types and accepting any object as map key. Here, the primitive Nit types use the shortest available MessagePack format (e.g. an `Int` between -32 and 127 takes a single byte). The custom types are used to declare some metadata and special types (objects, references, `Char`, etc.). The engines can, optionally, benefit from the map keys as any object to compress repeated metadata (class and attribute names).

These engines should replace the current `Binary::serialization` services, which are almost impossible to debug, less compressed than MessagePack and not standard.

The serialization engine supports writing and reading plain (no metadata) MessagePack data, similarly to the JSON engines.

Most clients should use the high-level services:
* Customizable engines to handle errors: `MsgPackSerializer` and `MsgPackDeserializer`.
* Quick services on streams: `Writer::serialize_msgpack` and `Reader::deserialize_msgpack`.
* Quick services on `Bytes` (mostly for testing): `Serializable::serialize_msgpack` and `Bytes::deserialize_msgpack`.

Or, to write engines one can use the low-level services:
* Low-level writing methods: `Writer::write_msgpack_uint64`, `Writer::write_msgpack_bin8`, etc.
* Low-level reading method: `Reader::read_msgpack`.

---

Using it in Jwrapper, the MessagePack format creates smaller files than JSON (at ~50% the size), when using the default options and serializing the metadata of the Java standard library:

* JSON: 14.9 MB
* MessagePack (default): 7.0 MB
* Old binary: 9.8 MB

The file gets shorter in plain MessagePack mode or when caching the metadata strings:
* MessagePack `plain_msgpack=true`: 6.3 MB
* MessagePack `cache_metadata_strings=true`: 3.0 MB

Caching metadata creates much smaller files, however for the size only gzip has a bigger effect.
Further investigations is required to see the impact it has on the speed.

* gzipped MessagePack (default): 0.7 MB
* gzipped MessagePack `cache_metadata_strings=true`: 0.6 MB

---

This PR depends on #2539 as it works on binary data, you can ignore the first 2 commits.

@R4PaSs you may want to look at the commits modifying `Int::to_bytes` and `Bytes::to_i`.

Pull-Request: #2550
Reviewed-by: Jean Privat <jean@pryen.org>
lib/serialization/engine_tools.nit