KDE/kdelibs/kdecore

David Faure faure at kde.org
Tue Jan 6 11:49:53 CET 2009


SVN commit 906455 by dfaure:

25% speedup in KMimeType::is(), inspired by patch by JakubS
CCMAIL: kde-optimize at kde.org


 M  +5 -3      services/kmimetype.cpp  
 M  +1 -1      services/kmimetype_p.h  
 M  +1 -1      services/kmimetypefactory.cpp  
 M  +5 -4      services/kmimetypefactory.h  
 M  +28 -0     tests/kmimetypetest.cpp  
 M  +1 -0      tests/kmimetypetest.h  


--- trunk/KDE/kdelibs/kdecore/services/kmimetype.cpp #906454:906455
@@ -597,9 +597,9 @@
     return QString();
 }
 
-bool KMimeTypePrivate::inherits(KMimeType::Ptr mime) const
+bool KMimeTypePrivate::inherits(const QString& mime) const
 {
-    if (mime && m_strName == mime->d_func()->m_strName) {
+    if (m_strName == mime) {
         return true;
     }
     foreach( const QString& parent, parentMimeTypes() ) {
@@ -617,7 +617,9 @@
     Q_D(const KMimeType);
     if (name() == mimeTypeName)
         return true;
-    KMimeType::Ptr mime = KMimeTypeFactory::self()->findMimeTypeByName(mimeTypeName, KMimeType::ResolveAliases);
+    QString mime = KMimeTypeFactory::self()->resolveAlias(mimeTypeName);
+    if (mime.isEmpty())
+        mime = mimeTypeName;
     return d->inherits(mime);
 }
 
--- trunk/KDE/kdelibs/kdecore/services/kmimetype_p.h #906454:906455
@@ -49,7 +49,7 @@
     return icon;
   }
 
-    bool inherits(KMimeType::Ptr mime) const;
+    bool inherits(const QString& mime) const;
     QString fallbackParent() const;
     QStringList parentMimeTypes() const;
     void collectParentMimeTypes(QStringList&) const;
--- trunk/KDE/kdelibs/kdecore/services/kmimetypefactory.cpp #906454:906455
@@ -149,7 +149,7 @@
 }
 
 
-QString KMimeTypeFactory::resolveAlias(const QString& mime)
+QString KMimeTypeFactory::resolveAlias(const QString& mime) const
 {
     return m_aliases.value(mime);
 }
--- trunk/KDE/kdelibs/kdecore/services/kmimetypefactory.h #906454:906455
@@ -61,14 +61,15 @@
      */
     virtual KMimeType::Ptr findMimeTypeByName(const QString &_name, KMimeType::FindByNameOption options = KMimeType::DontResolveAlias);
 
-private: // only for KMimeType
-    friend class KMimeType;
-    friend class KMimeFileParserTest;
     /**
      * Check if mime is an alias, and return the canonical name for it if it is.
      */
-    QString resolveAlias(const QString& mime);
+    QString resolveAlias(const QString& mime) const;
 
+private: // only for KMimeType
+    friend class KMimeType;
+    friend class KMimeFileParserTest;
+
     /**
      * Find a mimetype from a filename (using the pattern list)
      * @param filename filename to check.
--- trunk/KDE/kdelibs/kdecore/tests/kmimetypetest.cpp #906454:906455
@@ -516,6 +516,34 @@
     QVERIFY(mrml->is("application/octet-stream"));
 }
 
+void KMimeTypeTest::testMimeTypeInheritancePerformance()
+{
+    // Check performance of is(). In kde3 the list of mimetypes with previews had 63 items...
+    // We could get it with KServiceTypeTrader::self()->query("ThumbCreator") and the "MimeTypes"
+    // property, but this would give variable results and requires other modules installed.
+    QStringList mimeTypes; mimeTypes << "image/jpeg" << "image/png" << "image/tiff" << "text/plain" << "text/html";
+    mimeTypes += mimeTypes;
+    mimeTypes += mimeTypes;
+    mimeTypes += mimeTypes;
+    QCOMPARE(mimeTypes.count(), 40);
+    KMimeType::Ptr mime = KMimeType::mimeType("text/x-chdr");
+    QVERIFY(mime);
+    QTime dt; dt.start();
+    const int numIterations = 2000;
+    for (int i = 0; i < numIterations; ++i) {
+        QString match;
+        foreach (const QString& mt, mimeTypes) {
+            if (mime->is(mt)) {
+                match = mt;
+                // of course there would normally be a "break" here, but we're testing worse-case
+                // performance here
+            }
+        }
+        QCOMPARE(match, QString("text/plain"));
+    }
+    qDebug() << "Calling is()" << mimeTypes.count() * numIterations << "times took" << dt.elapsed() << "msecs";
+}
+
 // Helper method for all the trader tests
 static bool offerListHasService( const KService::List& offers,
                                  const QString& entryPath )
--- trunk/KDE/kdelibs/kdecore/tests/kmimetypetest.h #906454:906455
@@ -43,6 +43,7 @@
     void testAllMimeTypes();
     void testAlias();
     void testMimeTypeParent();
+    void testMimeTypeInheritancePerformance();
     void testMimeTypeTraderForTextPlain();
     void testMimeTypeTraderForDerivedMimeType();
     void testMimeTypeTraderForAlias();


More information about the Kde-optimize mailing list