[pan2: 58/68] Reserve space for read entries to improve performance.



commit 4d835258823a72844560429ca22135519fb5e297
Author: K. Haley <haleykd users sf net>
Date:   Sun Sep 5 16:01:44 2010 -0600

    Reserve space for read entries to improve performance.

 pan/usenet-utils/numbers.cc |   14 ++++++++------
 pan/usenet-utils/numbers.h  |    2 +-
 2 files changed, 9 insertions(+), 7 deletions(-)
---
diff --git a/pan/usenet-utils/numbers.cc b/pan/usenet-utils/numbers.cc
index a521149..5ca857f 100644
--- a/pan/usenet-utils/numbers.cc
+++ b/pan/usenet-utils/numbers.cc
@@ -22,7 +22,6 @@
 #include <cstdio> // snprintf
 #include <algorithm>
 #include <glib.h>
-#include <pan/general/string-view.h>
 #include "numbers.h"
 
 using namespace pan;
@@ -258,19 +257,22 @@ Numbers :: to_string () const
 void
 Numbers :: to_string (std::string & str) const
 {
+   std::string temp;
+   int bytes;
    char buf[64];
 
+   temp.reserve(24 * _marked.size() );
    for (r_cit it=_marked.begin(), end=_marked.end(); it!=end; ++it)
    {
       Range r (*it);
 
       if (r.low == r.high)
-        g_snprintf (buf, sizeof(buf), "%"G_GUINT64_FORMAT",", r.low);
+        bytes = g_snprintf (buf, sizeof(buf), "%"G_GUINT64_FORMAT",", r.low);
       else
-         g_snprintf (buf, sizeof(buf), "%"G_GUINT64_FORMAT"-%"G_GUINT64_FORMAT",", r.low, r.high);
-      str += buf;
+         bytes = g_snprintf (buf, sizeof(buf), "%"G_GUINT64_FORMAT"-%"G_GUINT64_FORMAT",", r.low, r.high);
+      temp.append(buf, bytes);
    }
 
-   if (!str.empty())
-      str.erase (--str.end()); // remove final comma
+   if (!temp.empty())
+      str.append(temp.begin(), --temp.end()); // remove final comma
 }
diff --git a/pan/usenet-utils/numbers.h b/pan/usenet-utils/numbers.h
index d2b0588..aba810d 100644
--- a/pan/usenet-utils/numbers.h
+++ b/pan/usenet-utils/numbers.h
@@ -73,7 +73,7 @@ namespace pan
       bool operator== (const Numbers& that) const {
         return _marked == that._marked;
       }
-      Numbers () { }
+      Numbers () { _marked.reserve(16);}
       ~Numbers () { }
 
     public:



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