Skip to content
This repository has been archived by the owner on Nov 5, 2021. It is now read-only.

Commit

Permalink
RDS file provider: Minor improvements to resource allocation. (#642)
Browse files Browse the repository at this point in the history
Ref: #634
PiperOrigin-RevId: 387390152
  • Loading branch information
manugarg authored Jul 28, 2021
1 parent d3f959b commit b2710ef
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 30 deletions.
23 changes: 18 additions & 5 deletions rds/file/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,20 +69,25 @@ type lister struct {

// ListResources returns the last successfully parsed list of resources.
func (ls *lister) ListResources(req *pb.ListResourcesRequest) (*pb.ListResourcesResponse, error) {
ls.mu.RLock()
defer ls.mu.RUnlock()

// If there are no filters, return early.
if len(req.GetFilter()) == 0 {
return &pb.ListResourcesResponse{Resources: append([]*pb.Resource{}, ls.resources...)}, nil
}

allFilters, err := filter.ParseFilters(req.GetFilter(), SupportedFilters.RegexFilterKeys, "")
if err != nil {
return nil, err
}
nameFilter, labelsFilter := allFilters.RegexFilters["name"], allFilters.LabelsFilter

ls.mu.RLock()
defer ls.mu.RUnlock()

// Allocate resources for response early but optimize for large number of
// total resources.
allocSize := len(ls.resources)
if allocSize > 10 && len(req.GetFilter()) != 0 {
allocSize = 10
if allocSize > 100 {
allocSize = 100
}
resources := make([]*pb.Resource, 0, allocSize)

Expand Down Expand Up @@ -200,6 +205,14 @@ func (p *Provider) ListResources(req *pb.ListResourcesRequest) (*pb.ListResource
return ls.ListResources(req)
}

// Avoid append and another allocation if there is only one lister, most
// common use case.
if len(p.listers) == 1 {
for _, ls := range p.listers {
return ls.ListResources(req)
}
}

var result []*pb.Resource
for _, fp := range p.filePaths {
res, err := p.listers[fp].ListResources(req)
Expand Down
54 changes: 29 additions & 25 deletions rds/file/file_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,35 +129,39 @@ func TestListResourcesWithResourcePath(t *testing.T) {

func BenchmarkListResources(b *testing.B) {
for _, n := range []int{100, 10000, 1000000} {
b.Run(fmt.Sprintf("%d-resources", n), func(b *testing.B) {
b.StopTimer()
ls := &lister{
resources: make([]*pb.Resource, n),
}
for i := 0; i < n; i++ {
ls.resources[i] = &pb.Resource{
Name: proto.String(fmt.Sprintf("host-%d", i)),
Ip: proto.String("10.1.1.1"),
Port: proto.Int32(80),
Labels: map[string]string{
"index": strconv.Itoa(i),
},
LastUpdated: proto.Int64(time.Now().Unix()),
for _, filters := range [][]*rdspb.Filter{nil, []*rdspb.Filter{{Key: proto.String("name"), Value: proto.String("host-1.*")}}} {
b.Run(fmt.Sprintf("%d-resources,%d-filters", n, len(filters)), func(b *testing.B) {
b.StopTimer()
ls := &lister{
resources: make([]*rdspb.Resource, n),
}
}
b.StartTimer()
for i := 0; i < n; i++ {
ls.resources[i] = &rdspb.Resource{
Name: proto.String(fmt.Sprintf("host-%d", i)),
Ip: proto.String("10.1.1.1"),
Port: proto.Int32(80),
Labels: map[string]string{
"index": strconv.Itoa(i),
},
LastUpdated: proto.Int64(time.Now().Unix()),
}
}
b.StartTimer()

for j := 0; j < b.N; j++ {
res, err := ls.ListResources(nil)
for j := 0; j < b.N; j++ {
res, err := ls.ListResources(&rdspb.ListResourcesRequest{
Filter: filters,
})

if err != nil {
b.Errorf("Unexpected error while listing resources: %v", err)
}
if err != nil {
b.Errorf("Unexpected error while listing resources: %v", err)
}

if len(res.GetResources()) != n {
b.Errorf("Got %d resources, wanted: %d", len(res.GetResources()), n)
if filters == nil && len(res.GetResources()) != n {
b.Errorf("Got %d resources, wanted: %d", len(res.GetResources()), n)
}
}
}
})
})
}
}
}

0 comments on commit b2710ef

Please sign in to comment.