tvanslyke created this revision.
tvanslyke added a reviewer: howard.hinnant.
Herald added a subscriber: cfe-commits.

shrink_to_fit() ends up doing a lot work to get information that we already 
know since we just called clear().  This change seems concise enough to be 
worth the couple extra lines and my benchmarks show that it is indeed a pretty 
decent win.  It looks like the same thing is going on twice in 
__copy_assign_alloc(), but I didn't want to go overboard since this is my first 
contribution to llvm/libc++.  Go easy on me!


Repository:
  rCXX libc++

https://reviews.llvm.org/D41976

Files:
  string


Index: string
===================================================================
--- string
+++ string
@@ -2103,7 +2103,15 @@
 #endif
 {
     clear();
-    shrink_to_fit();
+
+    size_type __cap = capacity();
+    // check if we need to deallocate anything
+    if(__recommend(0) != __cap)
+    {
+        pointer __old_data = __get_long_pointer();
+        __alloc_traits::deallocate(__alloc(), __old_data, __cap + 1);
+        __set_short_size(0);
+    }
     __r_.first() = __str.__r_.first();
     __move_assign_alloc(__str);
     __str.__zero();


Index: string
===================================================================
--- string
+++ string
@@ -2103,7 +2103,15 @@
 #endif
 {
     clear();
-    shrink_to_fit();
+
+    size_type __cap = capacity();
+    // check if we need to deallocate anything
+    if(__recommend(0) != __cap)
+    {
+        pointer __old_data = __get_long_pointer();
+        __alloc_traits::deallocate(__alloc(), __old_data, __cap + 1);
+        __set_short_size(0);
+    }
     __r_.first() = __str.__r_.first();
     __move_assign_alloc(__str);
     __str.__zero();
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to