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-ASN: 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 E95911F4BD for ; Mon, 24 Jun 2019 02:59:25 +0000 (UTC) From: Eric Wong To: meta@public-inbox.org Subject: [PATCH 57/57] ds: reduce overhead of tempfile creation Date: Mon, 24 Jun 2019 02:52:58 +0000 Message-Id: <20190624025258.25592-58-e@80x24.org> In-Reply-To: <20190624025258.25592-1-e@80x24.org> References: <20190624025258.25592-1-e@80x24.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit List-Id: We end up buffering giant things to the FS sometimes, and open() is not a cheap syscall; so being forced to do it twice to get a file description with O_APPEND is gross when we can just use O_EXCL ourselves and loop on EEXIST. --- lib/PublicInbox/DS.pm | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/lib/PublicInbox/DS.pm b/lib/PublicInbox/DS.pm index e3479e66..4947192f 100644 --- a/lib/PublicInbox/DS.pm +++ b/lib/PublicInbox/DS.pm @@ -18,7 +18,7 @@ use strict; use bytes; use POSIX (); use IO::Handle qw(); -use Fcntl qw(FD_CLOEXEC F_SETFD F_GETFD SEEK_SET); +use Fcntl qw(SEEK_SET :DEFAULT); use Time::HiRes qw(clock_gettime CLOCK_MONOTONIC); use parent qw(Exporter); our @EXPORT_OK = qw(now msg_more); @@ -32,9 +32,9 @@ use fields ('sock', # underlying socket 'wbuf_off', # offset into first element of wbuf to start writing at ); -use Errno qw(EAGAIN EINVAL); +use Errno qw(EAGAIN EINVAL EEXIST); use Carp qw(croak confess carp); -use File::Temp qw(tempfile); +require File::Spec; our ( %DescriptorMap, # fd (num) -> PublicInbox::DS object @@ -440,12 +440,16 @@ sub drop { # PerlIO::mmap or PerlIO::scalar if needed sub tmpio ($$$) { my ($self, $bref, $off) = @_; - # open(my $fh, '+>>', undef) doesn't set O_APPEND - my ($fh, $path) = eval { tempfile('wbuf-XXXXXXX', TMPDIR => 1) }; - $fh or return drop($self, "tempfile: $@"); - open($fh, '+>>', $path) or return drop($self, "open: $!"); + my $fh; # open(my $fh, '+>>', undef) doesn't set O_APPEND + do { + my $fn = File::Spec->tmpdir . '/wbuf-' . rand; + if (sysopen($fh, $fn, O_RDWR|O_CREAT|O_EXCL|O_APPEND, 0600)) { # likely + unlink($fn) or return drop($self, "unlink($fn) $!"); + } elsif ($! != EEXIST) { # EMFILE/ENFILE/ENOSPC/ENOMEM + return drop($self, "open: $!"); + } + } until (defined $fh); $fh->autoflush(1); - unlink($path) or return drop($self, "unlink: $!"); my $len = bytes::length($$bref) - $off; $fh->write($$bref, $len, $off) or return drop($self, "write ($len): $!"); $fh -- EW