Commit 1eb0c94f authored by Marco Bubke's avatar Marco Bubke

Utils: Add SmallString

SmallString is a very simple utf8 string class. It's purpose is performance.
It uses a short string opimization which provides 31 bytes of heap free
memory to save a 30 bytes long string plus null terminator. If the string
gets larger heap is allocated. The grow strategy is 1.5 to improve reuse
of allocated memory.

It uses optionally constexpr to provide string literals.

Change-Id: I7757fb51abfeca200d074cbfce2f1d99edc0ecb0
Reviewed-by: default avatarTobias Hunger <tobias.hunger@theqtcompany.com>
parent 7984e928
This diff is collapsed.
/****************************************************************************
**
** Copyright (C) 2016 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of Qt Creator.
**
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3 as published by the Free Software
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
**
****************************************************************************/
#pragma once
#include <cstddef>
#include <iterator>
#pragma push_macro("noexcept")
#ifndef __cpp_noexcept
#define noexcept
#endif
namespace Utils {
namespace Internal {
template <class Category,
class Type,
typename DistanceType = ptrdiff_t,
typename Pointer = Type*,
typename Reference = Type&>
struct SmallStringIterator : public std::iterator<Category, Type, DistanceType, Pointer, Reference>
{
SmallStringIterator() noexcept = default;
SmallStringIterator(Pointer ptr) : pointer_(ptr) noexcept
{
}
SmallStringIterator operator++() noexcept
{
return ++pointer_;
}
SmallStringIterator operator++(int) noexcept
{
return pointer_++;
}
SmallStringIterator operator--() noexcept
{
return --pointer_;
}
SmallStringIterator operator--(int) noexcept
{
return pointer_--;
}
SmallStringIterator operator+(DistanceType difference) const noexcept
{
return pointer_ + difference;
}
SmallStringIterator operator-(DistanceType difference) const noexcept
{
return pointer_ - difference;
}
SmallStringIterator operator+(std::size_t difference) const noexcept
{
return pointer_ + difference;
}
SmallStringIterator operator-(std::size_t difference) const noexcept
{
return pointer_ - difference;
}
DistanceType operator-(SmallStringIterator other) const noexcept
{
return pointer_ - other.data();
}
SmallStringIterator operator+=(DistanceType difference) noexcept
{
return pointer_ += difference;
}
SmallStringIterator operator-=(DistanceType difference) noexcept
{
return pointer_ -= difference;
}
Reference operator*() noexcept
{
return *pointer_;
}
const Reference operator*() const noexcept
{
return *pointer_;
}
Pointer operator->() noexcept
{
return pointer_;
}
const Pointer operator->() const noexcept
{
return pointer_;
}
bool operator==(SmallStringIterator other) const noexcept
{
return pointer_ == other.pointer_;
}
bool operator!=(SmallStringIterator other) const noexcept
{
return pointer_ != other.pointer_;
}
bool operator<(SmallStringIterator other) const noexcept
{
return pointer_ < other.pointer_;
}
Pointer data() noexcept
{
return pointer_;
}
private:
Pointer pointer_ = nullptr;
};
} // namespace Internal
} // namespace Utils
#pragma pop_macro("noexcept")
/****************************************************************************
**
** Copyright (C) 2016 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of Qt Creator.
**
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3 as published by the Free Software
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
**
****************************************************************************/
#pragma once
#include <QtGlobal>
#include <cstdint>
#pragma push_macro("constexpr")
#ifndef __cpp_constexpr
#define constexpr
#endif
#pragma push_macro("noexcept")
#ifndef __cpp_noexcept
#define noexcept
#endif
#ifdef __cpp_alignas
#define ALIGNAS_16 alignas(16)
#else
#define ALIGNAS_16
#endif
namespace Utils {
namespace Internal {
using size_type = std::size_t;
static const int smallStringLayoutByteSize = 32;
static const int maximumShortStringDataAreaSize = smallStringLayoutByteSize - 1;
struct AllocatedLayout {
struct Data {
char *pointer;
size_type size;
size_type capacity;
} data;
char dummy[maximumShortStringDataAreaSize - sizeof(Data)];
std::uint8_t isReference : 1;
std::uint8_t shortStringSize: 6;
std::uint8_t hasAllocated : 1;
};
struct ReferenceLayout {
struct Data {
const char *pointer;
size_type size;
size_type capacity;
} data;
char dummy[maximumShortStringDataAreaSize - sizeof(Data)];
std::uint8_t shortStringSize: 6;
std::uint8_t isReference : 1;
std::uint8_t hasAllocated : 1;
};
struct ShortStringLayout {
char string[maximumShortStringDataAreaSize];
std::uint8_t shortStringSize: 6;
std::uint8_t isReference : 1;
std::uint8_t hasAllocated : 1;
};
struct ALIGNAS_16 StringDataLayout {
StringDataLayout() noexcept = default;
constexpr StringDataLayout(const char *string,
size_type size) noexcept
: reference({{string, size, 0}, {}, 0, true, true})
{
}
template<size_type Size>
constexpr StringDataLayout(const char(&string)[Size]) noexcept
#if __cpp_constexpr < 201304
: reference({{string, Size - 1, 0}, {}, 0, true, true})
#endif
{
#if __cpp_constexpr >= 201304
if (Size <= maximumShortStringDataAreaSize) {
for (size_type i = 0; i < Size; ++i)
shortString.string[i] = string[i];
#if defined(__GNUC__)
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Woverflow"
shortString.shortStringSize = std::uint8_t(Size) - 1;
#pragma GCC diagnostic pop
#endif
shortString.hasAllocated = false;
shortString.isReference = false;
} else {
reference.data.pointer = string;
reference.data.size = Size - 1;
reference.data.capacity = 0;
reference.shortStringSize = 0;
reference.hasAllocated = true;
reference.isReference = true;
}
#endif
}
union {
AllocatedLayout allocated;
ReferenceLayout reference;
ShortStringLayout shortString = {};
};
};
} // namespace Internal
} // namespace Utils
#pragma pop_macro("noexcept")
#pragma pop_macro("constexpr")
/****************************************************************************
**
** Copyright (C) 2016 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of Qt Creator.
**
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3 as published by the Free Software
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
**
****************************************************************************/
#pragma once
#include "smallstringiterator.h"
#include "smallstringlayout.h"
#include "smallstringview.h"
#pragma push_macro("constexpr")
#ifndef __cpp_constexpr
#define constexpr
#endif
#pragma push_macro("noexcept")
#ifndef __cpp_noexcept
#define noexcept
#endif
namespace Utils {
class SmallString;
class SmallStringLiteral
{
friend class SmallString;
public:
using const_iterator = Internal::SmallStringIterator<std::random_access_iterator_tag, const char>;
using const_reverse_iterator = std::reverse_iterator<const_iterator>;
using size_type = std::size_t;
template<size_type Size>
constexpr
SmallStringLiteral(const char(&string)[Size]) noexcept
: m_data(string)
{
static_assert(Size >= 1, "Invalid string literal! Length is zero!");
}
constexpr
SmallStringLiteral(const char *string, const size_type size) noexcept
: m_data(string, size)
{
}
constexpr
const char *data() const
{
return Q_LIKELY(isShortString()) ? m_data.shortString.string : m_data.allocated.data.pointer;
}
constexpr
size_type size() const
{
return Q_LIKELY(isShortString()) ? m_data.shortString.shortStringSize : m_data.allocated.data.size;
}
const_iterator begin() const noexcept
{
return data();
}
const_iterator end() const noexcept
{
return data() + size();
}
const_reverse_iterator rbegin() const noexcept
{
return const_reverse_iterator(end() - 1l);
}
const_reverse_iterator rend() const noexcept
{
return const_reverse_iterator(begin() - 1l);
}
constexpr static
size_type shortStringCapacity() noexcept
{
return sizeof(Internal::ShortStringLayout) - 2;
}
constexpr
bool isShortString() const noexcept
{
return !m_data.shortString.hasAllocated;
}
constexpr
bool isReference() const noexcept
{
return m_data.shortString.isReference;
}
operator SmallStringView() const
{
return SmallStringView(data(), size());
}
private:
constexpr SmallStringLiteral(Internal::StringDataLayout data) noexcept
: m_data(data)
{
}
private:
Internal::StringDataLayout m_data;
};
} // namespace Utils
#pragma pop_macro("noexcept")
#pragma pop_macro("constexpr")
/****************************************************************************
**
** Copyright (C) 2016 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of Qt Creator.
**
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3 as published by the Free Software
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
**
****************************************************************************/
#pragma once
#include <cstring>
#include <memory>
namespace Utils {
namespace Memory {
inline char *allocate(std::size_t size)
{
#ifdef WIN32
return static_cast<char*>(_aligned_malloc(size, 64));
#else
return static_cast<char*>(std::malloc(size));
#endif
}
inline void deallocate(char *memory)
{
#ifdef WIN32
_aligned_free(memory);
#else
#if defined(__GNUC__) && !defined(__clang__)
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wfree-nonheap-object"
std::free(memory);
#pragma GCC diagnostic pop
#endif
#endif
}
inline char *reallocate(char *oldMemory, std::size_t newSize)
{
#ifdef WIN32
return static_cast<char*>(_aligned_realloc(oldMemory, newSize, 64));
#else
return static_cast<char*>(std::realloc(oldMemory, newSize));
#endif
}
} // namespace Memory
} // namespace Utils
/****************************************************************************
**
** Copyright (C) 2016 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of Qt Creator.
**
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3 as published by the Free Software
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
**
****************************************************************************/
#pragma once
#include "utils_global.h"
#include "smallstring.h"
#include <vector>
#pragma push_macro("noexcept")
#ifndef __cpp_noexcept
#define noexcept
#endif
namespace Utils {
class SmallStringVector : public std::vector<Utils::SmallString>
{
public:
SmallStringVector() = default;
SmallStringVector(std::initializer_list<Utils::SmallString> list)
{
reserve(list.size());
for (auto &&entry : list)
push_back(entry.clone());
}
explicit SmallStringVector(const QStringList &stringList)
{
reserve(std::size_t(stringList.count()));
for (const QString &string : stringList)
push_back(Utils::SmallString::fromQString(string));
}
#if !defined(UNIT_TESTS) && !(defined(_MSC_VER) && _MSC_VER < 1900)
SmallStringVector(const SmallStringVector &) = delete;
SmallStringVector &operator=(const SmallStringVector &) = delete;
#else
SmallStringVector(const SmallStringVector &) = default;
SmallStringVector &operator=(const SmallStringVector &) = default;
#endif
#if !(defined(_MSC_VER) && _MSC_VER < 1900)
SmallStringVector(SmallStringVector &&) noexcept = default;
SmallStringVector &operator=(SmallStringVector &&) noexcept = default;
#else
SmallStringVector(SmallStringVector &&other)
: std::vector<Utils::SmallString>(std::move(other))
{
}
SmallStringVector &operator=(SmallStringVector &&other)
{
std::vector<Utils::SmallString>(std::move(other));
return *this;
}
#endif
Utils::SmallString join(Utils::SmallString &&separator) const
{
Utils::SmallString joinedString;
joinedString.reserve(totalByteSize() + separator.size() * std::size_t(size()));
for (auto stringIterator = begin(); stringIterator != end(); ++stringIterator) {
joinedString.append(*stringIterator);
if (std::next(stringIterator) != end())
joinedString.append(separator);
}
return joinedString;
}
bool contains(const Utils::SmallString &string) const noexcept
{
return std::find(cbegin(), cend(), string) != cend();
}
bool removeFast(Utils::SmallStringView valueToBeRemoved)
{
auto position = std::remove(begin(), end(), valueToBeRemoved);
const bool hasEntry = position != end();
erase(position, end());
return hasEntry;
}
void append(Utils::SmallString &&string)
{
push_back(std::move(string));
}
SmallStringVector clone() const
{
SmallStringVector clonedVector;
clonedVector.reserve(size());
for (auto &&entry : *this)
clonedVector.push_back(entry.clone());
return clonedVector;
}
private:
std::size_t totalByteSize() const
{
std::size_t totalSize = 0;
for (auto &&string : *this)
totalSize += string.size();
return totalSize;
}
};
template <typename Type>
std::vector<Type> clone(const std::vector<Type> &vector)
{
std::vector<Type> clonedVector;
clonedVector.reserve(vector.size());
for (auto &&entry : vector)
clonedVector.push_back(entry.clone());
return clonedVector;
}
inline QDataStream &operator<<(QDataStream &out, const SmallStringVector &stringVector)
{
out << quint64(stringVector.size());
for (auto &&string : stringVector)
out << string;
return out;
}
inline QDataStream &operator>>(QDataStream &in, SmallStringVector &stringVector)
{
stringVector.clear();
quint64 size;
in >> size;
stringVector.reserve(size);
for (quint64 i = 0; i < size; ++i) {
SmallString string;
in >> string;
stringVector.push_back(std::move(string));
}
return in;
}
inline QDebug operator<<(QDebug debug, const SmallStringVector &stringVector)
{
debug << "StringVector(" << stringVector.join(Utils::SmallString(", ")).constData() << ")";
return debug;
}
inline void PrintTo(const SmallStringVector &textVector, ::std::ostream* os)
{
*os << "StringVector(" << textVector.join(Utils::SmallString(", ")).constData() << ")";
}
} // namespace Utils;
namespace std {
template<typename Type>
QDataStream &operator<<(QDataStream &out, const vector<Type> &vector)
{
out << quint64(vector.size());
for (auto &&entry : vector)
out << entry;
return out;
}