Utils: Make Id::name() return a QByteArrayView

As we keep all id name content in IdCache which stays until
application shutdown this is as safe as returning a full
QByteArray, but cheaper.

Change-Id: Ic41feb0a648d1267bbfb57a6a18a724a0ab52d80
Reviewed-by: Eike Ziller <eike.ziller@qt.io>
This commit is contained in:
hjk
2024-07-25 17:09:51 +02:00
parent e148be365e
commit 010eb642df
6 changed files with 20 additions and 12 deletions

View File

@@ -137,10 +137,17 @@ Id Id::generate()
Returns an internal representation of the id.
*/
QByteArray Id::name() const
QByteArrayView Id::name() const
{
QReadLocker lock(&s_cacheMutex);
return stringFromId.value(m_id).str;
StringHolder holder = stringFromId.value(m_id);
return QByteArrayView(holder.str, holder.n);
}
/*! \internal */
QByteArray Id::toByteArray() const
{
return name().toByteArray();
}
/*!
@@ -155,8 +162,7 @@ QByteArray Id::name() const
QString Id::toString() const
{
QReadLocker lock(&s_cacheMutex);
return QString::fromUtf8(stringFromId.value(m_id).str);
return QString::fromUtf8(name());
}
/*! \internal */
@@ -335,14 +341,14 @@ bool Id::alphabeticallyBefore(Id other) const
QString Id::suffixAfter(Id baseId) const
{
const QByteArray b = baseId.name();
const QByteArray n = name();
const QByteArrayView b = baseId.name();
const QByteArrayView n = name();
return n.startsWith(b) ? QString::fromUtf8(n.mid(b.size())) : QString();
}
QDataStream &operator<<(QDataStream &ds, Id id)
{
return ds << id.name();
return ds << id.name().toByteArray();
}
QDataStream &operator>>(QDataStream &ds, Id &id)

View File

@@ -34,7 +34,8 @@ public:
Id withSuffix(const QStringView suffix) const;
Id withPrefix(const char *prefix) const;
QByteArray name() const;
QByteArrayView name() const;
QByteArray toByteArray() const; // Avoid
QString toString() const; // Avoid.
Key toKey() const; // FIXME: Replace uses with .name() after Store/key transition.
QVariant toSetting() const; // Good to use.

View File

@@ -19,6 +19,7 @@ public:
Key(Key &&) = default;
Key(const QByteArray &key) : data(key) {}
Key(const QByteArrayView key) : data(key.toByteArray()) {}
template <int N>
Key(const char (&key)[N]) : data(key) {}

View File

@@ -597,7 +597,7 @@ TouchBarActionContainer::TouchBarActionContainer(Id id,
const QIcon &icon,
const QString &text)
: ActionContainerPrivate(id, actionManagerPrivate)
, m_touchBar(std::make_unique<TouchBar>(id.withPrefix(ID_PREFIX).name(), icon, text))
, m_touchBar(std::make_unique<TouchBar>(id.withPrefix(ID_PREFIX).toByteArray(), icon, text))
{
}
@@ -623,7 +623,7 @@ QAction *TouchBarActionContainer::actionForItem(QObject *item) const
void TouchBarActionContainer::insertAction(QAction *before, Command *command)
{
m_touchBar->insertAction(before,
command->id().withPrefix(ID_PREFIX).name(),
command->id().withPrefix(ID_PREFIX).toByteArray(),
command->touchBarAction());
}

View File

@@ -158,7 +158,7 @@ Environment ExternalTool::baseEnvironment() const
{
if (m_baseEnvironmentProviderId.isValid()) {
const std::optional<EnvironmentProvider> provider = EnvironmentProvider::provider(
m_baseEnvironmentProviderId.name());
m_baseEnvironmentProviderId.toByteArray());
if (provider && provider->environment)
return provider->environment();
}

View File

@@ -52,7 +52,7 @@ void MacroEvent::load(QDataStream &stream)
void MacroEvent::save(QDataStream &stream) const
{
stream << m_id.name();
stream << m_id.toByteArray();
stream << int(m_values.count());
for (auto i = m_values.cbegin(), end = m_values.cend(); i != end; ++i)
stream << i.key() << i.value();