diff mbox series

[committed,4/5] libstdc++: shrink-to-fit in std::basic_stacktrace::current(skip, max)

Message ID 20220412214128.509227-4-jwakely@redhat.com
State New
Headers show
Series [committed,1/5] libstdc++: Reduce memory usage in std::stacktrace::current | expand

Commit Message

Jonathan Wakely April 12, 2022, 9:41 p.m. UTC
Tested powerpc64-linux, pushed to trunk.

-- >8 --

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.
---
 libstdc++-v3/include/std/stacktrace | 15 ++++++++++++++-
 1 file changed, 14 insertions(+), 1 deletion(-)
diff mbox series

Patch

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;
       }