Re: [hackers] [sbase][PATCH] Minor optimizations for 'yes'

From: Laslo Hunhold <dev_AT_frign.de>
Date: Fri, 28 Jun 2019 08:57:27 +0200

On Thu, 27 Jun 2019 09:10:28 +0200
Quentin Rameau <quinq_AT_fifth.space> wrote:

Dear Quentin,

> I agree, yes is neither a benchmark tool nor a data generator, it just
> outputs 'y' for piping into other commands.
> Keep it simple.

I agree with you in general, but are we really "optimizing" yes(1) here
for the sake of performance? This looks to me like a case of premature
optimization.

We don't know if a script relies on the behaviour of yes(1) printing
all passed strings repeatedly. So, even though the tool is not
standardized by Posix the common "consensus" is that

  - with no arguments, it shall print y and a newline repeatedly.
  - with arguments, it shall print them, comma separated, followed by
    newline, repeatedly.

The point about readability is a good one. I will even take the blame
for writing it as it is now. However, you could greatly improve
readability without sacrificing features/performance. The line

        argv0 = *argv, argv0 ? (argc--, argv++) : (void *)0;

is used in many places in sbase and is more or less an idiom. The
entire tool itself could then be written as follows. We can even
remove the checks consequently within the loop, which could be regarded
as a "performance" improvement.

        int i;

        argv0 = *argv, argv0 ? (argc--, argv++) : (void *)0;
        
        if (argc == 0) {
                for (;;) {
                        fputs("y\n", stdout);
                }
        } else {
                for (;;) {
                        for (i = 0; i < argc; i++) {
                                fputs(argv[i], stdout);
                                fputc(' ', stdout);
                        }
                        fputc('\n', stdout);
                }
        }

What do you think? Patch is attached.

With best regards

Laslo

-- 
Laslo Hunhold <dev_AT_frign.de>
Received on Fri Jun 28 2019 - 08:57:27 CEST

This archive was generated by hypermail 2.3.0 : Fri Jun 28 2019 - 09:00:25 CEST