git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Stefan Beller <sbeller@google.com>
To: Mikael Magnusson <mikachu@gmail.com>, Andrey Rybak <rybak.a.v@gmail.com>
Cc: git <git@vger.kernel.org>, Jeff King <peff@peff.net>
Subject: Re: [PATCH] Optimize usage of grep by passing -q
Date: Mon, 16 Nov 2015 17:04:24 -0800	[thread overview]
Message-ID: <CAGZ79kYCNdDV2cDZSiugx6EuY_0BEgGMJJVm8EBWqPEE77sqbw@mail.gmail.com> (raw)
In-Reply-To: <CAHYJk3S-pBQGDk_PPXw5mgzuXhdKqifRB4Vwz1Nu0TecXz4JQg@mail.gmail.com>

+cc Andrey Rybak, who I credit for finding the reasoning below (he
sent to me privately,
without cc'ing the list)

On Mon, Nov 16, 2015 at 4:59 PM, Mikael Magnusson <mikachu@gmail.com> wrote:
> On Mon, Nov 16, 2015 at 10:43 PM, Stefan Beller <sbeller@google.com> wrote:
>> Instead of redirecting all grep output to /dev/null, we can just
>> pass in -q instead. This preserves the exit code behavior, but is faster.
>> As grep returns true if it finds at least one match, grep can exit promptly
>> after finding the first line and doesn't need to find more occurrences
>> which would be redirected to /dev/null anyways.
>>
>> This is true for the gnu version of grep. I am not sure if all
>> versions of grep support this optimization. In case it is not,
>> we'd revert this patch.
>
> POSIX specifies -q, so you should be fine.
> http://pubs.opengroup.org/onlinepubs/9699919799/utilities/grep.html
>

>From http://www.gnu.org/software/grep/manual/grep.html :
-q
--quiet
--silent
Quiet; do not write anything to standard output. Exit immediately with
zero status if any match is found, even if an error was detected. Also
see the -s or --no-messages option. (-q is specified by POSIX.)
-s
--no-messages
Suppress error messages about nonexistent or unreadable files.
Portability note: unlike GNU grep, 7th Edition Unix grep did not
conform to POSIX, because it lacked -q and its -s option behaved like
GNU grep's -q option.1
USG-style grep also lacked -q but its -s option behaved like GNU
grep's. Portable shell scripts should avoid both -q and -s and should
redirect standard and error output to /dev/null instead. (-s is
specified by POSIX.)

Reading that in full, I think my patch is a bad idea.

  reply	other threads:[~2015-11-17  1:04 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-11-16 21:43 [PATCH] Optimize usage of grep by passing -q Stefan Beller
2015-11-17  0:59 ` Mikael Magnusson
2015-11-17  1:04   ` Stefan Beller [this message]
2015-11-17 22:37     ` Jeff King

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

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

  git send-email \
    --in-reply-to=CAGZ79kYCNdDV2cDZSiugx6EuY_0BEgGMJJVm8EBWqPEE77sqbw@mail.gmail.com \
    --to=sbeller@google.com \
    --cc=git@vger.kernel.org \
    --cc=mikachu@gmail.com \
    --cc=peff@peff.net \
    --cc=rybak.a.v@gmail.com \
    /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.
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 NNTP newsgroup(s).