This is the mail archive of the
gcc-patches@gcc.gnu.org
mailing list for the GCC project.
Minimize <iosfwd>
- From: "Zack Weinberg" <zackw at panix dot com>
- To: "GCC Patches" <gcc-patches at gcc dot gnu dot org>, libstdc++ at gcc dot gnu dot org
- Date: Mon, 9 Apr 2007 13:35:47 -0700
- Subject: Minimize <iosfwd>
The attached patch cuts <iosfwd> down to the absolute minumum - it
declares nothing but what the standard says it declares plus a couple
things that cannot be avoided. (A few other headers may also be
smaller.) On a real application, preprocessed text shrinks by 50 to
300 lines per source file (this app has a lot of small source files,
so the total reduction is 47,000 lines!) I also see the .s files
shrink, but I'm not sure I have a sound baseline for comparison there.
This patch may not be good as is; I'm making conditional use of a
feature of glibc whereby one may instruct <wchar.h> to declare
mbstate_t (actually __mbstate_t) and nothing else, and I'm not sure
this interacts correctly with the various hacks for getting the C
headers into namespace std. I'd like to preserve this feature as I
suspect it's responsible for a lot of the real-world gain from the
change, but it may need modification.
I believe this change to have no ABI consequences, but there is a lot
of noise in my abi_check output (for some reason libstdc++ refuses to
use --enable-c-locale=gnu on my test system) so I am not certain.
It should be mentioned that I think it is possible, even after Paolo
Carlini's substantial improvements last month, to do similar things to
almost all the standard C++ headers. I may do more as I have time and
motivation.
zw
* include/bits/fposfwd.h: New file.
* include/Makefile.am (bits_headers): Add fposfwd.h.
* include/Makefile.in: Regenerate (by hand) (automake must die)
* include/bits/localefwd.h: Use _GLIBCXX_MBSTATE_T.
* include/bits/postypes.h: Include bits/fposfwd.h instead of
<cwchar>. Do not declare
fpos, streampos, or wstreampos here.
* include/std/iosfwd: Do not include bits/c++locale.h,
bits/stringfwd.h, bits/postypes.h,
or bits/functexcept.h. Do include bits/fposfwd.h. Declare
allocator, char_traits, and
the standard char_traits specializations by hand.
==================================================================
--- include/Makefile.am (revision 123704)
+++ include/Makefile.am (local)
@@ -80,6 +80,7 @@ bits_headers = \
${bits_srcdir}/concept_check.h \
${bits_srcdir}/cpp_type_traits.h \
${bits_srcdir}/deque.tcc \
+ ${bits_srcdir}/fposfwd.h \
${bits_srcdir}/fstream.tcc \
${bits_srcdir}/functexcept.h \
${bits_srcdir}/gslice.h \
==================================================================
--- include/Makefile.in (revision 123704)
+++ include/Makefile.in (local)
@@ -314,6 +314,7 @@ bits_headers = \
${bits_srcdir}/concept_check.h \
${bits_srcdir}/cpp_type_traits.h \
${bits_srcdir}/deque.tcc \
+ ${bits_srcdir}/fposfwd.h \
${bits_srcdir}/fstream.tcc \
${bits_srcdir}/functexcept.h \
${bits_srcdir}/gslice.h \
==================================================================
--- include/bits/localefwd.h (revision 123704)
+++ include/bits/localefwd.h (local)
@@ -121,9 +121,9 @@ _GLIBCXX_BEGIN_NAMESPACE(std)
class codecvt_base;
template<typename _InternT, typename _ExternT, typename _StateT>
class codecvt;
- template<> class codecvt<char, char, mbstate_t>;
+ template<> class codecvt<char, char, _GLIBCXX_MBSTATE_T>;
#ifdef _GLIBCXX_USE_WCHAR_T
- template<> class codecvt<wchar_t, char, mbstate_t>;
+ template<> class codecvt<wchar_t, char, _GLIBCXX_MBSTATE_T>;
#endif
template<typename _InternT, typename _ExternT, typename _StateT>
class codecvt_byname;
==================================================================
--- include/bits/postypes.h (revision 123704)
+++ include/bits/postypes.h (local)
@@ -43,7 +43,7 @@
#pragma GCC system_header
-#include <cwchar> // For mbstate_t
+#include <bits/fposfwd.h>
#ifdef _GLIBCXX_HAVE_STDINT_H
#include <stdint.h> // For int64_t
@@ -77,9 +77,6 @@ _GLIBCXX_BEGIN_NAMESPACE(std)
/// Integral type for I/O operation counts and buffer sizes.
typedef ptrdiff_t streamsize; // Signed integral type
- template<typename _StateT>
- class fpos;
-
/**
* @brief Class representing stream positions.
*
@@ -204,14 +201,6 @@ _GLIBCXX_BEGIN_NAMESPACE(std)
operator!=(const fpos<_StateT>& __lhs, const fpos<_StateT>& __rhs)
{ return streamoff(__lhs) != streamoff(__rhs); }
- // Clauses 21.1.3.1 and 21.1.3.2 describe streampos and wstreampos
- // as implementation defined types, but clause 27.2 requires that
- // they must both be typedefs for fpos<mbstate_t>
- /// File position for char streams.
- typedef fpos<mbstate_t> streampos;
- /// File position for wchar_t streams.
- typedef fpos<mbstate_t> wstreampos;
-
_GLIBCXX_END_NAMESPACE
#endif
==================================================================
--- include/bits/fposfwd.h (revision 123704)
+++ include/bits/fposfwd.h (local)
@@ -0,0 +1,86 @@
+// Position types -*- C++ -*-
+
+// Copyright (C) 2007
+// Free Software Foundation, Inc.
+//
+// This file is part of the GNU ISO C++ Library. This library is free
+// software; you can redistribute it and/or modify it under the
+// terms of the GNU General Public License as published by the
+// Free Software Foundation; either version 2, or (at your option)
+// any later version.
+
+// This library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU General Public License for more details.
+
+// You should have received a copy of the GNU General Public License along
+// with this library; see the file COPYING. If not, write to the Free
+// Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
+// USA.
+
+// As a special exception, you may use this file as part of a free software
+// library without restriction. Specifically, if other files instantiate
+// templates or use macros or inline functions from this file, or you compile
+// this file and link it with other files to produce an executable, this
+// file does not by itself cause the resulting executable to be covered by
+// the GNU General Public License. This exception does not however
+// invalidate any other reasons why the executable file might be covered by
+// the GNU General Public License.
+
+/** @file fposfwd.h
+ * This is an internal header file, included by other library headers.
+ * You should not attempt to use it directly.
+ */
+
+//
+// ISO C++ 14882: 27.4.3 - Template class fpos
+//
+
+#ifndef _GLIBCXX_FPOSFWD_H
+#define _GLIBCXX_FPOSFWD_H 1
+
+#pragma GCC system_header
+
+#include <bits/c++config.h>
+
+// We may be able to use a special feature of glibc's <wchar.h> to
+// get a definition of mbstate_t and nothing else. If used, it exposes
+// __mbstate_t rather than mbstate_t (the latter is a typedef to the
+// former when the full header is included, so we have no linkage issues).
+// This header provides the macro _GLIBCXX_MBSTATE_T which expands to
+// mbstate_t or __mbstate_t as appropriate.
+//
+// N.B. The special feature does not work if used via <cwchar>, nor does
+// it work if we attempt to force the <wchar.h> definitions into a
+// namespace.
+
+#if __GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ > 1)
+
+ #define __need_mbstate_t
+ #include <wchar.h>
+ #define _GLIBCXX_MBSTATE_T ::__mbstate_t
+
+#else
+
+ #include <cwchar>
+ #define _GLIBCXX_MBSTATE_T mbstate_t
+
+#endif
+
+_GLIBCXX_BEGIN_NAMESPACE(std)
+
+ template<typename _StateT>
+ class fpos;
+
+ // Clauses 21.1.3.1 and 21.1.3.2 describe streampos and wstreampos
+ // as implementation defined types, but clause 27.2 requires that
+ // they must both be typedefs for fpos<mbstate_t>
+ /// File position for char streams.
+ typedef fpos<_GLIBCXX_MBSTATE_T> streampos;
+ /// File position for wchar_t streams.
+ typedef fpos<_GLIBCXX_MBSTATE_T> wstreampos;
+
+_GLIBCXX_END_NAMESPACE
+
+#endif
Property changes on: include/bits/fposfwd.h
___________________________________________________________________
Name: svn:mime-type
+text/cpp
==================================================================
--- include/std/iosfwd (revision 123704)
+++ include/std/iosfwd (local)
@@ -43,13 +43,23 @@
#pragma GCC system_header
#include <bits/c++config.h>
-#include <bits/c++locale.h>
-#include <bits/stringfwd.h> // For string forward declarations.
-#include <bits/postypes.h>
-#include <bits/functexcept.h>
+#include <bits/fposfwd.h>
_GLIBCXX_BEGIN_NAMESPACE(std)
+ // allocator, char_traits, and the two specializations of char_traits
+ // are also declared in <bits/stringfwd.h>.
+ template<typename _Alloc>
+ class allocator;
+
+ template<class _CharT>
+ struct char_traits;
+
+ template<> struct char_traits<char>;
+#ifdef _GLIBCXX_USE_WCHAR_T
+ template<> struct char_traits<wchar_t>;
+#endif
+
template<typename _CharT, typename _Traits = char_traits<_CharT> >
class basic_ios;