#2181 Serialising a Buf

SlimerDude Sun 4 Aug 2013

Hi, what's the best (fastest) way to serialise and de-serialise the contents of a binary Buf?

There's toHex() and fromHex() but I'm guessing that's not very performant!

I'm looking to pass Bufs between Actors so need something immutable.

(Sometimes I wish Fantom had a mechanism to create your own immutable objects! Ulp! Danger!)

tcolar Sun 4 Aug 2013

You can create immutable objects, they just need to be const classes.

Sometimes you can use AtomicRef or Unsafe to pass a mutable objects (of course then it's not thread safe and you need to be careful what you are doing).

Do you actually need to pass the Buf around? could you not just pass a reference to it or something like that ? Not sure what you are trying to accomplish of course but seem unlikely to be a good idea to pass the buf(data) around.

brian Sun 4 Aug 2013

Typically I will pass Buf to actors using Unsafe if I absolutely know that no one else on the sending thread is modifying it. But otherwise probably Base64 encode/decoding is best way to serialize

SlimerDude Sun 4 Aug 2013

Cheers tcolar,

You can create immutable objects, they just need to be const classes.

Yeah, I remember Brain mentioning a while back that he usually copies data into some sort of const DTO for passing between Actors. This largely works for me, only with Bufs there's no way to get hold of the inner content to pass into a const class.

I've not used Unsafe for the longest time and hoped not to again - but given Thread 1 will never touch the Buf, and just wants to offload it entirely to Thread 2, Unsafe could work here. (I could sys::Buf.dup it just to make sure!?)

Sometimes I wish Fantom had a mechanism to create your own immutable objects!

I don't want to alter the Buf, just take a snapshot of it's contents and make it available to a different Actor for reading. As Lists and Maps have a toImmutable() implementation I was thinking it'd be nice if Buf did also.

brian Tue 6 Aug 2013

Yeah I've looked at toImmutable in Buf, but never figured out a good way to do without taking a performance hit since buffer I/O could require tons of extra checks

Login or Signup to reply.