<html>
    <head>
      <base href="https://bugs.freedesktop.org/" />
    </head>
    <body><table border="1" cellspacing="0" cellpadding="8">
        <tr>
          <th>Bug ID</th>
          <td><a class="bz_bug_link 
          bz_status_NEW "
   title="NEW - [patch] reduce use of gmalloc() in GooString::appendfv()"
   href="https://bugs.freedesktop.org/show_bug.cgi?id=89096">89096</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>[patch] reduce use of gmalloc() in GooString::appendfv()
          </td>
        </tr>

        <tr>
          <th>Product</th>
          <td>poppler
          </td>
        </tr>

        <tr>
          <th>Version</th>
          <td>unspecified
          </td>
        </tr>

        <tr>
          <th>Hardware</th>
          <td>Other
          </td>
        </tr>

        <tr>
          <th>OS</th>
          <td>All
          </td>
        </tr>

        <tr>
          <th>Status</th>
          <td>NEW
          </td>
        </tr>

        <tr>
          <th>Severity</th>
          <td>normal
          </td>
        </tr>

        <tr>
          <th>Priority</th>
          <td>medium
          </td>
        </tr>

        <tr>
          <th>Component</th>
          <td>general
          </td>
        </tr>

        <tr>
          <th>Assignee</th>
          <td>poppler-bugs@lists.freedesktop.org
          </td>
        </tr>

        <tr>
          <th>Reporter</th>
          <td>williambader@hotmail.com
          </td>
        </tr></table>
      <p>
        <div>
        <pre>Created <span class=""><a href="attachment.cgi?id=113383" name="attach_113383" title="patch to GooString::appendfv()">attachment 113383</a> <a href="attachment.cgi?id=113383&action=edit" title="patch to GooString::appendfv()">[details]</a></span>
patch to GooString::appendfv()

In the test file in <a class="bz_bug_link 
          bz_status_RESOLVED  bz_closed"
   title="RESOLVED FIXED - [patch] fix "length bigger than vheaTab size" and "length bigger than vmtxTab size""
   href="show_bug.cgi?id=89076">https://bugs.freedesktop.org/show_bug.cgi?id=89076</a> pdftops
spends the most time in GooString::formatDouble() and GooString::appendfv().
GooString::appendfv() calls gmallocn() to allocate a small buffer of arguments
to print each time that it is called.
pdftops can call it millions of times on some PDFs.
These patches change appendfv() to use a local buffer with 8 items (the same as
it used to allocate).  It now allocates a buffer only if it has more than 8
items.  I tested the change by setting the initial size to 1 and running a file
with valgrind to exercise cases that require multiple reallocations.
I have profiles before and after showing that the number of calls to gmallocn()
was reduced from almost 11M to 4.8M and the total time spent inside appendfv()
and gmallocn() was reduced.

pdftops before the patch (total from 3 runs of "pdftops
2-DESCR_648853-IT-EN-DE_MANITOU_RIGENERATO.pdf x.ps")
  %   cumulative   self              self     total
 time   seconds   seconds    calls  ms/call  ms/call  name
 15.25      0.93     0.93  8905797     0.00     0.00 
GooString::formatDouble(double, char*, int, int, bool, char**, int*)
  9.34      1.50     0.57  6172503     0.00     0.00  GooString::appendfv(char
const*, __va_list_tag*)
  7.87      1.98     0.48 34515024     0.00     0.00 
FlateStream::getHuffmanCodeWord(FlateHuffmanTab*)
  7.05      2.41     0.43 22730064     0.00     0.00  FlateStream::readSome()
  6.48      2.81     0.40 18514623     0.00     0.00  Lexer::getObj(Object*,
int)
  5.90      3.17     0.36 16607355     0.00     0.00  Parser::getObj(Object*,
bool, unsigned char*, CryptAlgorithm, int, int, int, int, bool)
  5.90      3.53     0.36 106901274     0.00     0.00 
JBIG2Segment::~JBIG2Segment()
  4.92      3.83     0.30 111594801     0.00     0.00  FlateStream::getChar()
  4.34      4.09     0.27 38266749     0.00     0.00  Object::free()
  3.69      4.32     0.23 87202734     0.00     0.00  Lexer::lookChar()
  3.61      4.54     0.22 19199391     0.00     0.00  GooString::append(char
const*, int)
  2.79      4.71     0.17      438     0.39    12.47  Gfx::go(bool)
  2.62      4.87     0.16    38745     0.00     0.00 
FlateStream::compHuffmanCodes(int*, int, FlateHuffmanTab*)
  1.39      4.95     0.09 10967463     0.00     0.00  gmallocn

pdftops after GooString, appendfv reduced 0.57s to 0.50s, gmalloc reduced 0.09s
to 0.02s,
 13.07      0.75     0.75  8905797     0.00     0.00 
GooString::formatDouble(double, char*, int, int, bool, char**, int*)
  8.77      1.25     0.50  6172503     0.00     0.00  GooString::appendfv(char
const*, __va_list_tag*)
  8.60      1.74     0.49 22730064     0.00     0.00  FlateStream::readSome()
  8.07      2.20     0.46 18514623     0.00     0.00  Lexer::getObj(Object*,
int)
  7.19      2.61     0.41 111594801     0.00     0.00  FlateStream::getChar()
...
  0.35      5.14     0.02  4794960     0.00     0.00  gmallocn</pre>
        </div>
      </p>
      <hr>
      <span>You are receiving this mail because:</span>
      
      <ul>
          <li>You are the assignee for the bug.</li>
      </ul>
    </body>
</html>