Re: QCString construction

David Faure <[email protected]> Sat, 10 Feb 2007 02:16:25 +0100
Newsgroups gmane.comp.kde.devel.optimize,gmane.comp.kde.devel.kmail
Organization Klaralvdalens Datakonsult.
Message-ID <[email protected]>
--Boundary-00=_vzRzFF6OZnVFaHZ
Content-Type: text/plain;
  charset="utf-8"
Content-Transfer-Encoding: quoted-printable
Content-Disposition: inline

This leads me to 3 patches.

One for kmail, I wrote a utility function for creating a QCString from char=
*+size, and used that
when creating a QCString from a DwString where it matters (i.e. where I fou=
nd pretty large strings
to be used when attaching large files).
Can a kmail developer review the change to KMMessage::asString() and asSend=
ableString(), too?
It avoids a asString() (Assemble) and a fromString (Parse), but I hope it's=
 doing the right thing.

One for Qt itself: why does QCString use the slow way when duplicating QCSt=
rings?
(I know that QCString tmp( s1 ); is shallow copy, but the next line calls o=
perator+=3D which calls detach)
Unless someone spots a flaw in this patch I'll be posting it to TT (and I'm=
 testing my local kde with it
starting from tomorrow ;)

And one for mimelib, not really related to QCString: am I right that memcpy=
 is faster than for (i=3D0; i < pos1; ++i) *to++ =3D *from++; ?
At least in gdb it seems faster, but I didn't benchmark it. Hopefully memcp=
y is done by a single CPU instruction or something, right?

=46inally, I wish we could avoid the data copying just to null-terminate it=
 in KMMessagePart::body(), but I'm not sure how...

=2D-=20
David Faure, [email protected], [email protected]
KDE/KOffice developer, Qt consultancy projects
Klar=C3=A4lvdalens Datakonsult AB, Platform-independent software solutions

--Boundary-00=_vzRzFF6OZnVFaHZ
Content-Type: text/x-diff;
  charset="utf-8";
  name="qcstring.h.diff"
Content-Transfer-Encoding: 7bit
Content-Disposition: attachment;
	filename="qcstring.h.diff"

