bug-gnulib
[Top][All Lists]
Advanced

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

Re: [PATCH] read-file: Avoid memory reallocations with seekable files.


From: Giuseppe Scrivano
Subject: Re: [PATCH] read-file: Avoid memory reallocations with seekable files.
Date: Tue, 03 Aug 2010 22:05:56 +0200
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/23.1 (gnu/linux)

Hi Paul,

Paul Eggert <address@hidden> 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 <address@hidden>
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  <address@hidden>
+
+       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  <address@hidden>
 
        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



reply via email to

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