On 01/10/20 03:29 +0000, sotrdg sotrdg via Libstdc++ wrote:
From fb8d644a4c315058af141a3e84fcc083d665c8b9 Mon Sep 17 00:00:00 2001
From: ejsvifq_mabmip <euloa...@live.com>
Date: Wed, 30 Sep 2020 23:26:47 -0400
Subject: [PATCH] Fix a long term performance issue of fstream on Windows since
MSVCRT defines BUFSIZ as 512 which causes the serious downgrade of I/O
performance.

Even stdio itself is using 4096 as real buffer size, the behavior should be the 
same as FILE* on Windows.

The attached patch seems a cleaner approach. Does it solve your
performance issues?

commit dd71d4081e34f8c95149c561456140ae59ea10ef
Author: Jonathan Wakely <jwak...@redhat.com>
Date:   Fri Oct 2 22:54:50 2020

    libstdc++: Override BUFSIZ for Windows targets [PR 94268]
    
    This replaces uses of BUFSIZ with a new _GLIBCXX_BUFSIZ macro that can
    be overridden in target-specific config headers.
    
    That allows the mingw and mingw-w64 targets to override it, because
    BUFSIZ is apparently defined to 512, resulting in poor performance. The
    MSVCRT stdio apparently uses 4096, so we use that too.
    
    libstdc++-v3/ChangeLog:
    
            PR libstdc++/94268
            * config/os/mingw32-w64/os_defines.h (_GLIBCXX_BUFSIZ):
            Define.
            * config/os/mingw32/os_defines.h (_GLIBCXX_BUFSIZ):
            Define.
            * include/bits/fstream.tcc: Use _GLIBCXX_BUFSIZ instead
            of BUFSIZ.
            * include/ext/stdio_filebuf.h: Likewise.
            * include/std/fstream (_GLIBCXX_BUFSIZ): Define.

diff --git a/libstdc++-v3/config/os/mingw32-w64/os_defines.h b/libstdc++-v3/config/os/mingw32-w64/os_defines.h
index e535f6c2b85..39bdedd19e9 100644
--- a/libstdc++-v3/config/os/mingw32-w64/os_defines.h
+++ b/libstdc++-v3/config/os/mingw32-w64/os_defines.h
@@ -90,4 +90,7 @@
 
 #define _GLIBCXX_USE_CRT_RAND_S 1
 
+// See libstdc++/94268
+#define _GLIBCXX_BUFSIZ 4096
+
 #endif
diff --git a/libstdc++-v3/config/os/mingw32/os_defines.h b/libstdc++-v3/config/os/mingw32/os_defines.h
index 1fee89c49f5..9d2f2bda660 100644
--- a/libstdc++-v3/config/os/mingw32/os_defines.h
+++ b/libstdc++-v3/config/os/mingw32/os_defines.h
@@ -78,4 +78,7 @@
 // See libstdc++/59807
 #define _GTHREAD_USE_MUTEX_INIT_FUNC 1
 
+// See libstdc++/94268
+#define _GLIBCXX_BUFSIZ 4096
+
 #endif
diff --git a/libstdc++-v3/include/bits/fstream.tcc b/libstdc++-v3/include/bits/fstream.tcc
index 81d00c4d318..a4ebbb84fe7 100644
--- a/libstdc++-v3/include/bits/fstream.tcc
+++ b/libstdc++-v3/include/bits/fstream.tcc
@@ -80,7 +80,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
     basic_filebuf<_CharT, _Traits>::
     basic_filebuf() : __streambuf_type(), _M_lock(), _M_file(&_M_lock),
     _M_mode(ios_base::openmode(0)), _M_state_beg(), _M_state_cur(),
-    _M_state_last(), _M_buf(0), _M_buf_size(BUFSIZ),
+    _M_state_last(), _M_buf(0), _M_buf_size(_GLIBCXX_BUFSIZ),
     _M_buf_allocated(false), _M_reading(false), _M_writing(false), _M_pback(), 
     _M_pback_cur_save(0), _M_pback_end_save(0), _M_pback_init(false),
     _M_codecvt(0), _M_ext_buf(0), _M_ext_buf_size(0), _M_ext_next(0),
diff --git a/libstdc++-v3/include/ext/stdio_filebuf.h b/libstdc++-v3/include/ext/stdio_filebuf.h
index fb95bec7350..3b297285ad3 100644
--- a/libstdc++-v3/include/ext/stdio_filebuf.h
+++ b/libstdc++-v3/include/ext/stdio_filebuf.h
@@ -75,7 +75,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
        *  closed when the stdio_filebuf is closed/destroyed.
       */
       stdio_filebuf(int __fd, std::ios_base::openmode __mode,
-		    size_t __size = static_cast<size_t>(BUFSIZ));
+		    size_t __size = static_cast<size_t>(_GLIBCXX_BUFSIZ));
 
       /**
        *  @param  __f  An open @c FILE*.
@@ -88,7 +88,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
        *  stdio_filebuf is closed/destroyed.
       */
       stdio_filebuf(std::__c_file* __f, std::ios_base::openmode __mode,
-		    size_t __size = static_cast<size_t>(BUFSIZ));
+		    size_t __size = static_cast<size_t>(_GLIBCXX_BUFSIZ));
 
       /**
        *  Closes the external data stream if the file descriptor constructor
diff --git a/libstdc++-v3/include/std/fstream b/libstdc++-v3/include/std/fstream
index efc99d1e5a5..c00f9d03895 100644
--- a/libstdc++-v3/include/std/fstream
+++ b/libstdc++-v3/include/std/fstream
@@ -44,6 +44,11 @@
 #include <string>             // For std::string overloads.
 #endif
 
+// This can be override by the target's os_defines.h
+#ifndef _GLIBCXX_BUFSIZ
+# define _GLIBCXX_BUFSIZ BUFSIZ
+#endif
+
 namespace std _GLIBCXX_VISIBILITY(default)
 {
 _GLIBCXX_BEGIN_NAMESPACE_VERSION

Reply via email to