From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on dcvr.yhbt.net X-Spam-Level: X-Spam-Status: No, score=-4.0 required=3.0 tests=ALL_TRUSTED,BAYES_00 shortcircuit=no autolearn=ham autolearn_force=no version=3.4.2 Received: from localhost (dcvr.yhbt.net [127.0.0.1]) by dcvr.yhbt.net (Postfix) with ESMTP id 41E4C1FF9F for ; Thu, 31 Dec 2020 13:51:56 +0000 (UTC) From: Eric Wong To: meta@public-inbox.org Subject: [PATCH 15/36] ipc: support Sereal Date: Thu, 31 Dec 2020 13:51:33 +0000 Message-Id: <20201231135154.6070-16-e@80x24.org> In-Reply-To: <20201231135154.6070-1-e@80x24.org> References: <20201231135154.6070-1-e@80x24.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit List-Id: 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';