[all-commits] [llvm/llvm-project] cf8d19: [ADT] Add methods to SmallString for efficient con...

Nathan James via All-commits all-commits at lists.llvm.org
Fri Oct 30 03:08:05 PDT 2020


  Branch: refs/heads/master
  Home:   https://github.com/llvm/llvm-project
  Commit: cf8d19f4fb2ca0eb6b7f8169d1d7ff68ba95d9f5
      https://github.com/llvm/llvm-project/commit/cf8d19f4fb2ca0eb6b7f8169d1d7ff68ba95d9f5
  Author: Nathan James <n.james93 at hotmail.co.uk>
  Date:   2020-10-30 (Fri, 30 Oct 2020)

  Changed paths:
    M llvm/include/llvm/ADT/SmallString.h
    M llvm/unittests/ADT/SmallStringTest.cpp

  Log Message:
  -----------
  [ADT] Add methods to SmallString for efficient concatenation

A common pattern when using SmallString is to repeatedly call append to build a larger string.
The issue here is the optimizer can't see through this and often has to check there is enough space in the storage for each string you try to append.
This results in lots of conditional branches and potentially multiple calls to grow needing to be emitted if the buffer wasn't large enough.
By taking an initializer_list of StringRefs, SmallString can preallocate the storage it needs for all of the StringRefs which only need to grow one time at most, then use a fast path of copying all the strings into its storage knowing there is guaranteed to be enough capacity.
By using StringRefs, this also means you can append different string like types in one go as they will all be implicitly converted to a StringRef.

Reviewed By: dblaikie

Differential Revision: https://reviews.llvm.org/D90386




More information about the All-commits mailing list