changeset 25650:336267b16a3d

Improve fread speed by 2.5X when SKIP parameter used (bug #54100). * oct-stream.cc (stream::read): Calculate eof_pos just once at beginning of function rather than every time a block is skipped.
author Rik <rik@octave.org>
date Fri, 20 Jul 2018 10:25:45 -0700
parents 85c3e315bd43
children b400ad33d92d
files libinterp/corefcn/oct-stream.cc
diffstat 1 files changed, 11 insertions(+), 11 deletions(-) [+]
line wrap: on
line diff
--- a/libinterp/corefcn/oct-stream.cc	Fri Jul 20 09:01:15 2018 -0400
+++ b/libinterp/corefcn/oct-stream.cc	Fri Jul 20 10:25:45 2018 -0700
@@ -6605,6 +6605,16 @@
       {
         std::istream& is = *isp;
 
+        // Initialize eof_pos variable just once per function call
+        off_t eof_pos = 0;
+        if (skip != 0 && is && ! is.eof ())
+          {
+            off_t orig_pos = tell ();
+            seek (0, SEEK_END);
+            eof_pos = tell ();
+            seek (orig_pos, SEEK_SET);
+          }
+
         std::list <void *> input_buf_list;
 
         while (is && ! is.eof ()
@@ -6634,19 +6644,9 @@
 
             if (is && skip != 0 && nel == block_size)
               {
-                // Seek to skip.
+                // Attempt to skip.
                 // If skip would move past EOF, position at EOF.
-
                 off_t orig_pos = tell ();
-
-                seek (0, SEEK_END);
-
-                off_t eof_pos = tell ();
-
-                // Is it possible for this to fail to return us to
-                // the original position?
-                seek (orig_pos, SEEK_SET);
-
                 off_t remaining = eof_pos - orig_pos;
 
                 if (remaining < skip)