autoconf-patches
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [PATCH] Replace 'test "${var+set}" = set' with 'test -n "${var+set}"


From: Eric Blake
Subject: Re: [PATCH] Replace 'test "${var+set}" = set' with 'test -n "${var+set}"'
Date: Wed, 08 Apr 2015 16:25:43 -0600
User-agent: Mozilla/5.0 (X11; Linux x86_64; rv:31.0) Gecko/20100101 Thunderbird/31.6.0

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

Attachment: signature.asc
Description: OpenPGP digital signature


reply via email to

[Prev in Thread] Current Thread [Next in Thread]