[ADT] Add methods to SmallString for efficient concatenation
authorNathan James <n.james93@hotmail.co.uk>
Fri, 30 Oct 2020 10:07:39 +0000 (10:07 +0000)
committerNathan James <n.james93@hotmail.co.uk>
Fri, 30 Oct 2020 10:07:40 +0000 (10:07 +0000)
commitcf8d19f4fb2ca0eb6b7f8169d1d7ff68ba95d9f5
tree3bc1a66340c8daf3ce7bf3a069c359376344b136
parent511484f27d923e16cbb88d3b00f800386a263cd5
[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
llvm/include/llvm/ADT/SmallString.h
llvm/unittests/ADT/SmallStringTest.cpp