# This is different from the standard module synopsis in # that it chooses performance over ease-of-use. # Think twice before micro-optimizing your Sereal usage. # Usually, Sereal is a lot faster than most of one's code, # so unless you are doing bulk encoding/decoding, you are # better off optimizing for maintainability. use Sereal qw(sereal_encode_with_object sereal_decode_with_object); my $enc = Sereal::Encoder->new(); my $dec = Sereal::Decoder->new(); my $big_data_structure = {...}; my $srldoc = sereal_encode_with_object($enc, $big_data_structure); my $and_back = sereal_decode_with_object($dec, $srldoc);
If you are not yet using re-usable Sereal::Encoder and Sereal::Decoder objects, then read no further. By switching from the "encode_sereal" and "decode_sereal" functions to either the OO interface or the advanced functional interface, you will get a noticeable speed boost as encoder and decoder structures can be reused. This is particularly significant for the encoder, which can re-use its output buffer. In some cases, such a warmed-up encoder can avoid most memory allocations.
I repeat, if you care about performance, then do not use the "encode_sereal" and "decode_sereal" interface.
The exact performance in time and space depends heavily on the data structure to be (de-)serialized. Often there is a trade-off between space and time. If in doubt, do your own testing and most importantly ALWAYS TEST WITH REAL DATA. If you care purely about speed at the expense of output size, you can use the "no_shared_hashkeys" option for a small speed-up, see below. If you need smaller output at the cost of higher CPU load and more memory used during encoding/decoding, try the "dedupe_strings" option and enable Snappy compression.
For ready-made comparison scripts, see the author_tools/bench.pl and author_tools/dbench.pl programs that are part of this distribution. Suffice to say that this library is easily competitive in both time and space efficiency with the best alternatives.
If switching to the OO interface is not enough, you may consider switching to the advanced functional interface that avoids method lookup overhead, and by inlining as custom Perl OPs, may also avoid some of the Perl function call overhead (Perl 5.14 and up). This additional speed-up is only a constant-offset, avoiding said method/function call, rather than speeding up encoding itself and so will be most significant if you are working with very small data sets.
"sereal_encode_with_object" and "sereal_decode_with_object" are optionally exported from the Sereal module (or "Sereal::Encoder" and "Sereal::Decoder" respectively). They work the same as the object-oriented interface except that they are invoked differently:
$srl_doc = $encoder->encode($data);
becomes
$srl_doc = sereal_encode_with_object($encoder, $data);
and
$data = $decoder->decode($srl_doc);
becomes
$data = sereal_decode_with_object($decoder, $srl_doc);
On Perl versions before 5.14, this will be marginally faster than the OO interface as it avoids method lookup. This should rarely matter. On Perl versions starting from 5.14, the function call to "sereal_encode_with_object" or "sereal_decode_with_object" will also be replaced with a custom Perl OP, thus avoiding most of the function call overhead as well.
In those cases, you can turn this feature off with the "no_shared_hashkeys" option for a small but measurable speed-up.
For support and discussion of Sereal, there are two Google Groups:
Announcements around Sereal (extremely low volume): <https://groups.google.com/forum/?fromgroups#!forum/sereal-announce>
Sereal development list: <https://groups.google.com/forum/?fromgroups#!forum/sereal-dev>
Damian Gryski
Steffen Mueller <smueller@cpan.org>
Rafaël Garcia-Suarez
Ævar Arnfjörð Bjarmason <avar@cpan.org>
Tim Bunce
Daniel Dragan <bulkdd@cpan.org> (Windows support and bugfixes)
Zefram
Some inspiration and code was taken from Marc Lehmann's excellent JSON::XS module due to obvious overlap in problem domain.
The license for the code in this distribution is the following, with the exceptions listed below:
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
Except portions taken from Marc Lehmann's code for the JSON::XS module, which is licensed under the same terms as this module. (Many thanks to Marc for inspiration, and code.)
Also except the code for Snappy compression library, whose license is reproduced below and which, to the best of our knowledge, is compatible with this module's license. The license for the enclosed Snappy code is:
Copyright 2011, Google Inc. All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. * Neither the name of Google Inc. nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.