[poppler] goo/GooString.cc

Jeff Muizelaar jrmuizel at kemper.freedesktop.org
Sat Nov 3 09:50:48 PDT 2007


 goo/GooString.cc |   30 +++++++++++++++++-------------
 1 file changed, 17 insertions(+), 13 deletions(-)

New commits:
commit 432e657a49cb097638a79e38c141088039572816
Author: Jeff Muizelaar <jeff at freiheit.infidigm.net>
Date:   Mon Sep 17 19:15:21 2007 -0400

    Use realloc/free instead of new/delete when resizing GooStrings
    
    This allows for a large performance improvement when appending a large number
    of characters to a GooString. This is especially helpful for TextOutputDev on
    large PDFs. For example, the following code has the potential to be O(n) instead of
    O(n²) with a good implementation of realloc.
    
    while (n) {
      string.append(character);
      n--;
    }

diff --git a/goo/GooString.cc b/goo/GooString.cc
index 8795856..6817df5 100644
--- a/goo/GooString.cc
+++ b/goo/GooString.cc
@@ -91,22 +91,26 @@ void inline GooString::resize(int newLength) {
 
   if (!s || (roundedSize(length) != roundedSize(newLength))) {
     // requires re-allocating data for string
-    if (newLength < STR_STATIC_SIZE)
-        s1 = sStatic;
-    else
-        s1 = new char[roundedSize(newLength)];
-
-    // we had to re-allocate the memory, so copy the content of previous
-    // buffer into a new buffer
-    if (s) {
+    if (newLength < STR_STATIC_SIZE) {
+      s1 = sStatic;
+    } else {
+      // allocate a rounded amount
+      if (s == sStatic)
+	s1 = (char*)gmalloc(roundedSize(newLength));
+      else
+	s1 = (char*)grealloc(s, roundedSize(newLength));
+    }
+    if (s == sStatic || s1 == sStatic) {
+      // copy the minimum, we only need to if are moving to or
+      // from sStatic.
+      // assert(s != s1) the roundedSize condition ensures this
       if (newLength < length) {
-        memcpy(s1, s, newLength);
+	memcpy(s1, s, newLength);
       } else {
-        memcpy(s1, s, length);
+	memcpy(s1, s, length);
       }
     }
-    if (s != sStatic)
-      delete[] s;
+
   }
 
   s = s1;
@@ -214,7 +218,7 @@ GooString *GooString::formatv(char *fmt, va_list argList) {
 
 GooString::~GooString() {
   if (s != sStatic)
-    delete[] s;
+    free(s);
 }
 
 GooString *GooString::clear() {


More information about the poppler mailing list