pspp-cvs
[Top][All Lists]
Advanced

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

[Pspp-cvs] pspp/src/data case-tmpfile.c [simpler-proc]


From: Ben Pfaff
Subject: [Pspp-cvs] pspp/src/data case-tmpfile.c [simpler-proc]
Date: Sat, 05 May 2007 21:55:48 +0000

CVSROOT:        /cvsroot/pspp
Module name:    pspp
Branch:         simpler-proc
Changes by:     Ben Pfaff <blp> 07/05/05 21:55:48

Modified files:
        src/data       : case-tmpfile.c 

Log message:
        Calling fseek before every fread or fwrite forces a flush, which kills
        performance.  So does calling ftell, which glibc translates into an
        fseek.  So keep track of our position manually.
        
        This single change speeds up "make check" in this branch from 1m30s to
        1m0s on my system.

CVSWeb URLs:
http://cvs.savannah.gnu.org/viewcvs/pspp/src/data/case-tmpfile.c?cvsroot=pspp&only_with_tag=simpler-proc&r1=1.1.2.3&r2=1.1.2.4

Patches:
Index: case-tmpfile.c
===================================================================
RCS file: /cvsroot/pspp/pspp/src/data/Attic/case-tmpfile.c,v
retrieving revision 1.1.2.3
retrieving revision 1.1.2.4
diff -u -b -r1.1.2.3 -r1.1.2.4
--- case-tmpfile.c      22 Apr 2007 20:40:15 -0000      1.1.2.3
+++ case-tmpfile.c      5 May 2007 21:55:48 -0000       1.1.2.4
@@ -32,10 +32,19 @@
 #include "gettext.h"
 #define _(msgid) gettext (msgid)
 
+#ifndef HAVE_FSEEKO
+#define fseeko fseek
+#endif
+
+#ifndef HAVE_OFF_T
+#define off_t long int
+#endif
+
 struct case_tmpfile 
   {
     FILE *file;
     size_t value_cnt;
+    off_t position;
   };
 
 struct case_tmpfile *
@@ -46,6 +55,7 @@
   if (ctf->file == NULL)
     error (0, errno, _("failed to create temporary file"));
   ctf->value_cnt = value_cnt;
+  ctf->position = 0;
   return ctf;
 }
 
@@ -70,14 +80,6 @@
   return ctf->file == NULL;
 }
 
-#ifndef HAVE_FSEEKO
-#define fseeko fseek
-#endif
-
-#ifndef HAVE_OFF_T
-#define off_t long int
-#endif
-
 static void
 mark_error (const struct case_tmpfile *ctf_) 
 {
@@ -90,33 +92,40 @@
 }
 
 static bool
-do_seek (const struct case_tmpfile *ctf,
+do_seek (const struct case_tmpfile *ctf_,
          casenumber case_idx, size_t value_idx) 
 {
+  struct case_tmpfile *ctf = (struct case_tmpfile *) ctf_;
+
+  if (!case_tmpfile_error (ctf)) 
+    {
   off_t value_ofs = value_idx + (off_t) ctf->value_cnt * case_idx;
   off_t byte_ofs = sizeof (union value) * value_ofs;
-  if (ctf->file != NULL && fseeko (ctf->file, byte_ofs, SEEK_SET) == 0)
-    return true;
 
-  if (!case_tmpfile_error (ctf)) 
+      if (ctf->position == byte_ofs)
+        return true;
+      else if (fseeko (ctf->file, byte_ofs, SEEK_SET) == 0)
+        {
+          ctf->position = byte_ofs;
+          return true;
+        }
+      else
     {
       error (0, errno, _("seeking in temporary file"));
       mark_error (ctf); 
     }
+    }
+
   return false;
 }
 
-bool
-case_tmpfile_get_values (const struct case_tmpfile *ctf,
-                         casenumber case_idx, size_t start_value,
-                         union value values[], size_t value_cnt) 
+static bool
+do_read (const struct case_tmpfile *ctf_, size_t bytes, void *buffer)
 {
-  assert (value_cnt <= ctf->value_cnt);
-  assert (value_cnt + start_value <= ctf->value_cnt);
+  struct case_tmpfile *ctf = (struct case_tmpfile *) ctf_;
 
-  if (!do_seek (ctf, case_idx, start_value))
-    return false;
-  if (fread (values, sizeof (union value) * value_cnt, 1, ctf->file) != 1) 
+  assert (!case_tmpfile_error (ctf));
+  if (fread (buffer, bytes, 1, ctf->file) != 1)
     {
       mark_error (ctf);
       if (ferror (ctf->file))
@@ -127,9 +136,36 @@
         NOT_REACHED ();
       return false;
     } 
+  ctf->position += bytes;
   return true;
 }
 
+static bool
+do_write (struct case_tmpfile *ctf, size_t bytes, const void *buffer) 
+{
+  assert (!case_tmpfile_error (ctf));
+  if (fwrite (buffer, bytes, 1, ctf->file) != 1)
+    {
+      mark_error (ctf); 
+      error (0, errno, _("writing to temporary file"));
+      return false;
+    }
+  ctf->position += bytes;
+  return true;
+}
+
+bool
+case_tmpfile_get_values (const struct case_tmpfile *ctf,
+                         casenumber case_idx, size_t start_value,
+                         union value values[], size_t value_cnt) 
+{
+  assert (value_cnt <= ctf->value_cnt);
+  assert (value_cnt + start_value <= ctf->value_cnt);
+
+  return (do_seek (ctf, case_idx, start_value)
+          && do_read (ctf, sizeof *values * value_cnt, values));
+}
+
 bool
 case_tmpfile_get_case (const struct case_tmpfile *ctf, casenumber case_idx,
                        struct ccase *c) 
@@ -152,15 +188,11 @@
                          const union value values[], size_t value_cnt)
 
 {
-  if (!do_seek (ctf, case_idx, start_value))
-    return false;
-  if (fwrite (values, sizeof (union value) * value_cnt, 1, ctf->file) != 1)
-    {
-      mark_error (ctf); 
-      error (0, errno, _("writing to temporary file"));
-      return false;
-    }
-  return true;
+  assert (value_cnt <= ctf->value_cnt);
+  assert (value_cnt + start_value <= ctf->value_cnt);
+
+  return (do_seek (ctf, case_idx, start_value)
+          && do_write (ctf, sizeof *values * value_cnt, values));
 }                         
 
 bool




reply via email to

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