Skip to content

Commit

Permalink
Cache the OrgId for nameReferenceTransformer
Browse files Browse the repository at this point in the history
Because this is in an inner loop and is fairly memory-allocation
expensive even on a single allocation, it comes up top-of-the-list in
memory allocation pprof profiles, for example with the coredns
ClusterAddon.

Add simple caching.
  • Loading branch information
justinsb committed Sep 7, 2021
1 parent 4d002af commit f59a706
Showing 1 changed file with 9 additions and 2 deletions.
11 changes: 9 additions & 2 deletions api/internal/accumulator/namereferencetransformer.go
Expand Up @@ -109,11 +109,18 @@ func debug(fMap filterMap) {
// 'spec/scaleTargetRef/name' field. Return a filter that can do that.
func (t *nameReferenceTransformer) determineFilters(
resources []*resource.Resource) (fMap filterMap) {

// We cache the resource OrgId values because they don't change and otherwise are very visible in a memory pprof
resourceOrgIds := make([]resid.ResId, len(resources))
for i, resource := range resources {
resourceOrgIds[i] = resource.OrgId()
}

fMap = make(filterMap)
for _, backReference := range t.backRefs {
for _, referrerSpec := range backReference.Referrers {
for _, res := range resources {
if res.OrgId().IsSelected(&referrerSpec.Gvk) {
for i, res := range resources {
if resourceOrgIds[i].IsSelected(&referrerSpec.Gvk) {
// If this is true, the res might be a referrer, and if
// so, the name reference it holds might need an update.
if resHasField(res, referrerSpec.Path) {
Expand Down

0 comments on commit f59a706

Please sign in to comment.