Performance

- Found some inefficient string initialization in Table.cpp, report.cpp,
  and in switching over to using more std::string capabilities, realized
  a 25% boost in Table::render speed.
- Eliminated Table::suppressWS.
- Eliminated Table::clean.
This commit is contained in:
Paul Beckingham 2010-07-06 01:37:35 -04:00
parent 25db00e97d
commit ad9c89b9fb
3 changed files with 41 additions and 147 deletions

View file

@ -63,7 +63,6 @@ Table::Table ()
, mDashedUnderline (false)
, mTablePadding (0)
, mTableWidth (0)
, mSuppressWS (false)
{
}
@ -180,32 +179,6 @@ void Table::addCell (const int row, const int col, const std::string& data)
{
unsigned int length = 0;
if (mSuppressWS)
{
std::string data2;
if (mCommify.find (col) != mCommify.end ())
data2 = commify (data);
else
data2 = data;
clean (data2);
// For multi-line cells, find the longest line.
if (data2.find ("\n") != std::string::npos)
{
length = 0;
std::vector <std::string> lines;
split (lines, data2, "\n");
for (unsigned int i = 0; i < lines.size (); ++i)
if (lines[i].length () > length)
length = lines[i].length ();
}
else
length = data2.length ();
mData.add (row, col, data2);
}
else
{
if (mCommify.find (col) != mCommify.end ())
mData.add (row, col, commify (data));
else
@ -223,7 +196,6 @@ void Table::addCell (const int row, const int col, const std::string& data)
}
else
length = data.length ();
}
// Automatically maintain max width.
mMaxDataWidth[col] = max (mMaxDataWidth[col], (int)length);
@ -485,28 +457,22 @@ const std::string Table::formatHeader (
std::string data = mColumns[col];
Color c = getHeaderUnderline (col);
int gap = width - strippedLength (data);
std::string pad = "";
std::string intraPad = "";
std::string preJust = "";
std::string attrOn = "";
std::string attrOff = "";
std::string pad = std::string (padding, ' ');
// TODO When the following is replaced by:
// std::string postJust = std::string (gap, ' ');
// two unit tests fail.
std::string postJust = "";
for (int i = 0; i < padding; ++i)
pad += " ";
// Place the value within the available space - justify.
int gap = width - data.length ();
for (int i = 0; i < gap; ++i)
postJust += " ";
std::string intraPad = "";
if (col < (signed) mColumns.size () - 1)
for (int i = 0; i < getIntraPadding (); ++i)
intraPad += " ";
intraPad = std::string (getIntraPadding (), ' ');
return c.colorize (pad + preJust + data + postJust + pad) + intraPad;
return c.colorize (pad + data + postJust + pad) + intraPad;
}
////////////////////////////////////////////////////////////////////////////////
@ -533,22 +499,13 @@ const std::string Table::formatHeaderDashedUnderline (
Color c = getHeaderUnderline (col);
std::string data = "";
for (int i = 0; i < width; ++i)
data += '-';
std::string pad = "";
std::string data = std::string (width, '-');
std::string pad = std::string (padding, ' ');
std::string intraPad = "";
std::string attrOn = "";
std::string attrOff = "";
for (int i = 0; i < padding; ++i)
pad += " ";
// Place the value within the available space - justify.
if (col < (signed) mColumns.size () - 1)
for (int i = 0; i < getIntraPadding (); ++i)
intraPad += " ";
intraPad = std::string (getIntraPadding (), ' ');
return c.colorize (pad + data + pad) + intraPad;
}
@ -569,15 +526,11 @@ void Table::formatCell (
just justification = getJustification (row, col);
std::string data = getCell (row, col);
std::string pad = "";
std::string pad = std::string (padding, ' ');
std::string intraPad = "";
for (int i = 0; i < padding; ++i)
pad += " ";
if (col < (signed) mColumns.size () - 1)
for (int i = 0; i < getIntraPadding (); ++i)
intraPad += " ";
intraPad = std::string (getIntraPadding (), ' ');
// Break the text into chunks of width characters.
std::string preJust;
@ -593,39 +546,26 @@ void Table::formatCell (
postJust = "";
if (justification == left)
for (int i = 0; i < gap; ++i)
postJust += " ";
postJust = std::string (gap, ' ');
else if (justification == right)
for (int i = 0; i < gap; ++i)
preJust += " ";
preJust = std::string (gap, ' ');
else if (justification == center)
{
for (int i = 0; i < gap / 2; ++i)
preJust += " ";
for (size_t i = 0; i < gap - preJust.length (); ++i)
postJust += " ";
preJust = std::string (gap / 2, ' ');
postJust = std::string (gap - preJust.length (), ' ');
}
lines.push_back (c.colorize (pad + preJust + chunks[chunk] + postJust + pad + intraPad));
}
// The blank is used to vertically pad cells that have blank lines.
pad = "";
for (int i = 0; i < width; ++i)
pad += " ";
pad = std::string (width, ' ');
blank = c.colorize (pad + intraPad);
}
////////////////////////////////////////////////////////////////////////////////
void Table::suppressWS ()
{
mSuppressWS = true;
}
////////////////////////////////////////////////////////////////////////////////
void Table::setDateFormat (const std::string& dateFormat)
{
@ -880,31 +820,6 @@ void Table::sort (std::vector <int>& order)
}
}
////////////////////////////////////////////////////////////////////////////////
void Table::clean (std::string& value)
{
size_t start = 0;
size_t pos;
while ((pos = value.find ('\t', start)) != std::string::npos)
{
value.replace (pos, 1, " ");
start = pos; // Not pos + 1, because we have a destructive operation, and
// this is ultimately safer.
}
while ((pos = value.find ('\r', start)) != std::string::npos)
{
value.replace (pos, 1, " ");
start = pos;
}
while ((pos = value.find ('\n', start)) != std::string::npos)
{
value.replace (pos, 1, " ");
start = pos;
}
}
////////////////////////////////////////////////////////////////////////////////
const std::string Table::render (int maxrows /* = 0 */, int maxlines /* = 0 */)
{

View file

@ -82,7 +82,6 @@ public:
void addCell (int, int, double);
void setCellColor (int, int, const Color&);
void suppressWS ();
void setDateFormat (const std::string&);
void setReportName (const std::string&);
@ -103,7 +102,6 @@ private:
const std::string formatHeaderDashedUnderline (const int, const int, const int);
void formatCell (const int, const int, const int, const int, const int, std::vector <std::string>&, std::string&);
void sort (std::vector <int>&);
void clean (std::string&);
private:
std::vector <std::string> mColumns;
@ -131,7 +129,6 @@ private:
std::map <int, order> mSortOrder;
// Misc...
bool mSuppressWS;
std::string mDateFormat;
std::string mReportName;
};

