Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf: optimize memory usage for client.List func #369

Merged
merged 2 commits into from
Jan 2, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 11 additions & 8 deletions client/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,25 +147,28 @@ 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()
if err != nil {
return err
}
if resultVal.IsNil() || resultVal.Cap() == 0 {
resultVal.Set(reflect.MakeSlice(resultVal.Type(), 0, len(rows)))
}
} 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, tableCache.Len()))
}
}
i := resultVal.Len()
maxCap := resultVal.Cap()

for _, row := range rows {
if i >= resultVal.Cap() {
if i >= maxCap {
break
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@halfcrazy sorry this took so long. Would this work:

	var rows map[string]model.Model
	if a.cond != nil {
		rows, err = a.cond.Matches()
		if err != nil {
			return err
		}
	} else {
		rows = tableCache.Rows()
	}
	
	// If given a null slice, fill it in with all the resulting rows, if not, just up to
	// its capacity.
	if resultVal.IsNil() || resultVal.Cap() == 0 {
		resultVal.Set(reflect.MakeSlice(resultVal.Type(), 0, len(rows)))
	}
	i := resultVal.Len()
	cap := resultVal.Cap()
	for _, row := range rows {
		if i >= cap {
			break
		}
		...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

update

appendValue(reflect.ValueOf(row))
Expand Down
Loading