user/dev discussion of public-inbox itself
 help / color / mirror / code / Atom feed
From: Eric Wong <e@80x24.org>
To: meta@public-inbox.org
Subject: [PATCH 1/8] treewide: favor open(..., '+<&=', $fd)
Date: Fri, 21 May 2021 10:28:25 +0000	[thread overview]
Message-ID: <20210521102832.10784-2-e@80x24.org> (raw)
In-Reply-To: <20210521102832.10784-1-e@80x24.org>

Cut down on unnecessary imports of IO::Handle and
method lookup + dispatch overhead.
---
 examples/unsubscribe.milter | 3 +--
 lib/PublicInbox/DS.pm       | 3 +--
 lib/PublicInbox/Daemon.pm   | 2 +-
 lib/PublicInbox/Sigfd.pm    | 3 +--
 t/epoll.t                   | 7 +++++--
 5 files changed, 9 insertions(+), 9 deletions(-)

diff --git a/examples/unsubscribe.milter b/examples/unsubscribe.milter
index 7b126e30..608524cb 100644
--- a/examples/unsubscribe.milter
+++ b/examples/unsubscribe.milter
@@ -2,7 +2,6 @@
 # Copyright (C) 2016-2021 all contributors <meta@public-inbox.org>
 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
 use strict;
-use warnings;
 use Sendmail::PMilter qw(:all);
 use IO::Socket;
 use Crypt::CBC;
@@ -128,7 +127,7 @@ my $fds = $ENV{LISTEN_FDS};
 if ($fds && (($ENV{LISTEN_PID} || 0) == $$)) {
 	die "$0 can only listen on one FD\n" if $fds != 1;
 	my $start_fd = 3;
-	my $s = IO::Socket->new_from_fd($start_fd, 'r') or
+	open(my $s, '<&=', $start_fd) or
 		die "inherited bad FD from LISTEN_FDS: $!\n";
 	$milter->set_socket($s);
 } else {
diff --git a/lib/PublicInbox/DS.pm b/lib/PublicInbox/DS.pm
index 3cddfd18..7a4dfed0 100644
--- a/lib/PublicInbox/DS.pm
+++ b/lib/PublicInbox/DS.pm
@@ -25,7 +25,6 @@ use v5.10.1;
 use parent qw(Exporter);
 use bytes;
 use POSIX qw(WNOHANG sigprocmask SIG_SETMASK);
-use IO::Handle qw();
 use Fcntl qw(SEEK_SET :DEFAULT O_APPEND);
 use Time::HiRes qw(clock_gettime CLOCK_MONOTONIC);
 use Scalar::Util qw(blessed);
@@ -135,7 +134,7 @@ sub add_timer ($$;@) {
 sub set_cloexec ($) {
     my ($fd) = @_;
 
-    $_io = IO::Handle->new_from_fd($fd, 'r+') or return;
+    open($_io, '+<&=', $fd) or return;
     defined(my $fl = fcntl($_io, F_GETFD, 0)) or return;
     fcntl($_io, F_SETFD, $fl | FD_CLOEXEC);
 }
diff --git a/lib/PublicInbox/Daemon.pm b/lib/PublicInbox/Daemon.pm
index b5f97d81..727311a4 100644
--- a/lib/PublicInbox/Daemon.pm
+++ b/lib/PublicInbox/Daemon.pm
@@ -367,7 +367,7 @@ sub inherit ($) {
 	my $end = $fds + 2; # LISTEN_FDS_START - 1
 	my @rv = ();
 	foreach my $fd (3..$end) {
-		my $s = IO::Handle->new_from_fd($fd, 'r');
+		open(my $s, '<&=', $fd) or warn "fdopen fd=$fd: $!";
 		if (my $k = sockname($s)) {
 			my $prev_was_blocking = $s->blocking(0);
 			warn <<"" if $prev_was_blocking;
diff --git a/lib/PublicInbox/Sigfd.pm b/lib/PublicInbox/Sigfd.pm
index a4d1b3bb..d91ea0e7 100644
--- a/lib/PublicInbox/Sigfd.pm
+++ b/lib/PublicInbox/Sigfd.pm
@@ -8,7 +8,6 @@ use strict;
 use parent qw(PublicInbox::DS);
 use PublicInbox::Syscall qw(signalfd EPOLLIN EPOLLET SFD_NONBLOCK);
 use POSIX ();
-use IO::Handle ();
 
 # returns a coderef to unblock signals if neither signalfd or kqueue
 # are available.
@@ -27,7 +26,7 @@ sub new {
 	my $io;
 	my $fd = signalfd(-1, [keys %signo], $flags);
 	if (defined $fd && $fd >= 0) {
-		$io = IO::Handle->new_from_fd($fd, 'r+');
+		open($io, '+<&=', $fd) or die "open: $!";
 	} elsif (eval { require PublicInbox::DSKQXS }) {
 		$io = PublicInbox::DSKQXS->signalfd([keys %signo], $flags);
 	} else {
diff --git a/t/epoll.t b/t/epoll.t
index f2a68904..f346b387 100644
--- a/t/epoll.t
+++ b/t/epoll.t
@@ -1,11 +1,14 @@
+#!perl -w
+# Copyright (C) 2020-2021 all contributors <meta@public-inbox.org>
+# License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
 use strict;
+use v5.10.1;
 use Test::More;
-use IO::Handle;
 use PublicInbox::Syscall qw(:epoll);
 plan skip_all => 'not Linux' if $^O ne 'linux';
 my $epfd = epoll_create();
 ok($epfd >= 0, 'epoll_create');
-my $hnd = IO::Handle->new_from_fd($epfd, 'r+'); # close on exit
+open(my $hnd, '+<&=', $epfd); # for autoclose
 
 pipe(my ($r, $w)) or die "pipe: $!";
 is(epoll_ctl($epfd, EPOLL_CTL_ADD, fileno($w), EPOLLOUT), 0,

  reply	other threads:[~2021-05-21 10:28 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-05-21 10:28 [PATCH 0/8] lei: export-kw, IMAP import incompatibility Eric Wong
2021-05-21 10:28 ` Eric Wong [this message]
2021-05-21 10:28 ` [PATCH 2/8] lei: drop EOFpipe in favor of PktOp Eric Wong
2021-05-21 10:28 ` [PATCH 3/8] lei tag: support tagging index-only messages Eric Wong
2021-05-21 10:28 ` [PATCH 4/8] lei_input: fix canonicalization of Maildirs for sync Eric Wong
2021-05-21 10:28 ` [PATCH 5/8] lei index: support command-line options Eric Wong
2021-05-21 10:28 ` [PATCH 6/8] lei export-kw: new command to export keywords to Maildirs Eric Wong
2021-05-21 10:28 ` [PATCH 7/8] uri_imap: support uid/auth/user as full accessors Eric Wong
2021-05-21 10:28 ` [PATCH 8/8] lei import: store IMAP user+auth in mail_sync folder URI Eric Wong

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://public-inbox.org/README

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20210521102832.10784-2-e@80x24.org \
    --to=e@80x24.org \
    --cc=meta@public-inbox.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://80x24.org/public-inbox.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).