[PATCH] KURL optimizations

Waldo Bastian kde-optimize@mail.kde.org
Fri, 10 Jan 2003 00:55:03 +0100


--Boundary-00=_XvgH+IvdMA2rQ1f
Content-Type: text/plain;
  charset="us-ascii"
Content-Transfer-Encoding: 7bit
Content-Disposition: inline

Maks, can you run this patch through your benchmark and see what it does?

Cheers,
Waldo
-- 
bastian@kde.org -=|[ SuSE, The Linux Desktop Experts ]|=- bastian@suse.com

--Boundary-00=_XvgH+IvdMA2rQ1f
Content-Type: text/x-diff;
  charset="us-ascii";
  name="kurl.diff"
Content-Transfer-Encoding: 7bit
Content-Disposition: attachment; filename="kurl.diff"

Index: kurl.cpp
===================================================================
RCS file: /home/kde/kdelibs/kdecore/kurl.cpp,v
retrieving revision 1.226
diff -u -r1.226 kurl.cpp
--- kurl.cpp	18 Dec 2002 15:57:28 -0000	1.226
+++ kurl.cpp	9 Jan 2003 23:53:47 -0000
@@ -37,6 +37,17 @@
 
 #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();
+    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 );
@@ -182,10 +193,14 @@
   if (!textCodec)
       textCodec = QTextCodec::codecForLocale();
 
-  if (!textCodec->canEncode(segment))
-      textCodec = codecForHint( 106 ); // Fall back to utf-8 if it doesn't fit.
-
   QCString csegment = textCodec->fromUnicode(segment);
+  // Check if everything went ok
+  if (textCodec->toUnicode(csegment) != segment)
+  {
+      // Uh oh
+      textCodec = codecForHint( 106 ); // Fall back to utf-8
+      csegment = textCodec->fromUnicode(segment);
+  } 
   old_length = csegment.length();
 
   int new_length = 0;
@@ -544,9 +559,8 @@
   bool badHostName = false;
   int start = 0;
   uint len = _url.length();
-  QChar* buf = new QChar[ len + 1 ];
-  QChar* orig = buf;
-  memcpy( buf, _url.unicode(), len * sizeof( QChar ) );
+  const QChar* buf = _url.unicode();
+  const QChar* orig = buf;
 
   QChar delim;
   QString tmp;
@@ -570,12 +584,12 @@
     goto NodeErr;
   if (buf[pos] == ':' && buf[pos+1] == '/' && buf[pos+2] == '/' )
     {
-      m_strProtocol = QString( orig, pos ).lower();
+      m_strProtocol = QString_lower(QString( orig, pos ));
       pos += 3;
     }
   else if (buf[pos] == ':' && buf[pos+1] == '/' )
     {
-      m_strProtocol = QString( orig, pos ).lower();
+      m_strProtocol = QString_lower(QString( orig, pos ));
       //kdDebug(126)<<"setting protocol to "<<m_strProtocol<<endl;
       pos++;
       start = pos;
@@ -583,7 +597,7 @@
     }
   else if ( buf[pos] == ':' )
     {
-      m_strProtocol = QString( orig, pos ).lower();
+      m_strProtocol = QString_lower(QString( orig, pos ));
       //kdDebug(126)<<"setting protocol to "<<m_strProtocol<<endl;
       pos++;
       start = pos;
@@ -613,7 +627,7 @@
       if (badHostName)
          goto NodeErr;
 
-      m_strHost = decode(QString( buf + start, pos - start ), encoding_hint).lower();
+      m_strHost = QString_lower(decode(QString( buf + start, pos - start ), encoding_hint));
       goto NodeOk;
     }
   if ( x == '@' )
@@ -624,7 +638,7 @@
     }
   /* else if ( x == ':' )
      {
-     m_strHost = decode(QString( buf + start, pos - start ), encoding_hint).lower();
+     m_strHost = QString_lower(decode(QString( buf + start, pos - start ), encoding_hint));
      pos++;
      goto Node8a;
      } */
@@ -633,7 +647,7 @@
       if (badHostName)
          goto NodeErr;
 
