]> Sergey Matveev's repositories - public-inbox.git/commitdiff
ipc: support Sereal
authorEric Wong <e@80x24.org>
Thu, 31 Dec 2020 13:51:33 +0000 (13:51 +0000)
committerEric Wong <e@80x24.org>
Fri, 1 Jan 2021 05:00:39 +0000 (05:00 +0000)
Some testing will be needed to see if it's worth the code
and maintenance overhead, but it seems easy-enough to get
working.

lib/PublicInbox/IPC.pm
t/ipc.t

index c04140ae03368f0bd43955f2a4536c925f62e501..0baa218c016fdd1271aeddd4bd0a7b353232d9c6 100644 (file)
@@ -9,6 +9,29 @@ use v5.10.1;
 use Socket qw(AF_UNIX SOCK_STREAM);
 use Carp qw(confess croak);
 use PublicInbox::Sigfd;
+my ($enc, $dec);
+# ->imports at BEGIN turns serial_*_with_object into custom ops on 5.14+
+# and eliminate method call overhead
+BEGIN {
+       eval {
+               require Sereal::Encoder;
+               require Sereal::Decoder;
+               Sereal::Encoder->import('sereal_encode_with_object');
+               Sereal::Decoder->import('sereal_decode_with_object');
+               ($enc, $dec) = (Sereal::Encoder->new, Sereal::Decoder->new);
+       };
+};
+
+if ($enc && $dec) { # should be custom ops
+       *freeze = sub ($) { sereal_encode_with_object $enc, $_[0] };
+       *thaw = sub ($) { sereal_decode_with_object $dec, $_[0], my $ret };
+} else {
+       eval { # some distros have Storable as a separate package from Perl
+               require Storable;
+               Storable->import(qw(freeze thaw));
+               $enc = 1;
+       } // warn("Storable (part of Perl) missing: $@\n");
+}
 
 sub _get_rec ($) {
        my ($sock) = @_;
@@ -52,11 +75,7 @@ sub ipc_worker_loop ($$) {
 
 sub ipc_worker_spawn ($$$) {
        my ($self, $ident, $oldset) = @_;
-       eval { require Storable; Storable->import(qw(freeze thaw)); };
-       if ($@) {
-               state $w //= warn "Storable (part of Perl) missing: $@\n";
-               return;
-       }
+       return unless $enc;
        my $pid = $self->{-ipc_worker_pid};
        confess "BUG: already spawned PID:$pid" if $pid;
        confess "BUG: already have worker socket" if $self->{-ipc_sock};
diff --git a/t/ipc.t b/t/ipc.t
index f9c4024b46f535b068825d594f5ac96e62e8b7c5..f3715e2c879c84a2bca8753c5ae543715c7a7dc8 100644 (file)
--- a/t/ipc.t
+++ b/t/ipc.t
@@ -49,7 +49,7 @@ my $test = sub {
 $test->('local');
 
 SKIP: {
-       require_mods(qw(Storable), 16);
+       require_mods(qw(Storable||Sereal), 16);
        my $pid = $ipc->ipc_worker_spawn('test worker');
        ok($pid > 0 && kill(0, $pid), 'worker spawned and running');
        defined($pid) or BAIL_OUT 'no spawn, no test';