Hi Paul, Paul Eggert <egg...@cs.ucla.edu> writes:
> Surely this would be much, much slower if the file seeks slowly, > for example, if it is a tape drive. > > It might be helpful to use fstat to find the file's type and size, > and to subtract the current file offset from its size > (for file types where st_size is applicable). thanks for the review, I have adjusted the patch accordingly. Cheers, Giuseppe >From e1276fa005fee48e036132ebbd9b69bdef297249 Mon Sep 17 00:00:00 2001 From: Giuseppe Scrivano <gscriv...@gnu.org> Date: Tue, 3 Aug 2010 15:40:19 +0200 Subject: [PATCH] read-file: Avoid memory reallocations with regular files. * lib/read-file.c: Include <sys/types.h>, <sys/stat.h>, <unistd.h>. (fread_file): With regular files, use the remaining length as the initial buffer size. --- ChangeLog | 7 +++++++ lib/read-file.c | 19 +++++++++++++++++++ 2 files changed, 26 insertions(+), 0 deletions(-) diff --git a/ChangeLog b/ChangeLog index 4d24a34..a178f88 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,10 @@ +2010-08-03 Giuseppe Scrivano <gscriv...@gnu.org> + + read-file: Avoid memory reallocations with regular files. + * lib/read-file.c: Include <sys/types.h>, <sys/stat.h>, <unistd.h>. + (fread_file): With regular files, use the remaining length as the + initial buffer size. + 2010-08-01 Bruno Haible <br...@clisp.org> Integrate the regex documentation. diff --git a/lib/read-file.c b/lib/read-file.c index 6b655db..cf8af6b 100644 --- a/lib/read-file.c +++ b/lib/read-file.c @@ -18,6 +18,11 @@ #include <config.h> +/* Get fstat. */ +#include <sys/types.h> +#include <sys/stat.h> +#include <unistd.h> + #include "read-file.h" /* Get realloc, free. */ @@ -38,6 +43,20 @@ fread_file (FILE * stream, size_t * length) size_t alloc = 0; size_t size = 0; int save_errno; + struct stat st; + + if (fstat (fileno (stream), &st) == 0 && S_ISREG (st.st_mode)) + { + long pos = ftell (stream); + if (pos < 0) + return NULL; + + alloc = st.st_size - pos + 1; + + buf = malloc (alloc); + if (!buf) + return NULL; + } for (;;) { -- 1.7.1