1 # Stefan Teleman <[email protected]> |
|
2 # 1. Clearer cast not relying on array index -1. |
|
3 # 2. Optimization based on initial std::string size and capacity. |
|
4 # Internal patch. Not submitted upstream yet. |
|
5 --- libstdc++-v3/include/bits/basic_string.h 2015-05-28 12:27:46.000000000 -0400 |
|
6 +++ libstdc++-v3/include/bits/basic_string.h 2015-09-14 01:16:05.666069507 -0400 |
|
7 @@ -131,6 +131,15 @@ |
|
8 typedef std::reverse_iterator<iterator> reverse_iterator; |
|
9 |
|
10 private: |
|
11 + // Use empty-base optimization: http://www.cantrip.org/emptyopt.html |
|
12 + struct _Alloc_hider : _Alloc |
|
13 + { |
|
14 + _Alloc_hider(_CharT* __dat, const _Alloc& __a) _GLIBCXX_NOEXCEPT |
|
15 + : _Alloc(__a), _M_p(__dat) { } |
|
16 + |
|
17 + _CharT* _M_p; // The actual data. |
|
18 + }; |
|
19 + |
|
20 // _Rep: string representation |
|
21 // Invariants: |
|
22 // 1. String really contains _M_length + 1 characters: due to 21.3.4 |
|
23 @@ -268,15 +277,6 @@ |
|
24 _M_clone(const _Alloc&, size_type __res = 0); |
|
25 }; |
|
26 |
|
27 - // Use empty-base optimization: http://www.cantrip.org/emptyopt.html |
|
28 - struct _Alloc_hider : _Alloc |
|
29 - { |
|
30 - _Alloc_hider(_CharT* __dat, const _Alloc& __a) _GLIBCXX_NOEXCEPT |
|
31 - : _Alloc(__a), _M_p(__dat) { } |
|
32 - |
|
33 - _CharT* _M_p; // The actual data. |
|
34 - }; |
|
35 - |
|
36 public: |
|
37 // Data Members (public): |
|
38 // NB: This is an unsigned type, and thus represents the maximum |
|
39 @@ -298,7 +298,10 @@ |
|
40 |
|
41 _Rep* |
|
42 _M_rep() const _GLIBCXX_NOEXCEPT |
|
43 - { return &((reinterpret_cast<_Rep*> (_M_data()))[-1]); } |
|
44 + { |
|
45 + _Rep* __r = static_cast<_Rep*>(reinterpret_cast<void*>(_M_data())); |
|
46 + return --__r; |
|
47 + } |
|
48 |
|
49 // For the internal use we have functions similar to `begin'/`end' |
|
50 // but they do not call _M_leak. |
|
51 --- libstdc++-v3/include/bits/basic_string.tcc 2014-01-02 17:30:10.000000000 -0500 |
|
52 +++ libstdc++-v3/include/bits/basic_string.tcc 2015-09-14 01:16:09.408163359 -0400 |
|
53 @@ -575,7 +575,8 @@ |
|
54 // malloc implementations that allocate memory blocks rounded up |
|
55 // to a size which is a power of 2). |
|
56 const size_type __pagesize = 4096; |
|
57 - const size_type __malloc_header_size = 4 * sizeof(void*); |
|
58 + const size_type __min_capacity = 32; |
|
59 + const size_type __malloc_header_size = 8 * sizeof(void*); |
|
60 |
|
61 // The below implements an exponential growth policy, necessary to |
|
62 // meet amortized linear time requirements of the library: see |
|
63 @@ -586,6 +587,9 @@ |
|
64 if (__capacity > __old_capacity && __capacity < 2 * __old_capacity) |
|
65 __capacity = 2 * __old_capacity; |
|
66 |
|
67 + if (__capacity < __min_capacity) |
|
68 + __capacity = __min_capacity; |
|
69 + |
|
70 // NB: Need an array of char_type[__capacity], plus a terminating |
|
71 // null char_type() element, plus enough for the _Rep data structure. |
|
72 // Whew. Seemingly so needy, yet so elemental. |
|