fs2-data icon indicating copy to clipboard operation
fs2-data copied to clipboard

Add high-level msgpack serialization module

Open jarmuszz opened this issue 3 months ago • 0 comments

This MR brings high-lever serialization for the msgpack module.

There are four new endpoints in total:

  • high.toItems[F, A]: Pipe[F, A, MsgpackItem]
  • high.serialize[F, A]: Pipe[F, A, Byte]
  • high.ast.valuesToItems[F]: Pipe[F, MsgpackValue, MsgpackItem]
  • high.ast.valuesToBytes[F]: Pipe[F, MsgpackValue, Byte]

which work by calling MsgpackSerializer instances on values in the stream.

Implicit instances of MsgpackSerializer can be accessed by calling MsgpackSerializer[A]. Applying a value of type A to a MsgpackSerializer[A] returns Either[String, Chunk[MsgpackItem]].

There is also a shorthand syntax for summoning and applying a serializer:

import fs2.data.msgpack.high.*

case class Foo(x: Int, y: String)
val s: MsgpackSerializer[Foo] = foo =>
  for {
    x <- MsgpackSerializer[Int](foo.x)
    y <- foo.y.serialize // The same as MsgpackSerializer[String](foo.y)
  } yield x ++ y

Benchmarks perform as follows on i5-1334U, balanced power profile:

MsgpackSerializerBenchmarks.serialize        avgt   10  1753.563 ± 27.627  us/op
MsgpackSerializerBenchmarks.serializeValues  avgt   10  2094.292 ± 68.639  us/op

In the process of making this PR, I have also made a few smaller changes in other parts of the msgpack module:

  • 2fd8df04a188e9b4c0f1c7898de6b250e3e66191 fixes js.Date deserialization
  • f9b363680928f8ba37ec2afca1ad7e128faffa04 renames low.items to low.fromBinary to make it fit the rest of the functions
  • cdd9e424d5fd558b75ee15a08f71124370b23451 makes deserializers properly request more items if collections span multiple chunks
  • 08deb065a1a0757f1c1d01acfaa63a09d2a3d071 fixes an edge-case of BigInt deserialization

jarmuszz avatar Oct 04 '25 19:10 jarmuszz