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

Martijn Vels via Phabricator via libcxx-commits libcxx-commits at lists.llvm.org
Wed Oct 16 11:24:20 PDT 2019


mvels created this revision.
Herald added subscribers: libcxx-commits, christof.
Herald added a project: libc++.
mvels added reviewers: ldionne, EricWF, mclow.lists.
Herald added a subscriber: dexonsmith.
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".

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)


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D69061

Files:
  libcxx/include/__config
  libcxx/include/memory
  libcxx/include/string

-------------- next part --------------
A non-text attachment was scrubbed...
Name: D69061.225274.patch
Type: text/x-patch
Size: 5383 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/libcxx-commits/attachments/20191016/796533ba/attachment.bin>


More information about the libcxx-commits mailing list