Change in kio[master]: Replace QVector with std::vector for Field storage. That ena...

Mark Gaiser (Code Review) noreply at kde.org
Sun Jul 19 20:35:06 UTC 2015


Mark Gaiser has uploaded a new change for review.

  https://gerrit.vesnicky.cesnet.cz/r/475

Change subject: Replace QVector with std::vector for Field storage. That enables more efficient filling of fields via emplace_back and using move semantics where an rvalue was used.
......................................................................

Replace QVector with std::vector for Field storage. That enables more efficient filling of fields via emplace_back and using move semantics where an rvalue was used.

CHANGELOG: Replace QVector with std::vector for improved performance.
Change-Id: I46eca4d26a5235b06e4ccb63ef2a455242377639
---
M src/core/udsentry.cpp
1 file changed, 32 insertions(+), 31 deletions(-)


  git pull ssh://gerrit.vesnicky.cesnet.cz:29418/kio refs/changes/75/475/1

diff --git a/src/core/udsentry.cpp b/src/core/udsentry.cpp
index 4263803..2b08aa4 100644
--- a/src/core/udsentry.cpp
+++ b/src/core/udsentry.cpp
@@ -30,6 +30,7 @@
 #include <KUser>
 
 #include <algorithm>
+#include <vector>
 
 using namespace KIO;
 
@@ -46,13 +47,13 @@
         long long m_long;
     };
 
-    QVector<Field> fields;
+    std::vector<Field> fields;
 
     // Gets the UDS fields and puts them in a vector.
     const QVector<uint> udsFields() const
     {
         QVector<uint> udsVector;
-        foreach (const Field &field, fields) {
+        for (auto &&field : fields) {
             udsVector.push_back(field.m_udsIndex);
         }
 
@@ -62,27 +63,11 @@
     // Returns true if the UDS exists, false otherwise.
     bool contains(uint uds) const
     {
-        auto result = std::find_if(fields.constBegin(), fields.constEnd(), [&uds](const Field &f){
+        auto result = std::find_if(fields.cbegin(), fields.cend(), [&uds](const Field &f){
             return f.m_udsIndex == uds;
         });
 
-        return result != fields.constEnd();
-    }
-
-    void insert(const Field& field)
-    {
-        bool replacedField = false;
-        const uint uds = field.m_udsIndex;
-
-        std::replace_if(fields.begin(), fields.end(), [&uds, &replacedField](const Field &f){
-            replacedField = f.m_udsIndex == uds;
-            return replacedField;
-        }, field);
-
-        // If the field isn't replaced yet, it wasn't found so add it.
-        if (!replacedField) {
-            fields.append(field);
-        }
+        return result != fields.cend();
     }
 
     static void save(QDataStream &, const UDSEntry &);
@@ -141,11 +126,11 @@
 
 QString UDSEntry::stringValue(uint field) const
 {
-    auto result = std::find_if(d->fields.constBegin(), d->fields.constEnd(), [&field](const UDSEntryPrivate::Field &f){
+    auto result = std::find_if(d->fields.cbegin(), d->fields.cend(), [&field](const UDSEntryPrivate::Field &f){
         return f.m_udsIndex == field;
     });
 
-    if (result != d->fields.constEnd()) {
+    if (result != d->fields.cend()) {
         return result->m_str;
     } else {
         return QString();
@@ -154,11 +139,11 @@
 
 long long UDSEntry::numberValue(uint field, long long defaultValue) const
 {
-    auto result = std::find_if(d->fields.constBegin(), d->fields.constEnd(), [&field](const UDSEntryPrivate::Field &f){
+    auto result = std::find_if(d->fields.cbegin(), d->fields.cend(), [&field](const UDSEntryPrivate::Field &f){
         return f.m_udsIndex == field;
     });
 
-    if (result != d->fields.constEnd()) {
+    if (result != d->fields.cend()) {
         return result->m_long;
     } else {
         return defaultValue;
@@ -182,12 +167,28 @@
 
 void UDSEntry::insert(uint field, const QString &value)
 {
-    d->insert(UDSEntryPrivate::Field(field, value));
+    auto result = std::find_if(d->fields.begin(), d->fields.end(), [&field](const UDSEntryPrivate::Field &f){
+        return f.m_udsIndex == field;
+    });
+
+    if (result != d->fields.end()) {
+        result->m_str = value;
+    } else {
+        d->fields.emplace_back(field, value);
+    }
 }
 
 void UDSEntry::insert(uint field, long long value)
 {
-    d->insert(UDSEntryPrivate::Field(field, value));
+    auto result = std::find_if(d->fields.begin(), d->fields.end(), [&field](const UDSEntryPrivate::Field &f){
+        return f.m_udsIndex == field;
+    });
+
+    if (result != d->fields.end()) {
+        result->m_long = value;
+    } else {
+        d->fields.emplace_back(field, value);
+    }
 }
 
 #ifndef KIOCORE_NO_DEPRECATED
@@ -204,7 +205,7 @@
 
 int UDSEntry::count() const
 {
-    return d->fields.count();
+    return d->fields.size();
 }
 
 bool UDSEntry::contains(uint field) const
@@ -231,7 +232,7 @@
 
 void UDSEntryPrivate::save(QDataStream &s, const UDSEntry &a)
 {
-    const QVector<Field> &fields = a.d->fields;
+    const std::vector<Field> &fields = a.d->fields;
     const int size = fields.size();
 
     s << size;
@@ -254,7 +255,7 @@
 {
     a.clear();
 
-    QVector<Field> &fields = a.d->fields;
+    std::vector<Field> &fields = a.d->fields;
 
     quint32 size;
     s >> size;
@@ -283,11 +284,11 @@
                 cachedStrings[i] = buffer;
             }
 
-            fields.append(Field(uds, cachedStrings.at(i)));
+            fields.emplace_back(uds, cachedStrings.at(i));
         } else if (uds & KIO::UDSEntry::UDS_NUMBER) {
             long long value;
             s >> value;
-            fields.append(Field(uds, value));
+            fields.emplace_back(uds, value);
         } else {
             Q_ASSERT_X(false, "KIO::UDSEntry", "Found a field with an invalid type");
         }

-- 
To view, visit https://gerrit.vesnicky.cesnet.cz/r/475
To unsubscribe, visit https://gerrit.vesnicky.cesnet.cz/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I46eca4d26a5235b06e4ccb63ef2a455242377639
Gerrit-PatchSet: 1
Gerrit-Project: kio
Gerrit-Branch: master
Gerrit-Owner: Mark Gaiser <markg85 at gmail.com>


More information about the Kde-frameworks-devel mailing list