Do not waste memory when maxlen is larger than minimum buffer size.
The variable lines_per_buf would always be 1, so avoid writing unnecessary
garbage into buffer file (also less memory usage).

You can test different values for maxlen with this short example program:

=====
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>

#define TIBUFSIZE_MINIMUM (8 * 1024)

int
main(int argc, char *argv[])
{
  long long int maxlen;
  long long int tibufsize;
  long long int lines_per_buf;

  if (argc != 2) {
    fputs("usage: example maxlen\n", stderr);
    return 1;
  }

  maxlen = strtol(argv[1], NULL, 10);

  for (tibufsize = TIBUFSIZE_MINIMUM; tibufsize < maxlen; tibufsize <<= 1)
    /* do nothing */ ;
  lines_per_buf = tibufsize / maxlen;

  printf("maxlen = %lld\ntibufsize = %lld\nlines_per_buf = %lld\n",
      maxlen, tibufsize, lines_per_buf);

  return 0;
}
=====

If we have less than TIBUFSIZE_MINIMUM, we get that and lines_per_buf is
set accordingly:
$ ./example 1
maxlen = 1
tibufsize = 8192
lines_per_buf = 8192

Still true if maxlen is exactly TIBUFSIZE_MINIMUM:
$ ./example 8192
maxlen = 8192
tibufsize = 8192
lines_per_buf = 1

If it's a bit larger, tibufsize gets multiplied by 2.  Therefore, it can
hold the max line only once.  A lot of memory is wasted.
$ ./example 8193
maxlen = 8193
tibufsize = 16384
lines_per_buf = 1

And this will go on and on and on...
$ ./example 16385
maxlen = 16385
tibufsize = 32768
lines_per_buf = 1


Tobias
---
 src/inp.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)

diff --git a/src/inp.c b/src/inp.c
index a7ad070..ba7571a 100644
--- a/src/inp.c
+++ b/src/inp.c
@@ -403,8 +403,7 @@ plan_b (char const *filename)
   if (revision)
     report_revision (found_revision);
   Fseek (ifp, 0, SEEK_SET);            /* rewind file */
-  for (tibufsize = TIBUFSIZE_MINIMUM;  tibufsize < maxlen;  tibufsize <<= 1)
-    /* do nothing */ ;
+  tibufsize = TIBUFSIZE_MINIMUM < maxlen ? maxlen : TIBUFSIZE_MINIMUM;
   lines_per_buf = tibufsize / maxlen;
   tireclen = maxlen;
   tibuf[0] = xmalloc (2 * tibufsize);
-- 
2.1.1


Reply via email to