Re: [PATCH] Replace 'test "${var+set}" = set' with 'test -n "${var+set}"'
Eric Blake <[email protected]>
| Newsgroups | gmane.comp.sysutils.autoconf.patches |
|---|---|
| Organization | Red Hat, Inc. |
| Message-ID | <[email protected]> |
On 04/08/2015 02:01 PM, Bernhard Reutner-Fischer wrote:
> * lib/autoconf/general.m4 (_AC_ENABLE_IF_ACTION): Use
> 'test -n "${var+set}"' instead of 'test "${var+set}" = set.
23 bytes: ^^^^^^^^^^^^^^^^^^^^^^^
20 bytes:^^^^^^^^^^^^^^^^^^^^
Both could be trimmed by shortening our replacement string:
test "${var+y}" = y
test -n "${var+y}"
so we are now down to 18 bytes.
And in modern shell, 'test -n ...' is strictly equivalent to 'test ...'
(not so in ancient shell, but those are getting fewer and farther
between) - if we did appropriate pre-filtering tests, we could get away
with 15 bytes:
test "${var+y}"
On 04/08/2015 03:11 PM, Paul Eggert wrote:
> Isn't '${var+:} false' faster yet?
^^^^^^^^^^^^^^ 14 bytes
Faster, but has the slight problem of having $? at 255 in some older
shells (hello Solaris /bin/sh), so you then have to check context to see
if the caller cared about a difference in exit status. On the other
hand, we already require a shell with function support in many cases, so
many of our use cases can assume we've already filtered out Solaris
/bin/sh false behavior.
I can beat it in an amortized way, by doing:
f(){ false;}
${var+:} f
but only after I have at least 4 tests at 10 bytes each making up for
the 12 bytes spent on the function definition (a longer function body of
'return 1;' instead of 'false;' guarantees no $? issue, but I already
argued that no shell with functions has a false that returns other than
1). And while we are likely to have 4 or more instances replaced, my
hack violates our shell function naming conventions (once we put it in
the right as_fn_ namespace, direct use of false wins every time), not to
mention legibility (hiding things in a function has its uses, but this
does not seem to be one of them).
I doubt that parsing speed is a noticeable issue, but file size
reduction is real; and smaller files lead to less memory consumption, so
it is definitely worth doing something along these lines. So, unless
someone can beat Paul at portable shell golf, I like his 14-byte
approach at the moment, if properly audited to make sure we aren't going
to rely on $? during the time where we are probing for shell features in
order to re-exec away from Solaris /bin/sh.
--
Eric Blake eblake redhat com +1-919-301-3266
Libvirt virtualization library http://libvirt.org
signature.asc
(application/pgp-signature, 604 B)
-----BEGIN PGP SIGNATURE----- Version: GnuPG v2 Comment: Public key at http://people.redhat.com/eblake/eblake.gpg Comment: Using GnuPG with Thunderbird - http://www.enigmail.net/ iQEcBAEBCAAGBQJVJarnAAoJEKeha0olJ0NqmOoH/13sgfuDM2WKuBSHaiwfrBNT 8Z8j+/i6+tbRkeKqRa7e01Lb2U7qZ1rV9yrot1/YARqLqudxmm7c1k/iYdAS8YOh NP3WmedfvdCFSJcVRHULE15BRyORVy6SJkhBJstF54WGMSsLz1/YU6Csapq3Qiir UU8Y4FCzCXU49QVa/bwz4zyGalDCRwpMB5ATMRrPpLO2lnchfhv1SPHTorkBflFf EIIDVYD0kx2UkWZMegrara3KEgM3kD+cAuGWaZ1Pw+yfnkvK40Wk8L5L7SEdi2D7 /4iDMocI0D14wIwNCT3JkwK3rQ6K6552CvtR6Dw21OVXsFXDt/2peJNnTRpM1hw= =KrF+ -----END PGP SIGNATURE-----