From: Jan Hubicka <hubicka@ucw.cz>
To: gcc-patches@gcc.gnu.org, jwakely@redhat.com
Subject: [libstdc++] Improve M_check_len
Date: Sun, 18 Jun 2023 20:27:37 +0200 [thread overview]
Message-ID: <ZI9MmdQ+OMehcdeg@kam.mff.cuni.cz> (raw)
Hi,
_M_check_len is used in vector reallocations. It computes __n + __s but does
checking for case that (__n + __s) * sizeof (Tp) would overflow ptrdiff_t.
Since we know that __s is a size of already allocated memory block if __n is
not too large, this will never happen on 64bit systems since memory is not that
large. This patch adds __builtin_constant_p checks for this case. This size
of fully inlined push_back function that is critical for loops that are
controlled by std::vector based stack.
With the patch to optimize std::max and to handle SRA candidates, we
fully now inline push_back with -O3 (not with -O2), however there are still
quite few silly things for example:
// _78 is original size of the allocated vector.
_76 = stack$_M_end_of_storage_177 - _142;
_77 = _76 /[ex] 8;
_78 = (long unsigned int) _77;
_79 = MAX_EXPR <_78, 1>;
_80 = _78 + _79; // this is result of _M_check_len doubling the allocated vector size.
if (_80 != 0) // result will always be non-zero.
goto <bb 7>; [54.67%]
else
goto <bb 13>; [45.33%]
<bb 7> [local count: 30795011]:
if (_80 > 1152921504606846975) // doubling succesfully allocated memmory will never get so large.
goto <bb 8>; [10.00%]
else
goto <bb 11>; [90.00%]
<bb 8> [local count: 3079501]:
if (_80 > 2305843009213693951) // I wonder if we really want to have two different throws
goto <bb 9>; [50.00%]
else
goto <bb 10>; [50.00%]
<bb 9> [local count: 1539750]:
std::__throw_bad_array_new_length ();
<bb 10> [local count: 1539750]:
std::__throw_bad_alloc ();
<bb 11> [local count: 27715510]:
_108 = _80 * 8;
_109 = operator new (_108);
Maybe we want to add assumption that result of the function is never
greater than max_size to get rid of the two checks above. However this
will still be recongized only after inlining and will continue confusing
inliner heuristics.
Bootstrapped/regtested x86_64-linux. I am not too familiar with libstdc++ internals,
so would welcome comments and ideas.
libstdc++-v3/ChangeLog:
PR tree-optimization/110287
* include/bits/stl_vector.h: Optimize _M_check_len for constantly sized
types and allocations.
diff --git a/libstdc++-v3/include/bits/stl_vector.h b/libstdc++-v3/include/bits/stl_vector.h
index 70ced3d101f..3ad59fe3e2b 100644
--- a/libstdc++-v3/include/bits/stl_vector.h
+++ b/libstdc++-v3/include/bits/stl_vector.h
@@ -1895,11 +1895,22 @@ _GLIBCXX_BEGIN_NAMESPACE_CONTAINER
size_type
_M_check_len(size_type __n, const char* __s) const
{
- if (max_size() - size() < __n)
- __throw_length_error(__N(__s));
+ // On 64bit systems vectors of small sizes can not
+ // reach overflow by growing by small sizes; before
+ // this happens, we will run out of memory.
+ if (__builtin_constant_p (sizeof (_Tp))
+ && __builtin_constant_p (__n)
+ && sizeof (ptrdiff_t) >= 8
+ && __n < max_size () / 2)
+ return size() + (std::max)(size(), __n);
+ else
+ {
+ if (max_size() - size() < __n)
+ __throw_length_error(__N(__s));
- const size_type __len = size() + (std::max)(size(), __n);
- return (__len < size() || __len > max_size()) ? max_size() : __len;
+ const size_type __len = size() + (std::max)(size(), __n);
+ return (__len < size() || __len > max_size()) ? max_size() : __len;
+ }
}
// Called by constructors to check initial size.
next reply other threads:[~2023-06-18 18:27 UTC|newest]
Thread overview: 14+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-06-18 18:27 Jan Hubicka [this message]
2023-06-19 10:12 ` Jonathan Wakely
2023-06-19 11:05 ` Jan Hubicka
2023-06-19 11:20 ` Jakub Jelinek
2023-06-19 15:13 ` Jonathan Wakely
2023-06-19 15:14 ` Jonathan Wakely
2023-06-19 15:35 ` Jonathan Wakely
2023-06-20 7:50 ` Jan Hubicka
2023-06-20 8:05 ` Jan Hubicka
2023-06-20 8:07 ` Jakub Jelinek
2023-06-20 8:21 ` Andreas Schwab
2023-06-20 10:45 ` Jonathan Wakely
2023-06-20 10:50 ` Jonathan Wakely
2023-06-19 16:14 ` Jan Hubicka
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=ZI9MmdQ+OMehcdeg@kam.mff.cuni.cz \
--to=hubicka@ucw.cz \
--cc=gcc-patches@gcc.gnu.org \
--cc=jwakely@redhat.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).