]> git.ipfire.org Git - thirdparty/coreutils.git/commitdiff
Big performance improvement when sorting many small files,
authorJim Meyering <jim@meyering.net>
Fri, 3 Mar 2000 08:18:48 +0000 (08:18 +0000)
committerJim Meyering <jim@meyering.net>
Fri, 3 Mar 2000 08:18:48 +0000 (08:18 +0000)
building on a suggestion by Charles Randall.

(fillbuf): Skip memmove if it would be a no-op,
as many memmove implementations are slow in that case.
Don't examine leftover bytes for eolchar, since they may be left
over from a previous file, and we want to read from this file.

(sort): At end of file, if there is more input and buffer room,
concatenate the next input file.

src/sort.c

index 1d07419446645c2ee625b2751744f23bf8e2f985..e96f0db195ab7aa1f98ceeaa1c210c45e4798d46 100644 (file)
@@ -547,10 +547,13 @@ fillbuf (struct buffer *buf, FILE *fp)
 {
   int cc;
 
-  memmove (buf->buf, buf->buf + buf->used - buf->left, buf->left);
-  cc = buf->used = buf->left;
+  if (buf->used != buf->left)
+    {
+      memmove (buf->buf, buf->buf + buf->used - buf->left, buf->left);
+      buf->used = buf->left;
+    }
 
-  while (!feof (fp) && !memchr (buf->buf + buf->used - cc, eolchar, cc))
+  while (!feof (fp))
     {
       if (buf->used == buf->alloc)
        {
@@ -565,6 +568,8 @@ fillbuf (struct buffer *buf, FILE *fp)
          exit (SORT_FAILURE);
        }
       buf->used += cc;
+      if (memchr (buf->buf + buf->used - cc, eolchar, cc))
+       break;
     }
 
   if (feof (fp) && buf->used && buf->buf[buf->used - 1] != eolchar)
@@ -1678,6 +1683,15 @@ sort (char **files, int nfiles, FILE *ofp, const char *output_file)
       fp = xfopen (*files++, "r");
       while (fillbuf (&buf, fp))
        {
+         if (nfiles && buf.used != buf.alloc && feof (fp))
+           {
+             /* End of file, but there is more input and buffer room.
+                Concatenate the next input file; this is faster in
+                the usual case.  */
+             buf.left = buf.used;
+             break;
+           }
+
          findlines (&buf, &lines);
          if (ntmp < lines.used)
            {