This is an archive of the discontinued LLVM Phabricator instance.

Partially inline copy constructor basic_string(const basic_string&[, allocator]) - ALTERNATIVE to https://reviews.llvm.org/D68617
AbandonedPublic

Authored by mvels on Oct 16 2019, 11:18 AM.

Details

Summary

Partially inline copy constructor basic_string(const basic_string&[, allocator])

This change optimizes the copy constructor using partial inlining.

  • adds default_value_tag() to memory, to support default initialization
  • inlines copy contructor: non SSO init delegated to instantiated __init_long() method

Note that this change does not consider existing value initialization ctors, most would likely benefit from default initialization.

Generated code is small, i.e, considerably smaller than other hot inlined functions such as move ctor

given:

void StringCopyCtor(void* mem, const std::string& s) {
    std::string*p = new(mem) std::string{s};
}

asm:

        cmp     byte ptr [rsi + 23], 0
        js      .LBB0_2
        mov     rax, qword ptr [rsi + 16]
        mov     qword ptr [rdi + 16], rax
        movups  xmm0, xmmword ptr [rsi]
        movups  xmmword ptr [rdi], xmm0
        ret
.LBB0_2:
        jmp     std::basic_string::__init_long # TAILCALL

Benchmarks::
BM_StringCopy_Empty                                           5.18ns ± 7%             1.53ns ± 5%  -70.45%        (p=0.000 n=10+10)
BM_StringCopy_Small                                           5.18ns ± 7%             1.54ns ± 5%  -70.21%        (p=0.000 n=10+10)
BM_StringCopy_Large                                           19.0ns ± 1%             19.3ns ± 1%   +1.77%        (p=0.000 n=10+10)
BM_StringCopy_Huge                                             321ns ± 4%              310ns ± 1%     ~            (p=0.408 n=10+8)

Event Timeline

mvels created this revision.Oct 16 2019, 11:18 AM
Herald added a project: Restricted Project. · View Herald TranscriptOct 16 2019, 11:18 AM
mvels retitled this revision from Update to feature define in __config to Partially inline copy constructor basic_string(const basic_string&[, allocator]) - ALTERNATIVE to https://reviews.llvm.org/D68617.Oct 16 2019, 11:22 AM
mvels updated this revision to Diff 225275.Oct 16 2019, 11:26 AM
  • Remove old defines
mvels updated this revision to Diff 225276.Oct 16 2019, 11:28 AM
  • Relocate define in __config
mvels updated this revision to Diff 225279.Oct 16 2019, 11:30 AM
  • fix ifdef for __init_long
Harbormaster completed remote builds in B39668: Diff 225276.
Harbormaster completed remote builds in B39670: Diff 225279.
mvels abandoned this revision.Jan 17 2020, 10:40 AM