[upd alt PATCH] Re: [PATCH] KURL optimizations

Roger Larsson kde-optimize@mail.kde.org
Sat, 11 Jan 2003 10:34:26 +0100


--Boundary-00=_iU+H+XHjdDaoOas
Content-Type: text/plain;
  charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable
Content-Disposition: inline

My patch again, rolls back the QString_lower changes and
add some other optimizations.

I am not sure that removing KGlobal::staticQString fileProt is a win.
But it should be faster at least the first time...
fileProt should probably be a static const in the class...

Maks, could you benchmark this?

Of cause it is possible to keep the QString_lower optimization, but why not=
=20
make the argument and result non const?

/RogerL

=2D-=20
Roger Larsson
Skellefte=E5
Sweden

--Boundary-00=_iU+H+XHjdDaoOas
Content-Type: text/x-diff;
  charset="iso-8859-1";
  name="new-kurl.patch"
Content-Transfer-Encoding: 7bit
Content-Disposition: attachment; filename="new-kurl.patch"

Index: kurl.cpp
===================================================================
RCS file: /home/kde/kdelibs/kdecore/kurl.cpp,v
retrieving revision 1.227
diff -u -3 -p -r1.227 kurl.cpp
--- kurl.cpp	10 Jan 2003 14:33:09 -0000	1.227
+++ kurl.cpp	11 Jan 2003 09:16:04 -0000
@@ -37,18 +37,6 @@
 
 #include <qtextcodec.h>
 
-// WARNING
-// This function CHANGES the argument even though it is const!!!
-static const QString &QString_lower(const QString &s)
-{
-    uint l = s.length();
-    const_cast<QString &>(s).setLength(l); // Force a detach
-    QChar *u = const_cast<QChar *>(s.unicode());
-    for(;l--;u++)
-       *u = u->lower();
-    return s;
-}
-
 static QTextCodec * codecForHint( int encoding_hint /* not 0 ! */ )
 {
     return QTextCodec::codecForMib( encoding_hint );
@@ -545,6 +533,13 @@ bool KURL::isEmpty() const
   return (m_strPath.isEmpty() && m_strProtocol.isEmpty());
 }
 
