[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Bug#319377: libapt-pkg: patches for speedup



Umh, sorry for a _really_ slow reply, apparently I lost or missed your
mail somehow. I only re-found this bug now. Replying in hope that it
is still useful :) And yes, the version number is a typo.

> Did you profile any of these changes separately?  What were the objective
> results from your changes as a whole?

I don't remember profiling those changes separately. The most
objective results I got were callgrind (the Valgrind profiling tool)
reporting less consumed cycles; the less objective results were
aptitude running faster on my P100 (and under callgrind). All of the
optimizations I did were to places pointed as bottlenecks by callgrind
profiling and all of my optimizations did result in a speedup.

>Integral_ mdz: yes, some of the idioms there are weird. I'd also think
>    that insert(s.end(), s2.begin(), s2.end()) should be the same as s
>    += s2.
>Integral_ mdz: I think he changed += to push_back in some places
>    because he changed a string to a vector
>Integral_ which seems pointless to me
>Integral_ unless the g++ implementation of std::string is truly
>    atrocious

Hmm. I seem to remember that appending to a string once did take time
linear to both the strings, however this doesn't seem to be the case
anymore.

I wrote a small program (attached) to illustrate the effect of the
changes I made to QuoteString. Here's some results on my computer:

$ /usr/bin/time ./test_QuoteString
1.99user 0.00system 0:02.03elapsed 98%CPU (0avgtext+0avgdata 0maxresident)k
0inputs+0outputs (0major+299minor)pagefaults 0swaps

[change the program to use QuoteString_vec instead of QuoteString_str]

$ /usr/bin/time ./test_QuoteString
1.39user 0.00system 0:01.49elapsed 93%CPU (0avgtext+0avgdata 0maxresident)k
0inputs+0outputs (0major+299minor)pagefaults 0swaps

So using a vector resulted in a 30% speedup. I also claim that
QuoteString was a place where it made difference (or actually see the
QuoteURI I made, an even more optimized version for URIs that makes
more of a difference).

	Sami
#include <string>
#include <vector>

using namespace std;

string QuoteString_str(const string &Str, const char *Bad)
{
   string Res;
   for (string::const_iterator I = Str.begin(); I != Str.end(); I++)
   {
      if (strchr(Bad,*I) != 0 || isprint(*I) == 0 ||
          *I <= 0x20 || *I >= 0x7F)
      {
         char Buf[10];
         sprintf(Buf,"%%%02x",(int)*I);
         Res += Buf;
      }
      else
         Res += *I;
   }
   return Res;
}

string QuoteString_vec(const string &Str, const char *Bad)
{
   vector<char> Res;
   Res.reserve(Str.length()*3/2);
   for (string::const_iterator I = Str.begin(); I != Str.end(); I++)
   {
      if (strchr(Bad,*I) != 0 || isprint(*I) == 0 ||
          *I <= 0x20 || *I >= 0x7F)
      {
 	 char Buf[10];
 	 sprintf(Buf,"%%%02x",(int)*I);
	 copy(Buf,Buf+3,back_insert_iterator<vector<char> >(Res));
      }
      else
	 Res.push_back(*I);
   }
   return string(Res.begin(),Res.end());
}

int main(void) {
   for (int s=0; s<500000; s++) {
      QuoteString_str("http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=319377";,
 		      "\\|{}[]<>\"^~_=!@#$%^&*");
//       QuoteString_vec("http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=319377";,
//  		      "\\|{}[]<>\"^~_=!@#$%^&*");
   }
}

Attachment: signature.asc
Description: Digital signature


Reply to: