This is an archive of the discontinued LLVM Phabricator instance.

Optimize basic_string::operator=(const basic_string&) for SSO assignments
ClosedPublic

Authored by mvels on Jan 14 2020, 7:35 AM.

Details

Summary

This change optimizes the operator=() assignment for short strings by direcly copying the raw data from the source into the current instance. This creates an optimized / inlined mempcy up to over 2X faster for short string assignments. With inlining enabled for operator=, performance is up to 6X faster.

Benchmarks 'as is':
name old time/op new time/op delta
BM_StringAssignStr_Empty_Opaque 6.05ns ± 2% 3.59ns ± 0% -40.67%
BM_StringAssignStr_Empty_Transparent 5.15ns ± 0% 3.08ns ± 0% -40.12%
BM_StringAssignStr_Small_Opaque 7.71ns ± 0% 3.59ns ± 0% -53.45%
BM_StringAssignStr_Small_Transparent 7.66ns ± 0% 3.09ns ± 0% -59.66%
BM_StringAssignStr_Large_Opaque 24.1ns ± 0% 24.9ns ± 0% +3.22%
BM_StringAssignStr_Large_Transparent 22.2ns ± 0% 22.8ns ± 0% +2.77%
BM_StringAssignStr_Huge_Opaque 315ns ± 6% 320ns ± 5% ~
BM_StringAssignStr_Huge_Transparent 318ns ± 5% 321ns ± 4% ~

Benchmarks with partial inlining operator=():
name old time/op new time/op delta
BM_StringAssignStr_Empty_Opaque 5.94ns ± 2% 1.95ns ± 0% -67.21%
BM_StringAssignStr_Empty_Transparent 5.14ns ± 0% 1.04ns ± 1% -79.73%
BM_StringAssignStr_Small_Opaque 7.69ns ± 0% 1.96ns ± 0% -74.48%
BM_StringAssignStr_Small_Transparent 7.65ns ± 0% 1.04ns ± 0% -86.40%
BM_StringAssignStr_Large_Opaque 24.1ns ± 0% 24.5ns ± 0% +1.61%
BM_StringAssignStr_Large_Transparent 22.2ns ± 0% 21.1ns ± 0% -4.70%
BM_StringAssignStr_Huge_Opaque 317ns ± 5% 323ns ± 4% ~
BM_StringAssignStr_Huge_Transparent 318ns ± 5% 320ns ± 5% ~

Event Timeline

mvels created this revision.Jan 14 2020, 7:35 AM
Herald added a project: Restricted Project. · View Herald TranscriptJan 14 2020, 7:35 AM
mvels updated this revision to Diff 237998.Jan 14 2020, 9:06 AM
  • Fix __assign() --> assign()
mvels updated this revision to Diff 238290.Jan 15 2020, 9:14 AM

Scoped for pathological case to be handled outline

mvels updated this revision to Diff 238299.Jan 15 2020, 9:31 AM

Force single branch on fast path

EricWF accepted this revision.Jan 15 2020, 2:25 PM

LGTM.

This revision is now accepted and ready to land.Jan 15 2020, 2:25 PM