Skip to content

Commit 27d5303

Browse files
committed
add setRow() and fixes for Table(Iterable) performance
1 parent c001eb2 commit 27d5303

2 files changed

Lines changed: 29 additions & 8 deletions

File tree

core/src/processing/data/Table.java

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -155,14 +155,30 @@ public Table(InputStream input, String options) throws IOException {
155155

156156
public Table(Iterable<TableRow> rows) {
157157
init();
158-
boolean firstRow = true;
159-
for (TableRow row : rows) {
160-
if (firstRow) {
161-
setColumnTypes(row.getColumnTypes());
162-
setColumnTitles(row.getColumnTitles());
163-
firstRow = false;
158+
159+
int row = 0;
160+
int alloc = 10;
161+
162+
for (TableRow incoming : rows) {
163+
if (row == 0) {
164+
setColumnTypes(incoming.getColumnTypes());
165+
setColumnTitles(incoming.getColumnTitles());
166+
// Do this after setting types, otherwise it'll attempt to parse the
167+
// allocated but empty rows, and drive CATEGORY columns nutso.
168+
setRowCount(alloc);
169+
170+
} else if (row == alloc) {
171+
// Far more efficient than re-allocating all columns and doing a copy
172+
alloc *= 2;
173+
setRowCount(alloc);
164174
}
165-
addRow(row);
175+
176+
//addRow(row);
177+
setRow(row++, incoming);
178+
}
179+
// Shrink the table to only the rows that were used
180+
if (row != alloc) {
181+
setRowCount(row);
166182
}
167183
}
168184

@@ -1837,7 +1853,11 @@ public TableRow addRow() {
18371853
* @param source a reference to the original row to be duplicated
18381854
*/
18391855
public TableRow addRow(TableRow source) {
1840-
int row = rowCount;
1856+
return setRow(rowCount, source);
1857+
}
1858+
1859+
1860+
public TableRow setRow(int row, TableRow source) {
18411861
// Make sure there are enough columns to add this data
18421862
ensureBounds(row, source.getColumnCount() - 1);
18431863

core/todo.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ X add getColumnTitle(int) and getColumnTitles() to TableRow interface
1515
X fixes for new Table(Iterable)
1616
X category data types were not importing their dictionary
1717
X column titles weren't set on the new table
18+
X drastic performance improvements for addRow()
1819
X when using setColumnType(), replace nulls with missingInt, missingFloat, etc
1920
X formerly, was throwing a NullPointerException
2021

0 commit comments

Comments
 (0)