Skip to content

Commit

Permalink
Improved performance of adding rows and columns to the WTable.
Browse files Browse the repository at this point in the history
The issue was the calling of WTableRow::rowNum() for every added cell,
which iterated over the whole table.

The methods WTableRow::createCell() and WTableRow::insertColumn() got
the new row argument defaulting to -1 that WTable can use when
creating new elements with WTable::insertRow() and
WTable::insertColumn(). These methods are called by WTable::expand()
so the performance improvement affects every operation that expands
the table.

The performance of populating a new table row by row is increased
with this commit from O(n^2+n) to O(n).
  • Loading branch information
jakub-w committed May 9, 2019
1 parent d1c74ac commit 316f6b1
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 12 deletions.
4 changes: 2 additions & 2 deletions src/Wt/WTable.C
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ WTableRow* WTable::insertRow(int row, std::unique_ptr<WTableRow> tableRow)
widgetAdded(cell.get());
}
rows_.insert(rows_.begin() + row, std::move(tableRow));
rows_[row].get()->expand(columnCount());
rows_[row].get()->expand(columnCount(), row);
repaint(RepaintFlag::SizeAffected);

return rows_[row].get();
Expand All @@ -113,7 +113,7 @@ WTableColumn* WTable::insertColumn(int column,
std::unique_ptr<WTableColumn> tableColumn)
{
for (unsigned i = 0; i < rows_.size(); ++i)
rows_[i]->insertColumn(column);
rows_[i]->insertColumn(column, i);

if ((unsigned)column <= columns_.size()) {
if (!tableColumn){
Expand Down
18 changes: 11 additions & 7 deletions src/Wt/WTableRow.C
Original file line number Diff line number Diff line change
Expand Up @@ -26,19 +26,23 @@ WTableRow::WTableRow()
WTableRow::~WTableRow()
{ }

std::unique_ptr<WTableCell> WTableRow::createCell(int column)
std::unique_ptr<WTableCell> WTableRow::createCell(int column, int row)
{
if (table_)
return table_->createCell(rowNum(), column);
if (table_) {
if (-1 == row) {
row = rowNum();
}
return table_->createCell(row, column);
}
else
return std::unique_ptr<WTableCell>(new WTableCell());
}

void WTableRow::expand(int numCells)
void WTableRow::expand(int numCells, int row)
{
int cursize = cells_.size();
for (int col = cursize; col < numCells; ++col) {
cells_.push_back(createCell(col));
cells_.push_back(createCell(col, row));
WTableCell *cell = cells_.back().get();
if (table_)
table_->widgetAdded(cell);
Expand All @@ -47,9 +51,9 @@ void WTableRow::expand(int numCells)
}
}

void WTableRow::insertColumn(int column)
void WTableRow::insertColumn(int column, int row)
{
cells_.insert(cells_.begin() + column, createCell(column));
cells_.insert(cells_.begin() + column, createCell(column, row));
WTableCell *cell = cells_[column].get();
if (table_)
table_->widgetAdded(cell);
Expand Down
6 changes: 3 additions & 3 deletions src/Wt/WTableRow.h
Original file line number Diff line number Diff line change
Expand Up @@ -136,10 +136,10 @@ class WT_API WTableRow : public WObject
virtual const std::string id() const override;

protected:
virtual std::unique_ptr<WTableCell> createCell(int column);
virtual std::unique_ptr<WTableCell> createCell(int column, int row = -1);

private:
void expand(int numCells);
void expand(int numCells, int row = -1);

WTable *table_;
std::vector<std::unique_ptr<WTableCell> > cells_;
Expand All @@ -151,7 +151,7 @@ class WT_API WTableRow : public WObject

void updateDom(DomElement& element, bool all);
void setTable(WTable *table);
void insertColumn(int column);
void insertColumn(int column, int row = -1);
std::unique_ptr<WTableCell> removeColumn(int column);

void undoHide();
Expand Down

0 comments on commit 316f6b1

Please sign in to comment.