Optimization: no need to flush stdin if we can determine quickly that stdin's
authorBruno Haible <bruno@clisp.org>
Mon, 20 Aug 2007 22:09:08 +0000 (22:09 +0000)
committerBruno Haible <bruno@clisp.org>
Mon, 20 Aug 2007 22:09:08 +0000 (22:09 +0000)
input buffer is empty.

ChangeLog
lib/closein.c
modules/closein

index 8a81c4a932d4fee0b45180eb21b7e57ac6e3c332..9fd9405b32b9c8ab2d5c3cc71f6a67b3152f3e80 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,10 @@
+2007-08-19  Bruno Haible  <bruno@clisp.org>
+
+       * modules/closein (Depends-on): Add freadahead.
+       * lib/closein.c: Include freadahead.h.
+       (close_stdin): Skip the fseeko and fflush calls if freadahead(stdin)
+       is zero.
+
 2007-08-19  Bruno Haible  <bruno@clisp.org>
 
        * modules/freadahead-tests: New file.
index 4450d5bd8dd6be0a2fdd290942ad2773a916b001..bca7711baed8eac446c9ed7c9061a14e0f87368e 100644 (file)
@@ -32,6 +32,7 @@
 #include "closeout.h"
 #include "error.h"
 #include "exitfail.h"
+#include "freadahead.h"
 #include "quotearg.h"
 
 static const char *file_name;
@@ -80,10 +81,16 @@ close_stdin (void)
 {
   bool fail = false;
 
-  /* Only attempt flush if stdin is seekable, as fflush is entitled to
-     fail on non-seekable streams.  */
-  if (fseeko (stdin, 0, SEEK_CUR) == 0 && fflush (stdin) != 0)
-    fail = true;
+  /* There is no need to flush stdin if we can determine quickly that stdin's
+     input buffer is empty; in this case we know that if stdin is seekable,
+     fseeko (stdin, 0, SEEK_CUR) == lseek (0, 0, SEEK_CUR).  */
+  if (freadahead (stdin) > 0)
+    {
+      /* Only attempt flush if stdin is seekable, as fflush is entitled to
+        fail on non-seekable streams.  */
+      if (fseeko (stdin, 0, SEEK_CUR) == 0 && fflush (stdin) != 0)
+       fail = true;
+    }
   if (close_stream (stdin) != 0)
     fail = true;
   if (fail)
index fefeae3648d951cd94dcde564113226f57e47ad7..522a383d3eef7ec35994bbb4e819857b44515db5 100644 (file)
@@ -8,6 +8,7 @@ m4/closein.m4
 
 Depends-on:
 closeout
+freadahead
 fflush
 stdbool