+//#ifdef KDE_QT_ONLY
+    const QString fileProt = "file";
+//#else
+//    I am afraid that this is not a optimization... I try it this way...
+//    static const QString & fileProt = KGlobal::staticQString( "file" );
+//#endif
+
 void KURL::parse( const QString& _url, int encoding_hint )
 {
   //kdDebug(126) << "parse " << _url << endl;
@@ -585,12 +580,12 @@ void KURL::parse( const QString& _url, i
     goto NodeErr;
   if (buf[pos] == ':' && buf[pos+1] == '/' && buf[pos+2] == '/' )
     {
-      m_strProtocol = QString_lower(QString( orig, pos ));
+      m_strProtocol = QString( orig, pos ).lower();
       pos += 3;
     }
   else if (buf[pos] == ':' && buf[pos+1] == '/' )
     {
-      m_strProtocol = QString_lower(QString( orig, pos ));
+      m_strProtocol = QString( orig, pos ).lower();
       //kdDebug(126)<<"setting protocol to "<<m_strProtocol<<endl;
       pos++;
       start = pos;
@@ -598,7 +593,7 @@ void KURL::parse( const QString& _url, i
     }
   else if ( buf[pos] == ':' )
     {
-      m_strProtocol = QString_lower(QString( orig, pos ));
+      m_strProtocol = QString( orig, pos ).lower();
       //kdDebug(126)<<"setting protocol to "<<m_strProtocol<<endl;
       pos++;
       start = pos;
@@ -628,7 +623,7 @@ void KURL::parse( const QString& _url, i
       if (badHostName)
          goto NodeErr;
 
-      m_strHost = QString_lower(decode(QString( buf + start, pos - start ), encoding_hint));
+      m_strHost = decode(QString( buf + start, pos - start ), encoding_hint).lower();
       goto NodeOk;
     }
   if ( x == '@' )
@@ -639,7 +634,7 @@ void KURL::parse( const QString& _url, i
     }
   /* else if ( x == ':' )
      {
-     m_strHost = QString_lower(decode(QString( buf + start, pos - start ), encoding_hint));
+     m_strHost = decode(QString( buf + start, pos - start ), encoding_hint).lower();
      pos++;
      goto Node8a;
      } */
@@ -648,7 +643,7 @@ void KURL::parse( const QString& _url, i
       if (badHostName)
          goto NodeErr;
 
-      m_strHost = QString_lower(decode(QString( buf + start, pos - start ), encoding_hint));
+      m_strHost = decode(QString( buf + start, pos - start ), encoding_hint).lower();
       start = pos;
       goto Node9;
     }
@@ -712,7 +707,7 @@ void KURL::parse( const QString& _url, i
     }
     if (badHostName)
        goto NodeErr;
-    m_strHost = QString_lower(decode(QString( buf + start, pos - start ), encoding_hint));
+    m_strHost = decode(QString( buf + start, pos - start ), encoding_hint).lower();
     if (pos < len) pos++; // Skip ']'
     if (pos == len)
        goto NodeOk;
@@ -735,10 +730,10 @@ void KURL::parse( const QString& _url, i
        goto NodeErr;
     if ( pos == len )
     {
-       m_strHost = QString_lower(decode(QString( buf + start, pos - start ), encoding_hint));
+       m_strHost = decode(QString( buf + start, pos - start ), encoding_hint).lower();
        goto NodeOk;
     }
-    m_strHost = QString_lower(decode(QString( buf + start, pos - start ), encoding_hint));
+    m_strHost = decode(QString( buf + start, pos - start ), encoding_hint).lower();
   }
   x = buf[pos];
   if ( x == '/' )
@@ -804,14 +799,12 @@ void KURL::parse( const QString& _url, i
   m_bIsMalformed = false; // Valid URL
 
   //kdDebug()<<"Prot="<<m_strProtocol<<"\nUser="<<m_strUser<<"\nPass="<<m_strPass<<"\nHost="<<m_strHost<<"\nPath="<<m_strPath<<"\nQuery="<<m_strQuery_encoded<<"\nRef="<<m_strRef_encoded<<"\nPort="<<m_iPort<<endl;
-  if (m_strProtocol.isEmpty() || (m_strProtocol == "file"))
+  if (m_strProtocol.isEmpty() || (m_strProtocol == fileProt))
   {
-#ifdef KDE_QT_ONLY
-    QString fileProt = "file";
-#else
-    static const QString & fileProt = KGlobal::staticQString( "file" );
-#endif
-    m_strProtocol = fileProt;
+    // Assign even if it was equal to fileProt - why?
+    // Now it is the same object too! Later tests will compare fast 
+    // (comparing with a 'char *' will always check each character)
+    m_strProtocol = fileProt; 
     if (!m_strHost.isEmpty())
     {
       // File-protocol has a host name..... hmm?
@@ -1119,7 +1112,7 @@ QString KURL::path( int _trailing ) cons
 
 bool KURL::isLocalFile() const
 {
-  return ( ( m_strProtocol == "file" ) && ( m_strHost.isEmpty()) && !hasSubURL() );
+  return ( ( m_strProtocol == fileProt ) && ( m_strHost.isEmpty()) && !hasSubURL() );
 }
 
 void KURL::setFileEncoding(const QString &encoding)
@@ -1518,7 +1511,7 @@ bool KURL::cd( const QString& _dir )
   }
 
   // Users home directory on the local disk ?
-  if ( ( _dir[0] == '~' ) && ( m_strProtocol == "file" ))
+  if ( ( _dir[0] == '~' ) && ( m_strProtocol == fileProt ))
   {
     m_strPath_encoded = QString::null;
     m_strPath = QDir::homeDirPath();
@@ -1664,11 +1657,6 @@ void KURL::setPath( const QString & path
     m_bIsMalformed = false;
   if (m_strProtocol.isEmpty())
   {
-#ifdef KDE_QT_ONLY
-    QString fileProt = "file";
-#else
-    static const QString & fileProt = KGlobal::staticQString( "file" );
-#endif
     m_strProtocol = fileProt;
   }
   m_strPath = path;

--Boundary-00=_iU+H+XHjdDaoOas--