mirror of
https://github.com/GothenburgBitFactory/taskwarrior.git
synced 2025-07-07 20:06:36 +02:00
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:
parent
25db00e97d
commit
ad9c89b9fb
3 changed files with 41 additions and 147 deletions
123
src/Table.cpp
123
src/Table.cpp
|
@ -63,7 +63,6 @@ Table::Table ()
|
||||||
, mDashedUnderline (false)
|
, mDashedUnderline (false)
|
||||||
, mTablePadding (0)
|
, mTablePadding (0)
|
||||||
, mTableWidth (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;
|
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 ())
|
if (mCommify.find (col) != mCommify.end ())
|
||||||
mData.add (row, col, commify (data));
|
mData.add (row, col, commify (data));
|
||||||
else
|
else
|
||||||
|
@ -223,7 +196,6 @@ void Table::addCell (const int row, const int col, const std::string& data)
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
length = data.length ();
|
length = data.length ();
|
||||||
}
|
|
||||||
|
|
||||||
// Automatically maintain max width.
|
// Automatically maintain max width.
|
||||||
mMaxDataWidth[col] = max (mMaxDataWidth[col], (int)length);
|
mMaxDataWidth[col] = max (mMaxDataWidth[col], (int)length);
|
||||||
|
@ -485,28 +457,22 @@ const std::string Table::formatHeader (
|
||||||
|
|
||||||
std::string data = mColumns[col];
|
std::string data = mColumns[col];
|
||||||
Color c = getHeaderUnderline (col);
|
Color c = getHeaderUnderline (col);
|
||||||
|
int gap = width - strippedLength (data);
|
||||||
|
|
||||||
std::string pad = "";
|
std::string pad = std::string (padding, ' ');
|
||||||
std::string intraPad = "";
|
|
||||||
std::string preJust = "";
|
// TODO When the following is replaced by:
|
||||||
std::string attrOn = "";
|
// std::string postJust = std::string (gap, ' ');
|
||||||
std::string attrOff = "";
|
// two unit tests fail.
|
||||||
std::string postJust = "";
|
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)
|
for (int i = 0; i < gap; ++i)
|
||||||
postJust += " ";
|
postJust += " ";
|
||||||
|
|
||||||
|
std::string intraPad = "";
|
||||||
if (col < (signed) mColumns.size () - 1)
|
if (col < (signed) mColumns.size () - 1)
|
||||||
for (int i = 0; i < getIntraPadding (); ++i)
|
intraPad = std::string (getIntraPadding (), ' ');
|
||||||
intraPad += " ";
|
|
||||||
|
|
||||||
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);
|
Color c = getHeaderUnderline (col);
|
||||||
|
|
||||||
std::string data = "";
|
std::string data = std::string (width, '-');
|
||||||
for (int i = 0; i < width; ++i)
|
std::string pad = std::string (padding, ' ');
|
||||||
data += '-';
|
|
||||||
|
|
||||||
std::string pad = "";
|
|
||||||
std::string intraPad = "";
|
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.
|
// Place the value within the available space - justify.
|
||||||
if (col < (signed) mColumns.size () - 1)
|
if (col < (signed) mColumns.size () - 1)
|
||||||
for (int i = 0; i < getIntraPadding (); ++i)
|
intraPad = std::string (getIntraPadding (), ' ');
|
||||||
intraPad += " ";
|
|
||||||
|
|
||||||
return c.colorize (pad + data + pad) + intraPad;
|
return c.colorize (pad + data + pad) + intraPad;
|
||||||
}
|
}
|
||||||
|
@ -569,15 +526,11 @@ void Table::formatCell (
|
||||||
just justification = getJustification (row, col);
|
just justification = getJustification (row, col);
|
||||||
std::string data = getCell (row, col);
|
std::string data = getCell (row, col);
|
||||||
|
|
||||||
std::string pad = "";
|
std::string pad = std::string (padding, ' ');
|
||||||
std::string intraPad = "";
|
std::string intraPad = "";
|
||||||
|
|
||||||
for (int i = 0; i < padding; ++i)
|
|
||||||
pad += " ";
|
|
||||||
|
|
||||||
if (col < (signed) mColumns.size () - 1)
|
if (col < (signed) mColumns.size () - 1)
|
||||||
for (int i = 0; i < getIntraPadding (); ++i)
|
intraPad = std::string (getIntraPadding (), ' ');
|
||||||
intraPad += " ";
|
|
||||||
|
|
||||||
// Break the text into chunks of width characters.
|
// Break the text into chunks of width characters.
|
||||||
std::string preJust;
|
std::string preJust;
|
||||||
|
@ -593,39 +546,26 @@ void Table::formatCell (
|
||||||
postJust = "";
|
postJust = "";
|
||||||
|
|
||||||
if (justification == left)
|
if (justification == left)
|
||||||
for (int i = 0; i < gap; ++i)
|
postJust = std::string (gap, ' ');
|
||||||
postJust += " ";
|
|
||||||
|
|
||||||
else if (justification == right)
|
else if (justification == right)
|
||||||
for (int i = 0; i < gap; ++i)
|
preJust = std::string (gap, ' ');
|
||||||
preJust += " ";
|
|
||||||
|
|
||||||
else if (justification == center)
|
else if (justification == center)
|
||||||
{
|
{
|
||||||
for (int i = 0; i < gap / 2; ++i)
|
preJust = std::string (gap / 2, ' ');
|
||||||
preJust += " ";
|
postJust = std::string (gap - preJust.length (), ' ');
|
||||||
|
|
||||||
for (size_t i = 0; i < gap - preJust.length (); ++i)
|
|
||||||
postJust += " ";
|
|
||||||
}
|
}
|
||||||
|
|
||||||
lines.push_back (c.colorize (pad + preJust + chunks[chunk] + postJust + pad + intraPad));
|
lines.push_back (c.colorize (pad + preJust + chunks[chunk] + postJust + pad + intraPad));
|
||||||
}
|
}
|
||||||
|
|
||||||
// The blank is used to vertically pad cells that have blank lines.
|
// The blank is used to vertically pad cells that have blank lines.
|
||||||
pad = "";
|
pad = std::string (width, ' ');
|
||||||
for (int i = 0; i < width; ++i)
|
|
||||||
pad += " ";
|
|
||||||
|
|
||||||
blank = c.colorize (pad + intraPad);
|
blank = c.colorize (pad + intraPad);
|
||||||
}
|
}
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////////
|
|
||||||
void Table::suppressWS ()
|
|
||||||
{
|
|
||||||
mSuppressWS = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////////////
|
||||||
void Table::setDateFormat (const std::string& dateFormat)
|
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 */)
|
const std::string Table::render (int maxrows /* = 0 */, int maxlines /* = 0 */)
|
||||||
{
|
{
|
||||||
|
|
|
@ -82,7 +82,6 @@ public:
|
||||||
void addCell (int, int, double);
|
void addCell (int, int, double);
|
||||||
void setCellColor (int, int, const Color&);
|
void setCellColor (int, int, const Color&);
|
||||||
|
|
||||||
void suppressWS ();
|
|
||||||
void setDateFormat (const std::string&);
|
void setDateFormat (const std::string&);
|
||||||
void setReportName (const std::string&);
|
void setReportName (const std::string&);
|
||||||
|
|
||||||
|
@ -103,7 +102,6 @@ private:
|
||||||
const std::string formatHeaderDashedUnderline (const int, const int, const int);
|
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 formatCell (const int, const int, const int, const int, const int, std::vector <std::string>&, std::string&);
|
||||||
void sort (std::vector <int>&);
|
void sort (std::vector <int>&);
|
||||||
void clean (std::string&);
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
std::vector <std::string> mColumns;
|
std::vector <std::string> mColumns;
|
||||||
|
@ -131,7 +129,6 @@ private:
|
||||||
std::map <int, order> mSortOrder;
|
std::map <int, order> mSortOrder;
|
||||||
|
|
||||||
// Misc...
|
// Misc...
|
||||||
bool mSuppressWS;
|
|
||||||
std::string mDateFormat;
|
std::string mDateFormat;
|
||||||
std::string mReportName;
|
std::string mReportName;
|
||||||
};
|
};
|
||||||
|
|
|
@ -706,24 +706,13 @@ int handleReportSummary (std::string &outs)
|
||||||
std::string subbar;
|
std::string subbar;
|
||||||
if (context.config.getBoolean ("color") || context.config.getBoolean ("_forcecolor"))
|
if (context.config.getBoolean ("color") || context.config.getBoolean ("_forcecolor"))
|
||||||
{
|
{
|
||||||
for (int b = 0; b < completedBar; ++b)
|
bar += bar_color.colorize (std::string ( completedBar, ' '));
|
||||||
subbar += " ";
|
bar += bg_color.colorize (std::string (barWidth - completedBar, ' '));
|
||||||
|
|
||||||
bar += bar_color.colorize (subbar);
|
|
||||||
subbar = "";
|
|
||||||
|
|
||||||
for (int b = 0; b < barWidth - completedBar; ++b)
|
|
||||||
subbar += " ";
|
|
||||||
|
|
||||||
bar += bg_color.colorize (subbar);
|
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
for (int b = 0; b < completedBar; ++b)
|
bar += std::string ( completedBar, '=')
|
||||||
bar += "=";
|
+ std::string (barWidth - completedBar, ' ');
|
||||||
|
|
||||||
for (int b = 0; b < barWidth - completedBar; ++b)
|
|
||||||
bar += " ";
|
|
||||||
}
|
}
|
||||||
table.addCell (row, 4, bar);
|
table.addCell (row, 4, bar);
|
||||||
|
|
||||||
|
@ -1347,8 +1336,7 @@ int handleReportGHistoryMonthly (std::string &outs)
|
||||||
dBar = " " + dBar;
|
dBar = " " + dBar;
|
||||||
}
|
}
|
||||||
|
|
||||||
while (bar.length () < leftOffset - aBar.length ())
|
bar += std::string (leftOffset - aBar.length (), ' ');
|
||||||
bar += " ";
|
|
||||||
|
|
||||||
bar += color_add.colorize (aBar);
|
bar += color_add.colorize (aBar);
|
||||||
bar += color_done.colorize (cBar);
|
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 cBar = ""; while (cBar.length () < completedBar) cBar += "X";
|
||||||
std::string dBar = ""; while (dBar.length () < deletedBar) dBar += "-";
|
std::string dBar = ""; while (dBar.length () < deletedBar) dBar += "-";
|
||||||
|
|
||||||
while (bar.length () < leftOffset - aBar.length ())
|
bar += std::string (leftOffset - aBar.length (), ' ');
|
||||||
bar += " ";
|
|
||||||
|
|
||||||
bar += aBar + cBar + dBar;
|
bar += aBar + cBar + dBar;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1554,9 +1540,7 @@ int handleReportGHistoryAnnual (std::string &outs)
|
||||||
dBar = " " + dBar;
|
dBar = " " + dBar;
|
||||||
}
|
}
|
||||||
|
|
||||||
while (bar.length () < leftOffset - aBar.length ())
|
bar += std::string (leftOffset - aBar.length (), ' ');
|
||||||
bar += " ";
|
|
||||||
|
|
||||||
bar += color_add.colorize (aBar);
|
bar += color_add.colorize (aBar);
|
||||||
bar += color_done.colorize (cBar);
|
bar += color_done.colorize (cBar);
|
||||||
bar += color_delete.colorize (dBar);
|
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 cBar = ""; while (cBar.length () < completedBar) cBar += "X";
|
||||||
std::string dBar = ""; while (dBar.length () < deletedBar) dBar += "-";
|
std::string dBar = ""; while (dBar.length () < deletedBar) dBar += "-";
|
||||||
|
|
||||||
while (bar.length () < leftOffset - aBar.length ())
|
bar += std::string (leftOffset - aBar.length (), ' ');
|
||||||
bar += " ";
|
|
||||||
|
|
||||||
bar += aBar + cBar + dBar;
|
bar += aBar + cBar + dBar;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue