[bug#60807] [PATCH 1/2] mtime: use Time::HiRes::stat when available for subsecond resolution

Mike Frysinger <[email protected]> Sun, 15 Jan 2023 03:26:04 -0500
Newsgroups gmane.comp.sysutils.automake.patches
Message-ID <Y8O4nHno6heX+wbQ@vapier>
On 14 Jan 2023 21:27, Jacob Bachmeyer wrote:
> Mike Frysinger wrote:
> > --- a/lib/Automake/FileUtils.pm
> > +++ b/lib/Automake/FileUtils.pm
> > @@ -42,6 +42,11 @@ use Exporter;
> >  use File::stat;
> >  use IO::File;
> >  
> > +# Perl's builtin stat does not provide sub-second resolution.  Use Time::HiRes
> > +# if it's available instead.  Hopefully one day perl will update.
> > +# https://github.com/Perl/perl5/issues/17900
> > +my $have_time_hires = eval { require Time::HiRes; };
> > +
> >  use Automake::Channels;
> >  use Automake::ChannelDefs;
> >  
> > @@ -115,10 +120,18 @@ sub mtime ($)
> >    return 0
> >      if $file eq '-' || ! -f $file;
> >  
> > -  my $stat = stat ($file)
> > -    or fatal "cannot stat $file: $!";
> > -
> > -  return $stat->mtime;
> > +  if ($have_time_hires)
> > +    {
> > +      my @stat = Time::HiRes::stat ($file)
> > +	or fatal "cannot stat $file: $!";
> > +      return $stat[9];
> > +    }
> > +  else
> > +    {
> > +      my $stat = stat ($file)
> > +	or fatal "cannot stat $file: $!";
> > +      return $stat->mtime;
> > +    }
> >  }
> 
> If you change that variable to a constant, you can eliminate the runtime 
> overhead entirely, since Perl optimizes if(1) and if(0) and folds 
> constants at compile time.
> 
> Something like:
> 
>     use constant HAVE_Time_HiRes => eval { require Time::HiRes; };
> 
> Then:
> 
>     if (HAVE_Time_HiRes)
>        ...
> 
> If you do this, Perl will inline the block actually used and elide the 
> branch at runtime.  This is generally useful for any test that can only 
> go one way in a specific run of the program.

thanks, i'll integrate that idea.  i'm by no means a perl programmer.
-mike
signature.asc (application/pgp-signature, 833 B)
-----BEGIN PGP SIGNATURE-----

iQIzBAABCAAdFiEEuQK1JxMl+JKsJRrUQWM7n+g39YEFAmPDuJwACgkQQWM7n+g3
9YHLRw//fQkZIBqRMl+j1WA/mrhoGVD0JvNNvwayZqMEc37jZ7Tw6JiI7RpcLUyj
GY7GV0RONQ43gpeQpNXfry85JNdW65bJNdeU+mTQ7+p63ZJRYSdReRywIwxacIX3
cMp/YB7C5+vEkTzL2FEB4QXwXVHTfXzJBQI7HrEFoywtDKbfv+SfKzq8kpmvvhge
0olBkD/dNSR0cNxrLwCa0BRp3UfDf3rFkQKJicpPzOMBtLuu27oVCpQQmDs2RGja
Q4jSpz7SOa9Kkm8tI5Mapz4/qjXS+kBSagLZsdxeUnQcRqU9+jpYwNMmyZc85A+P
9c0tejbpFsOCiyXNWXOJboHggopJdUKUccUIM00t8W2dSvSO28DTieq4OUJ5bVmo
sEl6abs85IEtyISyPnDPlkDq6f3wJXXXKYjHIMWIRpDQOpekMb/vD2kaNhi4RYXM
+xrBSo36AE2tr3t2gPJuxHxUrhxXT3rJhzK54B692DGZHrNn+oFXot4Pm/DHRtZ1
SJFbmfDRH+b2+UlFVuFQT5MeXOxgffkVHMVwalRClyks68aeYUg3tDHgP31IVxsO
tmQM+sHR521HuR/Owt4ViNbBCTrcDd8S1VboWFbB1499gkJNg/aQ6jeFRma9/YWM
7nFPncAQ3ToWJEAQopGKxtGY5+EnSKt/7W1vyfbU0QPXWymNjWw=
=jKF7
-----END PGP SIGNATURE-----