This is the mail archive of the libstdc++@gcc.gnu.org mailing list for the libstdc++ project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: Dead code (was: Re: Good numbers from Ritter's new string allocator)


Hi all,

I'm leaving town for a couple of days and these are my latest news:

1- I have verified that indeed on my Linux-x86 machine by using the safe value of
__malloc_header_size suggested by Ulrich (Drepper), i.e., 4*sizeof(void*), instead of the
optimal value of 8, essentially indistinguishable running times are obtained (difference <
1%). So, IMHO, at this point we can go for an hardwired 4*sizeof(void*).

2- The other paramter is the pagesize value, __pagesize. In this case, I think that
finetuning it to the actual pagesize value of the platform would bring bigger benefits. On
the other hand, Loren's scheme should greatly improve the performance of basic_string also
if the real pagesize is a small multiple or submultiple of 4096. Do you agree?

Honestly, I would be *very* glad to work out the configury machinery needed to fine tune
this parameter, but I definitely need some help from you in this field, quite new for me.
Which are the most common values of pagesize? We are talking about TARGET values, not HOST
values, right?, so, how to autoconf for them?

3- As a final point, if it is viable to actually get rid basic_string::_Rep of all that
cruft (including the misleading comments), there is one more related hunk which can go
away, i.e., a comment in c++.config.

I'm attaching here the updated patch.

Cheers,
Paolo.



diff -urN gcc-vanilla/libstdc++-v3/include/bits/basic_string.h
gcc/libstdc++-v3/include/bits/basic_string.h
--- gcc-vanilla/libstdc++-v3/include/bits/basic_string.h Fri Nov  2 18:38:10 2001
+++ gcc/libstdc++-v3/include/bits/basic_string.h Thu Nov 22 22:42:52 2001
@@ -197,21 +197,6 @@
  _CharT*
  _M_clone(const _Alloc&, size_type __res = 0);

-#if _GLIBCPP_ALLOC_CONTROL
- // These function pointers allow you to modify the allocation
- // policy used by the string classes.  By default they expand by
- // powers of two, but this may be excessive for space-critical
- // applications.
-
- // Returns true if ALLOCATED is too much larger than LENGTH
- static bool (*_S_excess_slop) (size_t __length, size_t __allocated);
-
- inline static bool
- __default_excess(size_t, size_t);
-#else
- inline static bool
- _S_excess_slop(size_t, size_t);
-#endif
       };

       // Use empty-base optimization: http://www.cantrip.org/emptyopt.html
diff -urN gcc-vanilla/libstdc++-v3/include/bits/basic_string.tcc
gcc/libstdc++-v3/include/bits/basic_string.tcc
--- gcc-vanilla/libstdc++-v3/include/bits/basic_string.tcc Thu Nov 22 00:54:51 2001
+++ gcc/libstdc++-v3/include/bits/basic_string.tcc Thu Nov 22 23:09:16 2001
@@ -349,13 +349,6 @@
  }
     }

-#ifdef _GLIBCPP_ALLOC_CONTROL
-  template<typename _CharT, typename _Traits, typename _Alloc>
-    bool (*basic_string<_CharT, _Traits, _Alloc>::_Rep::_S_excess_slop)
-    (size_t, size_t) =
-    basic_string<_CharT, _Traits, _Alloc>::_Rep::_S_default_excess;
-#endif
-
   template<typename _CharT, typename _Traits, typename _Alloc>
     typename basic_string<_CharT, _Traits, _Alloc>::_Rep*
     basic_string<_CharT, _Traits, _Alloc>::_Rep::
@@ -374,6 +367,37 @@
       // terminating null char_type() element, plus enough for the
       // _Rep data structure. Whew. Seemingly so needy, yet so elemental.
       size_t __size = (__capacity + 1) * sizeof(_CharT) + sizeof(_Rep);
+
+      // The standard places no restriction on allocating more memory
+      // than is strictly needed within this layer at the moment or
+      // requested by an explicit application call to reserve().  Many
+      // malloc implementations perform quite poorly when an
+      // application attempts to allocate memory in a stepwise fashion
+      // growing each allocation size by only 1 char.  Additionally,
+      // it makes little sense to allocate less linear memory than the
+      // natural resolution size of a malloc implementation.
+
+      // This version assumes the malloc implementation prefers to
+      // align allocations over the size of a page to a page boundary
+      // and under the size of a page to a power of 2.
+      const size_t __pagesize = 4096; // This magic constant, from OS.
+      const size_t __malloc_header_size = 4*sizeof(void*);
+      if ((__size + __malloc_header_size) > __pagesize)
+      {
+        size_t __extra =
+          (__pagesize - ((__size + __malloc_header_size) % __pagesize))
+          % __pagesize;
+        __capacity += __extra / sizeof(_CharT);
+        __size = (__capacity + 1) * sizeof(_CharT) + sizeof(_Rep);
+      }
+      else if (__size > 128) // This magic constant is from stl_alloc.h.
+      {
+        size_t __extra =
+          (256 - ((__size + __malloc_header_size) % 256)) % 256;
+        __capacity += __extra / sizeof(_CharT);
+        __size = (__capacity + 1) * sizeof(_CharT) + sizeof(_Rep);
+      }
+
       // NB: Might throw, but no worries about a leak, mate: _Rep()
       // does not throw.
       void* __place = _Raw_bytes_alloc(__alloc).allocate(__size);
@@ -402,19 +426,6 @@
  }
       __r->_M_length = _M_length;
       return __r->_M_refdata();
-    }
-
-  template<typename _CharT, typename _Traits, typename _Alloc>
-  inline bool
-#ifdef _GLIBCPP_ALLOC_CONTROL
-    basic_string<_CharT, _Traits, _Alloc>::_Rep::
-    _S_default_excess(size_t __s, size_t __r)
-#else
-    basic_string<_CharT, _Traits, _Alloc>::_Rep::
-    _S_excess_slop(size_t __s, size_t __r)
-#endif
-    {
-      return 2 * (__s <= 16 ? 16 : __s) < __r;
     }

   template<typename _CharT, typename _Traits, typename _Alloc>
diff -urN gcc-vanilla/libstdc++-v3/include/bits/c++config
gcc/libstdc++-v3/include/bits/c++config
--- gcc-vanilla/libstdc++-v3/include/bits/c++config Thu Nov 22 13:25:14 2001
+++ gcc/libstdc++-v3/include/bits/c++config Fri Nov 23 01:09:38 2001
@@ -55,10 +55,6 @@
 // Use corrected code from the committee library group's issues list.
 #define _GLIBCPP_RESOLVE_LIB_DEFECTS 1

-// Define this to permit user-level control of the expansion of string
-// buffers (via a fn pointer), see basic_string.* for more.
-//#define _GLIBCPP_ALLOC_CONTROL
-
 // Map gthr.h abstraction to that required for STL.  Do not key off of
 // __GTHREADS at this point since we haven't seen the correct symbol
 // yet, instead setup so that include/bits/stl_threads.h will know to















Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]