View file

@ -706,24 +706,13 @@ int handleReportSummary (std::string &outs)
std::string subbar;
if (context.config.getBoolean ("color") || context.config.getBoolean ("_forcecolor"))
{
for (int b = 0; b < completedBar; ++b)
subbar += " ";
bar += bar_color.colorize (subbar);
subbar = "";
for (int b = 0; b < barWidth - completedBar; ++b)
subbar += " ";
bar += bg_color.colorize (subbar);
bar += bar_color.colorize (std::string ( completedBar, ' '));
bar += bg_color.colorize (std::string (barWidth - completedBar, ' '));
}
else
{
for (int b = 0; b < completedBar; ++b)
bar += "=";
for (int b = 0; b < barWidth - completedBar; ++b)
bar += " ";
bar += std::string ( completedBar, '=')
+ std::string (barWidth - completedBar, ' ');
}
table.addCell (row, 4, bar);
@ -1347,8 +1336,7 @@ int handleReportGHistoryMonthly (std::string &outs)
dBar = " " + dBar;
}
while (bar.length () < leftOffset - aBar.length ())
bar += " ";
bar += std::string (leftOffset - aBar.length (), ' ');
bar += color_add.colorize (aBar);
bar += color_done.colorize (cBar);
@ -1360,9 +1348,7 @@ int handleReportGHistoryMonthly (std::string &outs)
std::string cBar = ""; while (cBar.length () < completedBar) cBar += "X";
std::string dBar = ""; while (dBar.length () < deletedBar) dBar += "-";
while (bar.length () < leftOffset - aBar.length ())
bar += " ";
bar += std::string (leftOffset - aBar.length (), ' ');
bar += aBar + cBar + dBar;
}
@ -1554,9 +1540,7 @@ int handleReportGHistoryAnnual (std::string &outs)
dBar = " " + dBar;
}
while (bar.length () < leftOffset - aBar.length ())
bar += " ";
bar += std::string (leftOffset - aBar.length (), ' ');
bar += color_add.colorize (aBar);
bar += color_done.colorize (cBar);
bar += color_delete.colorize (dBar);
@ -1567,9 +1551,7 @@ int handleReportGHistoryAnnual (std::string &outs)
std::string cBar = ""; while (cBar.length () < completedBar) cBar += "X";
std::string dBar = ""; while (dBar.length () < deletedBar) dBar += "-";
while (bar.length () < leftOffset - aBar.length ())
bar += " ";
bar += std::string (leftOffset - aBar.length (), ' ');
bar += aBar + cBar + dBar;
}