Skip to content

Commit

Permalink
Improve table data preallocation and potential slice leak. This chang…
Browse files Browse the repository at this point in the history
…e changes the behavior to preallocate slices to the the predicted length and capacity. It also remove some slice usage that could lead to memory preasure since it does not release data for GC to pick up
  • Loading branch information
xllora committed Jan 5, 2017
1 parent ea690cf commit 9845651
Showing 1 changed file with 11 additions and 4 deletions.
15 changes: 11 additions & 4 deletions bql/table/table.go
Expand Up @@ -281,21 +281,26 @@ func (t *Table) DotProduct(t2 *Table) error {
}
// Update the data.
td := t.data
t.data = []Row{}
cnt, size := 0, len(td)*len(t2.data)
t.data = make([]Row, size, size) // Preallocate resulting table.
for _, r1 := range td {
for _, r2 := range t2.data {
t.data = append(t.data, MergeRows([]Row{r1, r2}))
t.data[cnt] = MergeRows([]Row{r1, r2})
cnt++
}
}
return nil
}

// DeleteRow removes the row at position i from the table.
func (t *Table) DeleteRow(i int) error {
td := make([]Row, len(t.data)-1, len(t.data)-1) // Preallocate resulting table.
if i < 0 || i >= len(t.data) {
return fmt.Errorf("cannot delete row %d from a table with %d rows", i, len(t.data))
}
t.data = append(t.data[:i], t.data[i+1:]...)
copy(td, t.data[:i])
copy(td[i:], t.data[i+1:])
t.data = td
return nil
}

Expand All @@ -307,7 +312,9 @@ func (t *Table) Truncate() {
// Limit keeps the initial ith rows.
func (t *Table) Limit(i int64) {
if int64(len(t.data)) > i {
t.data = t.data[:i]
td := make([]Row, i, i) // Preallocate resulting table.
copy(td, t.data[:i])
t.data = td
}
}

Expand Down

0 comments on commit 9845651

Please sign in to comment.