pickler combinators / parsing text and binary data with erlang

16 views
Skip to first unread message

Joel Reymont

unread,
Dec 4, 2008, 4:07:05 PM12/4/08
to think...@googlegroups.com
I'm working on the forum app using Nitrogen and hope to have something
shortly.

In the meantime, I thought it would be good to discuss something that
I'm particularly fond of: pickler combinators.

http://www.wagerlabs.com/blog/2008/02/parsing-text-an.html

What I plan to do and haven't done yet is benchmark the pickler
implementation vs the straightforward binary processing. Would anyone
would be interested in doing this and posting the results?

Please let me know if you have any questions!

Thanks, Joel

--
http://wagerlabs.com

Joel Reymont

unread,
Dec 4, 2008, 4:37:28 PM12/4/08
to Rusty Klophaus, think...@googlegroups.com

On Dec 4, 2008, at 9:13 PM, Rusty Klophaus wrote:

> Out of curiosity, why aren't you using erlang:term_to_binary/1 and
> erlang:binary_to_term/1?


This design is for talking to other systems that don't necessarily
have access to the EI library (C library to read and write Erlang
terms) or don't need the complexity. There's a significant overhead to
a pickled Erlang term too, e.g.

1> term_to_binary(1).
<<131,97,1>>
2> term_to_binary({1, 2}).
<<131,104,2,97,1,97,2>>

I'm using pickler combinators in OpenPoker, for example, and it talks
to a Flash client. The Flash implementation took a short time since
the binary protocol is fairly simple. I'm sure an iPhone
implementation won't take much time either.

Because pickler combinators allow for nested data structures, I'm able
to neatly describe the protocol. Note that tourney_info is a function
and so is record, int, etc.

int() and the rest create a tuple of two closures (anonymous
functions) where one element of the tuple is used to picke and the
other one to unpickle.

Example:

tourney_info() ->
record(tourney_info, {
tourney(),
tourney_type(),
seat_count(),
int(),
int(),
datetime(),
amount(),
amount(),
amount(),
amount(),
byte(),
byte()
}).

year() ->
int().

month() ->
byte().

day() ->
byte().

date_() ->
tuple({year(), month(), day()}).

hour() ->
byte().

minute() ->
byte().

second() ->
byte().

time_() ->
tuple({hour(), minute(), second()}).

datetime() ->
tuple({date_(), time_()}).

--
http://wagerlabs.com

Reply all
Reply to author
Forward
0 new messages