Skip to content

Commit fd54b6a

Browse files
[libcxx] Remove Redundant Reset in ~basic_string
8dae17b refactors basic_string for more code reuse. This makes sense in most cases, but has performance overhead in the case of ~basic_string. The refactoring of ~basic_string to call __reset_internal_buffer() added a redundant (inside the destructor) reset of the object, which the optimizer is unable to optimize away in many cases. This patch prevents a ~1% regression we observed on an internal workload when applying the original refactoring. This does slightly pessimize the code readability, but I think this change is worth it given the performance impact. I'm hoping to add a benchmark(s) to the upstream libc++ benchmark suite around string construction/destruction to ensure that this case does not regress as it seems common in real world applications. I will put up a separate PR for that when I figure out a reasonable way to write it.
1 parent f5fdd43 commit fd54b6a

File tree

1 file changed

+7
-1
lines changed

1 file changed

+7
-1
lines changed

libcxx/include/string

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1206,7 +1206,13 @@ public:
12061206
}
12071207
# endif // _LIBCPP_CXX03_LANG
12081208

1209-
inline _LIBCPP_CONSTEXPR_SINCE_CXX20 ~basic_string() { __reset_internal_buffer(); }
1209+
inline _LIBCPP_CONSTEXPR_SINCE_CXX20 ~basic_string() {
1210+
// We do not use __reset_internal_buffer() here to avoid the overhead of
1211+
// resetting the object.
1212+
__annotate_delete();
1213+
if (__is_long())
1214+
__alloc_traits::deallocate(__alloc_, __get_long_pointer(), __get_long_cap());
1215+
}
12101216

12111217
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 operator __self_view() const _NOEXCEPT {
12121218
return __self_view(typename __self_view::__assume_valid(), data(), size());

0 commit comments

Comments
 (0)