coreutils
[Top][All Lists]
Advanced

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

Re: 70x speed-up for seq's common cases


From: Jim Meyering
Subject: Re: 70x speed-up for seq's common cases
Date: Fri, 14 Sep 2012 10:40:45 +0200

Bernhard Voelker wrote:
> On 09/14/2012 08:54 AM, Jim Meyering wrote:
>
> Real fast now!
>
>> +static bool
>> +seq_fast (char const *a, char const *b)
>> +{
>
>> [...]
>
>> +      char *buf = xmalloc (N * (n + 1));
>
> Shouldn't we free(buf), too?

Thanks.
Yes, to placate leak-checking tools, but for the record, any time buf
was allocated, we'd exit (successfully) right after return from that
function.

Hence, I've added the free guarded by IF_LINT:

@@ -429,7 +429,7 @@ seq_fast (char const *a, char const *b)
       if (buf < z)
         fwrite (buf, z - buf, 1, stdout);

+      IF_LINT (free (buf));
     }


>> @@ -412,6 +520,33 @@ main (int argc, char **argv)
>
>> [...]
>
>> +  if (format_str == NULL
>> +      && all_digits_p (argv[1])
>> +      && (n_args == 1 || all_digits_p (argv[2]))
>> +      && (n_args < 3 || STREQ ("1", argv[3])))
>> +    {
>> +      char const *s1 = n_args == 1 ? "1" : argv[1];
>> +      char const *s2 = n_args == 1 ? argv[1] : argv[2];
>> +      if (seq_fast (s1, s2))
>> +        exit (EXIT_SUCCESS);
>> +
>> +      /* Upon any failure, let the more general code deal with it.  */
>> +    }
>
> seq_fast can only return ok.

True, but "ok" can be false ;-)
I.e., with out of order arguments:

  seq 9 1

> BTW:
> src/seq.c: In function 'all_digits_p':
> src/seq.c:440:1: error: function might be candidate for attribute
> pure' if it is known to return normally
> [-Werror=suggest-attribute=pure]
>
> Therefore:
>   -static bool
>   +static bool _GL_ATTRIBUTE_PURE
>   all_digits_p (char const *s)

Oh!  Thank you.
Why didn't gcc 4.8.0 20120912 tell me that one?



reply via email to

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