[committed] libstdc++: Improve behaviour of std::stacktrace::current

Jonathan Wakely jwakely@redhat.com
Mon Apr 11 21:27:55 GMT 2022


On Mon, 11 Apr 2022 at 18:03, Jonathan Wakely via Libstdc++
<libstdc++@gcc.gnu.org> wrote:
>         // Precondition: _M_frames == nullptr
>         pointer
>         _M_allocate(allocator_type& __alloc, size_type __n) noexcept
>         {
>           __try
>             {
> -             _M_frames = __n ? __alloc.allocate(__n) : nullptr;
> -             _M_capacity = __n;
> +             if (0 < __n && __n <= _S_max_size(__alloc)) [[unlikely]]

I originally changed this to return early if the size isn't OK:

  if (unlikely condition)
    return nullptr;

but in the version I pushed it's:

  if (likely condition)
    // do allocation

but forgot to change the attribute to match.

Fixed by the attached. Tested x86_64-linux and pushed to trunk.

I have further improvements to stacktrace::current coming tomorrow.
-------------- next part --------------
commit b1124648ff8f655307f264d7b353fd68e3b03e9c
Author: Jonathan Wakely <jwakely@redhat.com>
Date:   Mon Apr 11 20:13:44 2022

    libstdc++: Fix incorrect branch prediction hint in std::stacktrace
    
    libstdc++-v3/ChangeLog:
    
            * include/std/stacktrace (basic_stacktrace::_Impl::_M_allocate):
            Change [[unlikely]] attribute to [[likely]].

diff --git a/libstdc++-v3/include/std/stacktrace b/libstdc++-v3/include/std/stacktrace
index dd78c71c5dc..79038e803f2 100644
--- a/libstdc++-v3/include/std/stacktrace
+++ b/libstdc++-v3/include/std/stacktrace
@@ -579,7 +579,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
 	{
 	  __try
 	    {
-	      if (0 < __n && __n <= _S_max_size(__alloc)) [[unlikely]]
+	      if (0 < __n && __n <= _S_max_size(__alloc)) [[likely]]
 		{
 		  _M_frames = __alloc.allocate(__n);
 		  _M_capacity = __n;


More information about the Libstdc++ mailing list