From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 2181) id C4B973858C27; Tue, 12 Apr 2022 21:39:37 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org C4B973858C27 MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset="utf-8" From: Jonathan Wakely To: gcc-cvs@gcc.gnu.org, libstdc++-cvs@gcc.gnu.org Subject: [gcc r12-8124] libstdc++: shrink-to-fit in std::basic_stacktrace::current(skip, max) X-Act-Checkin: gcc X-Git-Author: Jonathan Wakely X-Git-Refname: refs/heads/master X-Git-Oldrev: 7cf88759957a7006e1e1d0a82a1a4de3c4db109e X-Git-Newrev: b2c007b87dcd5db5d59447de2081777aea66f35f Message-Id: <20220412213937.C4B973858C27@sourceware.org> Date: Tue, 12 Apr 2022 21:39:37 +0000 (GMT) X-BeenThere: libstdc++-cvs@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Libstdc++-cvs mailing list List-Unsubscribe: , List-Archive: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 12 Apr 2022 21:39:37 -0000 https://gcc.gnu.org/g:b2c007b87dcd5db5d59447de2081777aea66f35f commit r12-8124-gb2c007b87dcd5db5d59447de2081777aea66f35f Author: Jonathan Wakely Date: Tue Apr 12 17:17:20 2022 +0100 libstdc++: shrink-to-fit in std::basic_stacktrace::current(skip, max) If a large stacktrace is reduced to a max depth that is less than half the capacity it will now be reallocated to remove the unused capacity. libstdc++-v3/ChangeLog: * include/std/stacktrace (basic_stacktrace::current): Reallocate a smaller container if the unused capacity is larger than the used size. Diff: --- libstdc++-v3/include/std/stacktrace | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/libstdc++-v3/include/std/stacktrace b/libstdc++-v3/include/std/stacktrace index 382d900a822..98ce9231150 100644 --- a/libstdc++-v3/include/std/stacktrace +++ b/libstdc++-v3/include/std/stacktrace @@ -289,7 +289,20 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION if (__err < 0) __ret._M_clear(); else if (__ret.size() > __max_depth) - __ret._M_impl._M_resize(__max_depth, __ret._M_alloc); + { + __ret._M_impl._M_resize(__max_depth, __ret._M_alloc); + + if (__ret._M_impl._M_capacity / 2 >= __max_depth) + { + // shrink to fit + _Impl __tmp = __ret._M_impl._M_clone(__ret._M_alloc); + if (__tmp._M_capacity) + { + __ret._M_clear(); + __ret._M_impl = __tmp; + } + } + } } return __ret; }