on xenial help sign in
Added suggested optimization from MP-comment caching a map and string to avoid superfluous dereferencing.
commitbca90c108d38e091cf4352036c1d205593897e05 authorMirco Müller <mirco.mueller@canonical.com> dateThu 2 Jul 2015, 16:21 (11 years ago) parentc4c0657
2 files changed+15−13
plugins/Greeter/Unity/Launcher/launchermodelas.cpp+8−7 plugins/Unity/Launcher/dbusinterface.cpp+7−6
plugins/Greeter/Unity/Launcher/launchermodelas.cpp+8−7
@@ -200,15 +200,16 @@ void LauncherModel::refresh()
200200 Q_FOREACH (LauncherItem* item, m_list) {
201201 bool found = false;
202202 Q_FOREACH(const QVariant &asItem, items) {
203− if (asItem.toMap().value("id").toString() == item->appId()) {
203+ QMap<QString, QVariant> cachedMap = asItem.toMap();
204+ if (cachedMap.value("id").toString() == item->appId()) {
204205 // Only keep and update it if we either show unpinned or it is pinned
205− if (!m_onlyPinned || asItem.toMap().value("pinned").toBool()) {
206+ if (!m_onlyPinned || cachedMap.value("pinned").toBool()) {
206207 found = true;
207− item->setName(asItem.toMap().value("name").toString());
208− item->setIcon(asItem.toMap().value("icon").toString());
209− item->setCount(asItem.toMap().value("count").toInt());
210− item->setCountVisible(asItem.toMap().value("countVisible").toBool());
211− item->setProgress(asItem.toMap().value("progress").toInt());
208+ item->setName(cachedMap.value("name").toString());
209+ item->setIcon(cachedMap.value("icon").toString());
210+ item->setCount(cachedMap.value("count").toInt());
211+ item->setCountVisible(cachedMap.value("countVisible").toBool());
212+ item->setProgress(cachedMap.value("progress").toInt());
212213 int idx = m_list.indexOf(item);
213214 Q_EMIT dataChanged(index(idx), index(idx), QVector<int>() << RoleName << RoleIcon);
214215 }
plugins/Unity/Launcher/dbusinterface.cpp+7−6
@@ -160,31 +160,32 @@ bool DBusInterface::handleMessage(const QDBusMessage& message, const QDBusConnec
160160 LauncherItem *item = static_cast<LauncherItem*>(m_launcherModel->get(index));
161161
162162 QVariantList retval;
163+ QString cachedString = message.arguments()[1].toString();
163164 if (message.member() == "Get") {
164165 if (!item) {
165166 return false;
166167 }
167− if (message.arguments()[1].toString() == "count") {
168+ if (cachedString == "count") {
168169 retval.append(QVariant::fromValue(QDBusVariant(item->count())));
169− } else if (message.arguments()[1].toString() == "countVisible") {
170+ } else if (cachedString == "countVisible") {
170171 retval.append(QVariant::fromValue(QDBusVariant(item->countVisible())));
171− } else if (message.arguments()[1].toString() == "progress") {
172+ } else if (cachedString == "progress") {
172173 retval.append(QVariant::fromValue(QDBusVariant(item->progress())));
173174 }
174175 } else if (message.member() == "Set") {
175− if (message.arguments()[1].toString() == "count") {
176+ if (cachedString == "count") {
176177 int newCount = message.arguments()[2].value<QDBusVariant>().variant().toInt();
177178 if (!item || newCount != item->count()) {
178179 Q_EMIT countChanged(appid, newCount);
179180 notifyPropertyChanged("com.canonical.Unity.Launcher.Item", encodeAppId(appid), "count", QVariant(newCount));
180181 }
181− } else if (message.arguments()[1].toString() == "countVisible") {
182+ } else if (cachedString == "countVisible") {
182183 bool newVisible = message.arguments()[2].value<QDBusVariant>().variant().toBool();
183184 if (!item || newVisible != item->countVisible()) {
184185 Q_EMIT countVisibleChanged(appid, newVisible);
185186 notifyPropertyChanged("com.canonical.Unity.Launcher.Item", encodeAppId(appid), "countVisible", newVisible);
186187 }
187− } else if (message.arguments()[1].toString() == "progress") {
188+ } else if (cachedString == "progress") {
188189 int newProgress = message.arguments()[2].value<QDBusVariant>().variant().toInt();
189190 if (!item || newProgress != item->progress()) {
190191 Q_EMIT progressChanged(appid, newProgress);