Merge lp:~aacid/unity-scopes-shell/optimize_resultsmodel_data into lp:unity-scopes-shell

Proposed by Albert Astals Cid
Status: Merged
Approved by: Marcus Tomlinson
Approved revision: 338
Merged at revision: 347
Proposed branch: lp:~aacid/unity-scopes-shell/optimize_resultsmodel_data
Merge into: lp:unity-scopes-shell
Diff against target: 185 lines (+50/-34)
2 files modified
src/Unity/resultsmodel.cpp (+48/-32)
src/Unity/resultsmodel.h (+2/-2)
To merge this branch: bzr merge lp:~aacid/unity-scopes-shell/optimize_resultsmodel_data
Reviewer Review Type Date Requested Status
Marcus Tomlinson (community) Approve
Review via email: mp+300883@code.launchpad.net

Commit message

Optimize ResultsModel::data a bit

Do the role to component string early in ::setComponentsMapping instead of every time in ::data
This way we do not need to create temporary strings every time ::data gets called

Also since we know there's a finite amount of roles, store the role->string mapping in a vector instead of a map/hash

To post a comment you must log in.
Revision history for this message
Marcus Tomlinson (marcustomlinson) wrote :

Looks good.

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'src/Unity/resultsmodel.cpp'
2--- src/Unity/resultsmodel.cpp 2016-05-05 09:40:46 +0000
3+++ src/Unity/resultsmodel.cpp 2016-07-22 13:17:47 +0000
4@@ -43,6 +43,7 @@
5 , m_maxAttributes(2)
6 , m_purge(true)
7 {
8+ m_componentMapping.resize(RoleSocialActions + 1);
9 }
10
11 QString ResultsModel::categoryId() const
12@@ -60,17 +61,45 @@
13
14 void ResultsModel::setComponentsMapping(QHash<QString, QString> const& mapping)
15 {
16- std::unordered_map<std::string, std::string> newMapping;
17+ QVector<std::string> newMapping(RoleSocialActions + 1);
18 for (auto it = mapping.begin(); it != mapping.end(); ++it) {
19- newMapping[it.key().toStdString()] = it.value().toStdString();
20+ Roles field;
21+ const QString fieldName = it.key();
22+ if (fieldName == QLatin1String("title")) {
23+ field = RoleTitle;
24+ } else if (fieldName == QLatin1String("attributes")) {
25+ field = RoleAttributes;
26+ } else if (fieldName == QLatin1String("art")) {
27+ field = RoleArt;
28+ } else if (fieldName == QLatin1String("subtitle")) {
29+ field = RoleSubtitle;
30+ } else if (fieldName == QLatin1String("mascot")) {
31+ field = RoleMascot;
32+ } else if (fieldName == QLatin1String("emblem")) {
33+ field = RoleEmblem;
34+ } else if (fieldName == QLatin1String("summary")) {
35+ field = RoleSummary;
36+ } else if (fieldName == QLatin1String("background")) {
37+ field = RoleBackground;
38+ } else if (fieldName == QLatin1String("overlay-color")) {
39+ field = RoleOverlayColor;
40+ } else if (fieldName == QLatin1String("quick-preview-data")) {
41+ field = RoleQuickPreviewData;
42+ } else if (fieldName == QLatin1String("social-actions")) {
43+ field = RoleSocialActions;
44+ } else {
45+ qDebug() << "Unknown components field" << fieldName;
46+ break;
47+ }
48+ newMapping[field] = it.value().toStdString();
49 }
50
51 if (rowCount() > 0) {
52 beginResetModel();
53- m_componentMapping.swap(newMapping);
54+ m_componentMapping = newMapping;
55 endResetModel();
56 } else {
57- m_componentMapping.swap(newMapping);
58+ m_componentMapping = newMapping;
59 }
60 }
61
62@@ -218,13 +247,11 @@
63 }
64
65 QVariant
66-ResultsModel::componentValue(scopes::Result const* result, std::string const& fieldName) const
67+ResultsModel::componentValue(scopes::Result const* result, Roles field) const
68 {
69- auto mappingIt = m_componentMapping.find(fieldName);
70- if (mappingIt == m_componentMapping.end()) {
71+ std::string const& realFieldName = m_componentMapping[field];
72+ if (realFieldName.empty())
73 return QVariant();
74- }
75- std::string const& realFieldName = mappingIt->second;
76 try {
77 scopes::Variant const& v = result->value(realFieldName);
78 return scopeVariantToQVariant(v);
79@@ -238,12 +265,8 @@
80 QVariant
81 ResultsModel::attributesValue(scopes::Result const* result) const
82 {
83- auto mappingIt = m_componentMapping.find("attributes");
84- if (mappingIt == m_componentMapping.end()) {
85- return QVariant();
86- }
87 try {
88- std::string const& realFieldName = mappingIt->second;
89+ std::string const& realFieldName = m_componentMapping[RoleAttributes];
90 scopes::Variant const& v = result->value(realFieldName);
91 if (v.which() != scopes::Variant::Type::Array) {
92 return QVariant();
93@@ -301,7 +324,7 @@
94 QVariant
95 ResultsModel::data(const QModelIndex& index, int role) const
96 {
97- int row = index.row();
98+ const int row = index.row();
99 if (row >= m_results.size())
100 {
101 qWarning() << "ResultsModel::data - invalid index" << row << "size"
102@@ -309,7 +332,7 @@
103 return QVariant();
104 }
105
106- scopes::Result* result = m_results.at(index.row()).get();
107+ scopes::Result* result = m_results.at(row).get();
108
109 switch (role) {
110 case RoleUri:
111@@ -319,11 +342,9 @@
112 case RoleDndUri:
113 return QString::fromStdString(result->dnd_uri());
114 case RoleResult:
115- return QVariant::fromValue(std::static_pointer_cast<unity::scopes::Result>(m_results.at(index.row())));
116- case RoleTitle:
117- return componentValue(result, "title");
118+ return QVariant::fromValue(std::static_pointer_cast<unity::scopes::Result>(m_results.at(row)));
119 case RoleArt: {
120- QString image(componentValue(result, "art").toString());
121+ QString image(componentValue(result, RoleArt).toString());
122 if (image.isEmpty()) {
123 QString uri(QString::fromStdString(result->uri()));
124 // FIXME: figure out a better way and get rid of this, it's an awful hack
125@@ -339,25 +360,24 @@
126 }
127 return image;
128 }
129+ case RoleTitle:
130 case RoleSubtitle:
131- return componentValue(result, "subtitle");
132 case RoleMascot:
133- return componentValue(result, "mascot");
134 case RoleEmblem:
135- return componentValue(result, "emblem");
136+ case RoleSummary:
137+ case RoleOverlayColor:
138+ case RoleQuickPreviewData:
139+ case RoleSocialActions:
140+ return componentValue(result, Roles(role));
141 case RoleAttributes:
142 return attributesValue(result);
143- case RoleSummary:
144- return componentValue(result, "summary");
145 case RoleBackground: {
146- QVariant backgroundVariant(componentValue(result, "background"));
147+ QVariant backgroundVariant(componentValue(result, RoleBackground));
148 if (backgroundVariant.isNull()) {
149 return backgroundVariant;
150 }
151 return backgroundUriToVariant(backgroundVariant.toString());
152 }
153- case RoleOverlayColor:
154- return componentValue(result, "overlay-color");
155 case RoleScopeId:
156 if (result->uri().compare(0, 8, "scope://") == 0) {
157 try {
158@@ -368,10 +388,6 @@
159 }
160 }
161 return QVariant();
162- case RoleQuickPreviewData:
163- return componentValue(result, "quick-preview-data");
164- case RoleSocialActions:
165- return componentValue(result, "social-actions");
166 default:
167 return QVariant();
168 }
169
170=== modified file 'src/Unity/resultsmodel.h'
171--- src/Unity/resultsmodel.h 2016-03-17 16:35:16 +0000
172+++ src/Unity/resultsmodel.h 2016-07-22 13:17:47 +0000
173@@ -73,10 +73,10 @@
174 bool needsPurging() const;
175
176 private:
177- QVariant componentValue(unity::scopes::Result const* result, std::string const& fieldName) const;
178+ QVariant componentValue(unity::scopes::Result const* result, Roles field) const;
179 QVariant attributesValue(unity::scopes::Result const* result) const;
180
181- std::unordered_map<std::string, std::string> m_componentMapping;
182+ QVector<std::string> m_componentMapping;
183 QList<std::shared_ptr<unity::scopes::Result>> m_results;
184 QString m_categoryId;
185 int m_maxAttributes;

Subscribers

People subscribed via source and target branches

to all changes: