On 24/03/15 07:34, Bernhard Voelker wrote:
> On 03/23/2015 01:03 PM, Pádraig Brady wrote:
>> It just occurred to me that we could simplify from 3 to 2 loops,
>> while also making the code more adaptive to the input,
>> by simply determining the average line length per block.
>
>
> great idea!
>
>> I'll push the attached later.
>
> just 2 minor, almost readability notes:
>
> As you introduced the 'end' variable, why not using it
> in the memchr() case, too?
>
> @@ -290,7 +290,7 @@ wc (int fd, char const *file_x, struct fstatus *fstatus,
> off_t current_pos)
> else
> {
> /* memchr is more efficient with longer lines. */
> - while ((p = memchr (p, '\n', (buf + bytes_read) - p)))
> + while ((p = memchr (p, '\n', end - p)))
Nice one.
> {
> ++p;
> ++lines;
>
> I'd favor multiplication by 15 on the other side, thus avoiding
> to have to check for DIV-By-ZERO manually:
>
> @@ -303,7 +303,7 @@ wc (int fd, char const *file_x, struct fstatus *fstatus,
> off_t current_pos)
> FIXME: This line length was determined in 2015, on both
> x86_64 and ppc64, but it's worth re-evaluating in future with
> newer compilers, CPUs, or memchr() implementations etc. */
> - if (lines == plines || (bytes_read / (lines - plines) > 15))
> + if ((lines - plines) * 15 <= bytes_read)
Better thanks. Actually I'll divide both sides by 15 to
avoid any hint of overflow issues.
thanks!
Pádraig.