[PATCH] D41976: Low-hanging fruit optimization in string::__move_assign().

Timothy VanSlyke via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Thu Jan 11 19:15:46 PST 2018


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();


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D41976.129568.patch
Type: text/x-patch
Size: 553 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20180112/3a44637b/attachment-0001.bin>


More information about the cfe-commits mailing list