Utils: Changeable SmallString stack size

The size of the small string optimization was hard coded. For some use
cases you want a bigger or smaller size. It is now configurable by a
template parameter. For that we changed the name to BasicSmallString and
added an alias of BasicSmallString<31> to SmallString.

Change-Id: I844b4420d260290307a6018bb6cc4cf3ba7bd449
Reviewed-by: Tim Jenssen <tim.jenssen@qt.io>
This commit is contained in:
Tim Jenssen
2016-11-01 17:15:17 +01:00
parent 601dc5fc07
commit b31c3e0dcb
4 changed files with 216 additions and 210 deletions

View File

@@ -51,41 +51,44 @@ namespace Internal {
using size_type = std::size_t;
static const int smallStringLayoutByteSize = 32;
static const int maximumShortStringDataAreaSize = smallStringLayoutByteSize - 1;
template <uint MaximumShortStringDataAreaSize>
struct AllocatedLayout {
struct Data {
char *pointer;
size_type size;
size_type capacity;
} data;
char dummy[maximumShortStringDataAreaSize - sizeof(Data)];
char dummy[MaximumShortStringDataAreaSize - sizeof(Data)];
std::uint8_t shortStringSize: 6;
std::uint8_t isReadOnlyReference : 1;
std::uint8_t isReference : 1;
};
template <uint MaximumShortStringDataAreaSize>
struct ReferenceLayout {
struct Data {
const char *pointer;
size_type size;
size_type capacity;
} data;
char dummy[maximumShortStringDataAreaSize - sizeof(Data)];
char dummy[MaximumShortStringDataAreaSize - sizeof(Data)];
std::uint8_t shortStringSize: 6;
std::uint8_t isReadOnlyReference : 1;
std::uint8_t isReference : 1;
};
template <uint MaximumShortStringDataAreaSize>
struct ShortStringLayout {
char string[maximumShortStringDataAreaSize];
char string[MaximumShortStringDataAreaSize];
std::uint8_t shortStringSize: 6;
std::uint8_t isReadOnlyReference : 1;
std::uint8_t isReference : 1;
};
template <uint MaximumShortStringDataAreaSize>
struct ALIGNAS_16 StringDataLayout {
static_assert( MaximumShortStringDataAreaSize >= 15, "Size must be greater equal than 15 bytes!");
static_assert(((MaximumShortStringDataAreaSize + 1) % 16) == 0, "Size + 1 must be dividable by 16!");
StringDataLayout() noexcept = default;
constexpr StringDataLayout(const char *string,
@@ -101,7 +104,7 @@ struct ALIGNAS_16 StringDataLayout {
#endif
{
#if __cpp_constexpr >= 201304
if (Size <= maximumShortStringDataAreaSize) {
if (Size <= MaximumShortStringDataAreaSize) {
for (size_type i = 0; i < Size; ++i)
shortString.string[i] = string[i];
@@ -125,9 +128,9 @@ struct ALIGNAS_16 StringDataLayout {
}
union {
AllocatedLayout allocated;
ReferenceLayout reference;
ShortStringLayout shortString = ShortStringLayout();
AllocatedLayout<MaximumShortStringDataAreaSize> allocated;
ReferenceLayout<MaximumShortStringDataAreaSize> reference;
ShortStringLayout<MaximumShortStringDataAreaSize> shortString = ShortStringLayout<MaximumShortStringDataAreaSize>();
};
};