From 984565189c6a2b986a32ebc1715cdbb97f91eff4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Xavier=20Llor=C3=A0?= Date: Thu, 5 Jan 2017 15:46:09 -0800 Subject: [PATCH] Improve table data preallocation and potential slice leak. This change 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 --- bql/table/table.go | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/bql/table/table.go b/bql/table/table.go index 0b783949..5de35e35 100644 --- a/bql/table/table.go +++ b/bql/table/table.go @@ -281,10 +281,12 @@ 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 @@ -292,10 +294,13 @@ func (t *Table) DotProduct(t2 *Table) error { // 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 } @@ -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 } }