Index: qcstring.h
===================================================================
--- qcstring.h	(revision 615855)
+++ qcstring.h	(working copy)
@@ -352,14 +352,14 @@ Q_EXPORT inline bool operator>=( const c
 Q_EXPORT inline const QCString operator+( const QCString &s1,
 					  const QCString &s2 )
 {
-    QCString tmp( s1.data() );
+    QCString tmp( s1 );
     tmp += s2;
     return tmp;
 }
 
 Q_EXPORT inline const QCString operator+( const QCString &s1, const char *s2 )
 {
-    QCString tmp( s1.data() );
+    QCString tmp( s1 );
     tmp += s2;
     return tmp;
 }
@@ -373,7 +373,7 @@ Q_EXPORT inline const QCString operator+
 
 Q_EXPORT inline const QCString operator+( const QCString &s1, char c2 )
 {
-    QCString tmp( s1.data() );
+    QCString tmp( s1 );
     tmp += c2;
     return tmp;
 }

--Boundary-00=_vzRzFF6OZnVFaHZ
Content-Type: text/x-diff;
  charset="utf-8";
  name="dwstring.cpp.diff"
Content-Transfer-Encoding: 7bit
Content-Disposition: attachment;
	filename="dwstring.cpp.diff"

Index: dwstring.cpp
===================================================================
--- dwstring.cpp	(revision 631586)
+++ dwstring.cpp	(working copy)
@@ -1276,12 +1276,12 @@ void DwString::_replace(size_t aPos1, si
         assert(newBuf != 0);
         if (newBuf != 0) {
             to = newBuf;
-            from = mRep->mBuffer + mStart;
-            for (i=0; i < pos1; ++i) *to++ = *from++;
-            from = aBuf;
-            for (i=0; i < len2; ++i) *to++ = *from++;
-            from = mRep->mBuffer + mStart + pos1 + len1;
-            for (i=0; i < mLength - pos1 - len1; ++i) *to++ = *from++;
+            memcpy(to, mRep->mBuffer + mStart, pos1);
+            to += pos1;
+            memcpy(to, aBuf, len2);
+            to += len2;
+            memcpy(to, mRep->mBuffer + mStart + pos1 + len1, mLength - pos1 - len1);
+            to += mLength - pos1 - len1;
             *to = 0;
             DwStringRep* rep = new DwStringRep(newBuf, size);
             assert(rep != 0);

--Boundary-00=_vzRzFF6OZnVFaHZ
Content-Type: text/x-diff;
  charset="utf-8";
  name="kmail.diff"
Content-Transfer-Encoding: 7bit
Content-Disposition: attachment;
	filename="kmail.diff"

Index: kmmessage.cpp
===================================================================
--- kmmessage.cpp	(revision 631646)
+++ kmmessage.cpp	(working copy)
@@ -64,6 +64,7 @@ using namespace KMime::Types;
 #include <klocale.h>
 #include <stdlib.h>
 #include <unistd.h>
+#include "util.h"
 
 #if ALLOW_GUI
 #include <kmessagebox.h>
@@ -306,14 +307,15 @@ const DwMessage *KMMessage::asDwMessage(
 
 //-----------------------------------------------------------------------------
 QCString KMMessage::asString() const {
-  return asDwString().c_str();
+  const DwString& asString = asDwString();
+  return KMail::Util::CString( asString.c_str(), asString.length() );
 }
 
 
 QCString KMMessage::asSendableString() const
 {
-  KMMessage msg;
-  msg.fromString(asString());
+  KMMessage msg( *this );
+  //msg.fromString(asString());
   msg.removePrivateHeaderFields();
   msg.removeHeaderField("Bcc");
   return msg.asString();
@@ -321,8 +323,8 @@ QCString KMMessage::asSendableString() c
 
 QCString KMMessage::headerAsSendableString() const
 {
-  KMMessage msg;
-  msg.fromString(asString());
+  KMMessage msg( *this );
+  //msg.fromString(asString());
   msg.removePrivateHeaderFields();
   msg.removeHeaderField("Bcc");
   return msg.headerAsString().latin1();
@@ -2469,8 +2471,8 @@ void KMMessage::setNeedsAssembly()
 //-----------------------------------------------------------------------------
 QCString KMMessage::body() const
 {
-  DwString body = mMsg->Body().AsString();
-  QCString str = body.c_str();
+  const DwString& body = mMsg->Body().AsString();
+  QCString str = KMail::Util::CString( body.c_str(), body.length() );
   kdWarning( str.length() != body.length(), 5006 )
     << "KMMessage::body(): body is binary but used as text!" << endl;
   return str;
@@ -3135,8 +3137,9 @@ DwBodyPart* KMMessage::createDWBodyPart(
   if (!contDisp.isEmpty())
     headers.ContentDisposition().FromString(contDisp);
 
-  if (!aPart->body().isNull())
-    part->Body().FromString(aPart->body());
+  const QCString bodyStr = aPart->body();
+  if (!bodyStr.isNull())
+    part->Body().FromString(bodyStr);
   else
     part->Body().FromString("");
 
Index: partNode.cpp
===================================================================
--- partNode.cpp	(revision 631646)
+++ partNode.cpp	(working copy)
@@ -37,6 +37,7 @@
 #include <mimelib/utility.h>
 #include <qregexp.h>
 #include <kasciistricmp.h>
+#include "util.h"
 
 /*
   ===========================================================================
@@ -178,8 +179,10 @@ const QCString & partNode::encodedBody()
   if ( mEncodedOk )
     return mEncodedBody;
 
-  if ( mDwPart )
-    mEncodedBody = mDwPart->Body().AsString().c_str();
+  if ( mDwPart ) {
+    const DwString& asString = mDwPart->Body().AsString();
+    mEncodedBody = KMail::Util::CString( asString.c_str(), asString.length() );
+  }
   else
     mEncodedBody = 0;
   mEncodedOk = true;
Index: messagecomposer.cpp
===================================================================
--- messagecomposer.cpp	(revision 631646)
+++ messagecomposer.cpp	(working copy)
@@ -1577,12 +1577,14 @@ void MessageComposer::composeMessage( KM
       DwMediaType& ct = headers.ContentType();
       ct.SetBoundary(mSaveBoundary);
       dwPart->Assemble();
-      mEncodedBody = dwPart->AsString().c_str();
+      const DwString& asString = dwPart->AsString();
+      mEncodedBody = KMail::Util::CString( asString.c_str(), asString.length() );
     }
     else {
       dwPart = theMessage.createDWBodyPart( &mOldBodyPart );
       dwPart->Assemble();
-      mEncodedBody = dwPart->AsString().c_str();
+      const DwString& asString = dwPart->AsString();
+      mEncodedBody = KMail::Util::CString( asString.c_str(), asString.length() );
     }
     delete dwPart;
     dwPart = 0;
@@ -1784,7 +1786,8 @@ void MessageComposer::addBodyAndAttachme
 
       DwBodyPart* innerDwPart = msg->createDWBodyPart( it->part );
       innerDwPart->Assemble();
-      QCString encodedAttachment = innerDwPart->AsString().c_str();
+      const DwString& asString = innerDwPart->AsString();
+      QCString encodedAttachment = KMail::Util::CString( asString.c_str(), asString.length() );
       delete innerDwPart;
       innerDwPart = 0;
 
@@ -1815,7 +1818,8 @@ void MessageComposer::addBodyAndAttachme
               rEncryptMessagePart = newAttachPart;
               DwBodyPart* dwPart = msg->createDWBodyPart( &newAttachPart );
               dwPart->Assemble();
-              encodedAttachment = dwPart->AsString().c_str();
+              const DwString& asString = dwPart->AsString();
+              encodedAttachment = KMail::Util::CString( asString.c_str(), asString.length() );
               delete dwPart;
               dwPart = 0;
             }
Index: util.h
===================================================================
--- util.h	(revision 631646)
+++ util.h	(working copy)
@@ -68,6 +68,17 @@ namespace Util {
     QCString lf2crlf( const QCString & src );
 
     /**
+     * Construct a QCString from a null-terminated char* and a size (number of characters, not including trailing '\0')
+     */
+    QCString CString( const char* str, size_t strLen );
+
+    inline QCString CString( const char* str, size_t strLen ) {
+      QCString cstr( strLen + 1 );
+      memcpy( cstr.data(), str, strLen + 1 );
+      return cstr;
+    }
+
+    /**
      * A LaterDeleter is intended to be used with the RAII ( Resource
      * Acquisiation is Initialization ) paradigm. When an instance of it
      * goes out of scope it deletes the associated object  It can be 

--Boundary-00=_vzRzFF6OZnVFaHZ
Content-Type: text/plain; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: inline

_______________________________________________
Kde-optimize mailing list
[email protected]
https://mail.kde.org/mailman/listinfo/kde-optimize

--Boundary-00=_vzRzFF6OZnVFaHZ--