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

memoize isKnownServices for improved performance #2617

Merged
merged 1 commit into from
Jun 19, 2017
Merged
Changes from all commits
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
25 changes: 24 additions & 1 deletion render/theinternet.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"regexp"
"strings"

"github.com/bluele/gcache"

"github.com/weaveworks/scope/probe/host"
"github.com/weaveworks/scope/report"
)
Expand Down Expand Up @@ -32,12 +34,33 @@ var (
// and having separate nodes for them makes visualizations worse
`ec2.*\.amazonaws\.com`,
}, `|`) + `)$`)

// Memoization for isKnownService.
//
// The 10000 comes from the observation that large reports contain
// hundreds of names, and in a multi-tenant context we want to be
// able to render a few dozen reports concurrently. Also, unlike
// memoization in the reducers, which is keyed on reports, this
// cache is effective when rendering multiple reports from the
// same cluster of probes, e.g. from different points in time,
// since names tend to change infrequently.
//
// Since names are generally <50 bytes, this shouldn't weight in
// at more than a few MB of memory.
knownServiceCache = gcache.New(10000).ARC().Build()
)

// TODO: Make it user-customizable https://github.com/weaveworks/scope/issues/1876
// NB: this is a hotspot in rendering performance.
func isKnownService(hostname string) bool {
return knownServiceMatcher.MatchString(hostname) && !knownServiceExcluder.MatchString(hostname)
if v, err := knownServiceCache.Get(hostname); err == nil {
return v.(bool)
}

known := knownServiceMatcher.MatchString(hostname) && !knownServiceExcluder.MatchString(hostname)
knownServiceCache.Set(hostname, known)

return known
}

// LocalNetworks returns a superset of the networks (think: CIDRs) that are
Expand Down