Reduce code bloat a bit

Especially the emoji array shrinks a lot with this, but adds a few extra
relocations on startup. But it removes a lot of exception handling code
at runtime, which is nice and possibly this is still faster.
This commit is contained in:
Nicolas Werner 2023-02-19 21:22:20 +01:00
parent 20740c9976
commit 920409e914
No known key found for this signature in database
GPG key ID: C8D75E610773F2D9
8 changed files with 11340 additions and 11297 deletions

View file

@ -7,4 +7,12 @@
using namespace emoji; using namespace emoji;
// a null terminated string_view
template<size_t N>
static consteval std::u16string_view
null_literal(const char16_t (&lit)[N])
{
return std::u16string_view(lit, N);
}

View file

@ -15,11 +15,11 @@ class Emoji(object):
def generate_qml_list(**kwargs): def generate_qml_list(**kwargs):
entrycount = sum([len(c[1]) for c in kwargs.items()]) entrycount = sum([len(c[1]) for c in kwargs.items()])
tmpl = Template(''' tmpl = Template('''
const std::array<Emoji, {{ entrycount }} > emoji::Provider::emoji = { constexpr std::array<Emoji, {{ entrycount }} > emoji::Provider::emoji = {
{%- for c in kwargs.items() %} {%- for c in kwargs.items() %}
// {{ c[0].capitalize() }} // {{ c[0].capitalize() }}
{%- for e in c[1] %} {%- for e in c[1] %}
Emoji{QStringLiteral(u"{{ e.code }}"), QStringLiteral(u"{{ e.shortname }}"), QStringLiteral(u"{{ e.unicodename }}"), emoji::Emoji::Category::{{ c[0].capitalize() }}}, Emoji{null_literal(u"{{ e.code }}"), null_literal(u"{{ e.shortname }}"), null_literal(u"{{ e.unicodename }}"), emoji::Emoji::Category::{{ c[0].capitalize() }}},
{%- endfor %} {%- endfor %}
{%- endfor %} {%- endfor %}
}; };

View file

@ -17,15 +17,15 @@
namespace conf { namespace conf {
namespace modals { namespace modals {
constexpr int WIDGET_MARGIN = 20; inline constexpr int WIDGET_MARGIN = 20;
constexpr int WIDGET_SPACING = 15; inline constexpr int WIDGET_SPACING = 15;
constexpr auto LABEL_MEDIUM_SIZE_RATIO = 1.3; inline constexpr auto LABEL_MEDIUM_SIZE_RATIO = 1.3;
} }
namespace strings { namespace strings {
const QString url_html = QStringLiteral("<a href=\"\\1\">\\1</a>"); inline const QString url_html = QStringLiteral("<a href=\"\\1\">\\1</a>");
const QRegularExpression url_regex( inline const QRegularExpression url_regex(
// match an unquoted URL // match an unquoted URL
[]() { []() {
const auto general_unicode = QStringLiteral( const auto general_unicode = QStringLiteral(
@ -78,17 +78,17 @@ const QRegularExpression url_regex(
}(), }(),
QRegularExpression::UseUnicodePropertiesOption); QRegularExpression::UseUnicodePropertiesOption);
// A matrix link to be converted back to markdown // A matrix link to be converted back to markdown
static const QRegularExpression inline const QRegularExpression
matrixToLink(QStringLiteral(R"(<a href=\"(https://matrix.to/#/.*?)\">(.*?)</a>)")); matrixToLink(QStringLiteral(R"(<a href=\"(https://matrix.to/#/.*?)\">(.*?)</a>)"));
} }
// Window geometry. // Window geometry.
namespace window { namespace window {
constexpr int height = 600; inline constexpr int height = 600;
constexpr int width = 1066; inline constexpr int width = 1066;
constexpr int minHeight = 340; inline constexpr int minHeight = 340;
constexpr int minWidth = 340; inline constexpr int minWidth = 340;
} // namespace window } // namespace window
} // namespace conf } // namespace conf

View file

@ -57,17 +57,17 @@ EmojiModel::data(const QModelIndex &index, int role) const
case Qt::DisplayRole: case Qt::DisplayRole:
case CompletionModel::CompletionRole: case CompletionModel::CompletionRole:
case static_cast<int>(EmojiModel::Roles::Unicode): case static_cast<int>(EmojiModel::Roles::Unicode):
return Provider::emoji[index.row()].unicode; return Provider::emoji[index.row()].unicode();
case Qt::ToolTipRole: case Qt::ToolTipRole:
return Provider::emoji[index.row()].shortName + ", " + return Provider::emoji[index.row()].shortName() + ", " +
Provider::emoji[index.row()].unicodeName; Provider::emoji[index.row()].unicodeName();
case CompletionModel::SearchRole2: case CompletionModel::SearchRole2:
case static_cast<int>(EmojiModel::Roles::UnicodeName): case static_cast<int>(EmojiModel::Roles::UnicodeName):
return Provider::emoji[index.row()].unicodeName; return Provider::emoji[index.row()].unicodeName();
case CompletionModel::SearchRole: case CompletionModel::SearchRole:
case static_cast<int>(EmojiModel::Roles::ShortName): case static_cast<int>(EmojiModel::Roles::ShortName):
return Provider::emoji[index.row()].shortName; return Provider::emoji[index.row()].shortName();
case static_cast<int>(EmojiModel::Roles::Category): case static_cast<int>(EmojiModel::Roles::Category):
return QVariant::fromValue(Provider::emoji[index.row()].category); return QVariant::fromValue(Provider::emoji[index.row()].category);

File diff suppressed because it is too large Load diff

View file

@ -31,15 +31,59 @@ public:
}; };
Q_ENUM(Category) Q_ENUM(Category)
Q_PROPERTY(const QString &unicode MEMBER unicode) Q_PROPERTY(QString unicode READ unicode CONSTANT)
Q_PROPERTY(const QString &shortName MEMBER shortName) Q_PROPERTY(QString shortName READ shortName CONSTANT)
Q_PROPERTY(const QString &unicodeName MEMBER unicodeName) Q_PROPERTY(QString unicodeName READ unicodeName CONSTANT)
Q_PROPERTY(emoji::Emoji::Category category MEMBER category) Q_PROPERTY(emoji::Emoji::Category category MEMBER category)
public: public:
QString unicode; constexpr Emoji(std::u16string_view unicode,
QString shortName; std::u16string_view shortName,
QString unicodeName; std::u16string_view unicodeName,
Category cat)
: unicode_(unicode)
, shortName_(shortName)
, unicodeName_(unicodeName)
, category(cat)
{
}
constexpr Emoji()
: unicode_(u"", 1)
, shortName_(u"", 1)
, unicodeName_(u"", 1)
, category(Category::Search)
{
}
constexpr Emoji(const Emoji &) = default;
constexpr Emoji(Emoji &&) = default;
constexpr Emoji &operator=(const Emoji &) = default;
constexpr Emoji &operator=(Emoji &&) = default;
QString unicode() const
{
return QString::fromRawData(reinterpret_cast<const QChar *>(unicode_.data()),
unicode_.size());
}
QString shortName() const
{
return QString::fromRawData(reinterpret_cast<const QChar *>(shortName_.data()),
shortName_.size());
}
QString unicodeName() const
{
return QString::fromRawData(reinterpret_cast<const QChar *>(unicodeName_.data()),
unicodeName_.size());
}
private:
std::u16string_view unicode_;
std::u16string_view shortName_;
std::u16string_view unicodeName_;
public:
Category category; Category category;
}; };

View file

@ -32,6 +32,7 @@ auto client_ = std::make_unique<mtx::crypto::OlmClient>();
std::map<std::string, std::string> request_id_to_secret_name; std::map<std::string, std::string> request_id_to_secret_name;
constexpr auto MEGOLM_ALGO = "m.megolm.v1.aes-sha2"; constexpr auto MEGOLM_ALGO = "m.megolm.v1.aes-sha2";
constexpr auto OLM_ALGO = "m.olm.v1.curve25519-aes-sha2";
} }
namespace olm { namespace olm {

View file

@ -13,8 +13,6 @@
#include <CacheCryptoStructs.h> #include <CacheCryptoStructs.h>
constexpr auto OLM_ALGO = "m.olm.v1.curve25519-aes-sha2";
namespace olm { namespace olm {
Q_NAMESPACE Q_NAMESPACE