Skip to content

Commit

Permalink
Merge pull request #369 from halfcrazy/perf/list-mem
Browse files Browse the repository at this point in the history
perf: optimize memory usage for client.List func
  • Loading branch information
jcaamano authored Jan 2, 2024
2 parents 239822f + 776d5b2 commit c71cb09
Showing 1 changed file with 8 additions and 8 deletions.
16 changes: 8 additions & 8 deletions client/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,13 +147,6 @@ func (a api) List(ctx context.Context, result interface{}) error {
return ErrNotFound
}

// If given a null slice, fill it in the cache table completely, if not, just up to
// its capability
if resultVal.IsNil() || resultVal.Cap() == 0 {
resultVal.Set(reflect.MakeSlice(resultVal.Type(), 0, tableCache.Len()))
}
i := resultVal.Len()

var rows map[string]model.Model
if a.cond != nil {
rows, err = a.cond.Matches()
Expand All @@ -163,9 +156,16 @@ func (a api) List(ctx context.Context, result interface{}) error {
} else {
rows = tableCache.Rows()
}
// If given a null slice, fill it in the cache table completely, if not, just up to
// its capability.
if resultVal.IsNil() || resultVal.Cap() == 0 {
resultVal.Set(reflect.MakeSlice(resultVal.Type(), 0, len(rows)))
}
i := resultVal.Len()
maxCap := resultVal.Cap()

for _, row := range rows {
if i >= resultVal.Cap() {
if i >= maxCap {
break
}
appendValue(reflect.ValueOf(row))
Expand Down

0 comments on commit c71cb09

Please sign in to comment.