[PATCH] newlib: fix fseek optimization with SEEK_CUR

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

[PATCH] newlib: fix fseek optimization with SEEK_CUR

Bastien Bouclet
The call to fflush was invalidating the read buffer, preventing relative
seeks to positions that would have been inside the read buffer from
being optimized. The call to srefill would then re-read mostly the same
data that was initially in the read buffer.
---
 newlib/libc/stdio/fseeko.c     | 31 ++++++-------------------------
 newlib/libc/stdio64/fseeko64.c | 31 ++++++-------------------------
 2 files changed, 12 insertions(+), 50 deletions(-)

diff --git a/newlib/libc/stdio/fseeko.c b/newlib/libc/stdio/fseeko.c
index 3e0f9e90b..bbf1af43e 100644
--- a/newlib/libc/stdio/fseeko.c
+++ b/newlib/libc/stdio/fseeko.c
@@ -141,31 +141,12 @@ _fseeko_r (struct _reent *ptr,
   switch (whence)
     {
     case SEEK_CUR:
-      /*
-       * In order to seek relative to the current stream offset,
-       * we have to first find the current stream offset a la
-       * ftell (see ftell for details).
-       */
-      _fflush_r (ptr, fp);   /* may adjust seek offset on append stream */
-      if (fp->_flags & __SOFF)
- curoff = fp->_offset;
-      else
- {
-  curoff = seekfn (ptr, fp->_cookie, (_fpos_t) 0, SEEK_CUR);
-  if (curoff == -1L)
-    {
-      _newlib_flockfile_exit (fp);
-      return EOF;
-    }
- }
-      if (fp->_flags & __SRD)
- {
-  curoff -= fp->_r;
-  if (HASUB (fp))
-    curoff -= fp->_ur;
- }
-      else if (fp->_flags & __SWR && fp->_p != NULL)
- curoff += fp->_p - fp->_bf._base;
+      curoff = _ftello_r(ptr, fp);
+      if (curoff == -1L)
+        {
+          _newlib_flockfile_exit (fp);
+          return EOF;
+        }
 
       offset += curoff;
       whence = SEEK_SET;
diff --git a/newlib/libc/stdio64/fseeko64.c b/newlib/libc/stdio64/fseeko64.c
index 0672086a3..f38005570 100644
--- a/newlib/libc/stdio64/fseeko64.c
+++ b/newlib/libc/stdio64/fseeko64.c
@@ -142,31 +142,12 @@ _fseeko64_r (struct _reent *ptr,
   switch (whence)
     {
     case SEEK_CUR:
-      /*
-       * In order to seek relative to the current stream offset,
-       * we have to first find the current stream offset a la
-       * ftell (see ftell for details).
-       */
-      _fflush_r (ptr, fp);   /* may adjust seek offset on append stream */
-      if (fp->_flags & __SOFF)
- curoff = fp->_offset;
-      else
- {
-  curoff = seekfn (ptr, fp->_cookie, (_fpos64_t) 0, SEEK_CUR);
-  if (curoff == -1L)
-    {
-      _newlib_flockfile_exit(fp);
-      return EOF;
-    }
- }
-      if (fp->_flags & __SRD)
- {
-  curoff -= fp->_r;
-  if (HASUB (fp))
-    curoff -= fp->_ur;
- }
-      else if (fp->_flags & __SWR && fp->_p != NULL)
- curoff += fp->_p - fp->_bf._base;
+      curoff = _ftello64_r(ptr, fp);
+      if (curoff == -1L)
+        {
+          _newlib_flockfile_exit (fp);
+          return EOF;
+        }
 
       offset += curoff;
       whence = SEEK_SET;
--
2.24.0

Reply | Threaded
Open this post in threaded view
|

Re: [PATCH] newlib: fix fseek optimization with SEEK_CUR

Corinna Vinschen
Hi Bastien,

On Nov  9 17:28, Bastien Bouclet wrote:
> The call to fflush was invalidating the read buffer, preventing relative
> seeks to positions that would have been inside the read buffer from
> being optimized. The call to srefill would then re-read mostly the same
> data that was initially in the read buffer.

I checked this against upstream BSD versions.  OpenBSD and NetBSD
operate like our code, including the flush, while FreeBSD uses its
internal ftello and never flushed since the repository import back in
1994.

I'm pretty unsure if we can do this.  Apparently the flush op is only
necessary for streams in append mode.  If at all.

Can we be sure this works as desired on append streams as well?

Also, given that this is changing very basic code, nobody is unaffected.
Any input from other folks?


Thanks,
Corinna

--
Corinna Vinschen
Cygwin Maintainer
Red Hat

signature.asc (849 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: [PATCH] newlib: fix fseek optimization with SEEK_CUR

Bastien Bouclet
In reply to this post by Bastien Bouclet
Hi Corinna,

Thank you for your answer.

> I checked this against upstream BSD versions.  OpenBSD and NetBSD
> operate like our code, including the flush, while FreeBSD uses its
> internal ftello and never flushed since the repository import back in
> 1994.

One difference I've noticed is that fflush does not invalidate the
stream read buffer in the BSD versions of libc.  In newlib this was
introduced in commit a8ef755c2776b8da4ea386360c1df74ce268c165.  Which
is probably why OpenBSD and NetBSD can call fflush in fseek with
SEEK_CUR.

> Can we be sure this works as desired on append streams as well?

Regarding the append streams, it's worth noting there is another call
to fflush at the beginning of fseek in that case.  I've written a small
test program to verify they did not regress in simple cases.

> Also, given that this is changing very basic code, nobody is unaffected.

I would like to see the performance issue fixed one way or another.
The systems I target do not have a page cache, the extra reads have a
noticeable impact on user experience.  Another other option could be
having a compile time option for disabling the code in fflush that
forces a disk access on the next read.

Regards,
Bastien

stdio-append.c (2K) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: [PATCH] newlib: fix fseek optimization with SEEK_CUR

Corinna Vinschen
In reply to this post by Bastien Bouclet
On Nov  9 17:28, Bastien Bouclet wrote:
> The call to fflush was invalidating the read buffer, preventing relative
> seeks to positions that would have been inside the read buffer from
> being optimized. The call to srefill would then re-read mostly the same
> data that was initially in the read buffer.
> ---
>  newlib/libc/stdio/fseeko.c     | 31 ++++++-------------------------
>  newlib/libc/stdio64/fseeko64.c | 31 ++++++-------------------------
>  2 files changed, 12 insertions(+), 50 deletions(-)

Pushed.


Thanks,
Corinna

--
Corinna Vinschen
Cygwin Maintainer
Red Hat

signature.asc (849 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: [PATCH] newlib: fix fseek optimization with SEEK_CUR

Corinna Vinschen-6
On Nov 18 11:11, Corinna Vinschen wrote:

> On Nov  9 17:28, Bastien Bouclet wrote:
> > The call to fflush was invalidating the read buffer, preventing relative
> > seeks to positions that would have been inside the read buffer from
> > being optimized. The call to srefill would then re-read mostly the same
> > data that was initially in the read buffer.
> > ---
> >  newlib/libc/stdio/fseeko.c     | 31 ++++++-------------------------
> >  newlib/libc/stdio64/fseeko64.c | 31 ++++++-------------------------
> >  2 files changed, 12 insertions(+), 50 deletions(-)
>
> Pushed.
Sorry, but I had to revert this patch.  It breaks gnulib's autoconf
test.  The attached conftest.c returns 5, rather than 0 as before
because lseek and ftello return different results.

While this is expected on BSD systems, it's not expected on at least
Linux and Cygwin.  Since this breaks backward compatibility and
leads to gnulib wrongly providing its own fflush, fseek and fseeko
implementations when building for newlib/Cygwin.

I attached the gnulib testcase for completeness.

Many thanks to Takashi Yano for figuring this out after the CYgwin
octave build was broken.


Thanks,
Corinna

--
Corinna Vinschen
Cygwin Maintainer
Red Hat



conftest.c (1K) Download Attachment
conftest.txt (15 bytes) Download Attachment
signature.asc (849 bytes) Download Attachment