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 | 29 ++++++++++++++++++++++++-----
 t/ipc.t                |  2 +-
 2 files changed, 25 insertions(+), 6 deletions(-)

diff --git a/lib/PublicInbox/IPC.pm b/lib/PublicInbox/IPC.pm
index c04140ae..0baa218c 100644
--- a/lib/PublicInbox/IPC.pm
+++ b/lib/PublicInbox/IPC.pm
@@ -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 f9c4024b..f3715e2c 100644
--- 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';
--
unsubscribe: one-click, see List-Unsubscribe header
archive: https://public-inbox.org/meta/

Reply via email to