-      m_strHost = decode(QString( buf + start, pos - start ), encoding_hint).lower();
+      m_strHost = QString_lower(decode(QString( buf + start, pos - start ), encoding_hint));
       start = pos;
       goto Node9;
     }
@@ -697,7 +711,7 @@
     }
     if (badHostName)
        goto NodeErr;
-    m_strHost = decode(QString( buf + start, pos - start ), encoding_hint).lower();
+    m_strHost = QString_lower(decode(QString( buf + start, pos - start ), encoding_hint));
     if (pos < len) pos++; // Skip ']'
     if (pos == len)
        goto NodeOk;
@@ -720,10 +734,10 @@
        goto NodeErr;
     if ( pos == len )
     {
-       m_strHost = decode(QString( buf + start, pos - start ), encoding_hint).lower();
+       m_strHost = QString_lower(decode(QString( buf + start, pos - start ), encoding_hint));
        goto NodeOk;
     }
-    m_strHost = decode(QString( buf + start, pos - start ), encoding_hint).lower();
+    m_strHost = QString_lower(decode(QString( buf + start, pos - start ), encoding_hint));
   }
   x = buf[pos];
   if ( x == '/' )
@@ -786,14 +800,17 @@
 
  NodeOk:
   //kdDebug(126)<<"parsing finished. m_strProtocol="<<m_strProtocol<<" m_strHost="<<m_strHost<<" m_strPath="<<m_strPath<<endl;
-  delete []orig;
   m_bIsMalformed = false; // Valid URL
-  if (m_strProtocol.isEmpty())
-    m_strProtocol = "file";
 
   //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 == "file")
+  if (m_strProtocol.isEmpty() || (m_strProtocol == "file"))
   {
+#ifdef KDE_QT_ONLY
+    QString fileProt = "file";
+#else
+    static const QString & fileProt = KGlobal::staticQString( "file" );
+#endif
+    m_strProtocol = fileProt;
     if (!m_strHost.isEmpty())
     {
       // File-protocol has a host name..... hmm?
@@ -814,7 +831,6 @@
 
  NodeErr:
 //  kdDebug(126) << "KURL couldn't parse URL \"" << _url << "\"" << endl;
-  delete []orig;
   reset();
   m_strProtocol = _url;
 }
@@ -1071,16 +1087,11 @@
 
 void KURL::setEncodedPath( const QString& _txt, int encoding_hint )
 {
-#ifdef KDE_QT_ONLY
-  QString fileProt = "file";
-#else
-  static const QString & fileProt = KGlobal::staticQString( "file" );
-#endif
   m_strPath_encoded = _txt;
 
   decode( m_strPath_encoded, m_strPath, m_strPath_encoded, encoding_hint );
   // Throw away encoding for local files, makes file-operations faster.
-  if (m_strProtocol == fileProt)
+  if (m_strProtocol == "file")
      m_strPath_encoded = QString::null;
 }
 
@@ -1107,12 +1118,7 @@
 
 bool KURL::isLocalFile() const
 {
-#ifdef KDE_QT_ONLY
-  QString fileProt = "file";
-#else
-  static const QString & fileProt = KGlobal::staticQString( "file" );
-#endif
-  return ( ( m_strProtocol == fileProt ) && ( m_strHost.isEmpty()) && !hasSubURL() );
+  return ( ( m_strProtocol == "file" ) && ( m_strHost.isEmpty()) && !hasSubURL() );
 }
 
 void KURL::setFileEncoding(const QString &encoding)
@@ -1656,7 +1662,14 @@
   if (isEmpty())
     m_bIsMalformed = false;
   if (m_strProtocol.isEmpty())
-    m_strProtocol = "file";
+  {
+#ifdef KDE_QT_ONLY
+    QString fileProt = "file";
+#else
+    static const QString & fileProt = KGlobal::staticQString( "file" );
+#endif
+    m_strProtocol = fileProt;
+  }
   m_strPath = path;
   m_strPath_encoded = QString::null;
 }

--Boundary-00=_XvgH+IvdMA2rQ1f--