Skip to content

Commit

Permalink
Replace the map by a slice in function and location caching.
Browse files Browse the repository at this point in the history
  • Loading branch information
abeaumont committed Dec 20, 2021
1 parent 4aaec20 commit 14eaf48
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 15 deletions.
10 changes: 6 additions & 4 deletions pkg/convert/profile_extra_bench_test.go
Expand Up @@ -56,15 +56,17 @@ func parseWithCache(p *tree.Profile) int {
var b bytes.Buffer
for _, s := range p.Sample {
for i := len(s.LocationId) - 1; i >= 0; i-- {
loc, ok := locs[s.LocationId[i]]
if !ok {
id := s.LocationId[i]
if id >= uint64(len(locs)) {
continue
}
loc := locs[id]
for j := len(loc.Line) - 1; j >= 0; j-- {
fn, found := fns[loc.Line[j].FunctionId]
if !found {
id := loc.Line[j].FunctionId
if id >= uint64(len(fns)) {
continue
}
fn := fns[id]
if b.Len() > 0 {
_ = b.WriteByte(';')
}
Expand Down
16 changes: 9 additions & 7 deletions pkg/scrape/pprof.go
Expand Up @@ -72,8 +72,8 @@ func (w *pprofWriter) writeProfile(b []byte) error {
profileTime = time.Now()
}

var locs map[uint64]*tree.Location
var fns map[uint64]*tree.Function
var locs []*tree.Location
var fns []*tree.Function

for _, s := range p.GetSampleType() {
sampleTypeName := p.StringTable[s.Type]
Expand Down Expand Up @@ -184,7 +184,7 @@ func newCacheEntry(l []*tree.Label) *cacheEntry {
return &cacheEntry{Trie: transporttrie.New(), labels: l}
}

func (t *cache) writeProfiles(x *tree.Profile, sampleType int64, locs map[uint64]*tree.Location, fns map[uint64]*tree.Function) {
func (t *cache) writeProfiles(x *tree.Profile, sampleType int64, locs []*tree.Location, fns []*tree.Function) {
valueIndex := 0
if sampleType != 0 {
for i, v := range x.SampleType {
Expand All @@ -201,10 +201,11 @@ func (t *cache) writeProfiles(x *tree.Profile, sampleType int64, locs map[uint64
for _, s := range x.Sample {
entry := t.getOrCreate(sampleType, s.Label)
for i := len(s.LocationId) - 1; i >= 0; i-- {
loc, ok := locs[s.LocationId[i]]
if !ok {
id := s.LocationId[i]
if id >= uint64(len(locs)) {
continue
}
loc := locs[id]
// Multiple line indicates this location has inlined functions,
// where the last entry represents the caller into which the
// preceding entries were inlined.
Expand All @@ -215,10 +216,11 @@ func (t *cache) writeProfiles(x *tree.Profile, sampleType int64, locs map[uint64
//
// Therefore iteration goes in reverse order.
for j := len(loc.Line) - 1; j >= 0; j-- {
fn, found := fns[loc.Line[j].FunctionId]
if !found {
id := loc.Line[j].FunctionId
if id >= uint64(len(fns)) {
continue
}
fn := fns[id]
if b.Len() > 0 {
_ = b.WriteByte(';')
}
Expand Down
8 changes: 4 additions & 4 deletions pkg/storage/tree/profile_extra.go
Expand Up @@ -147,16 +147,16 @@ func FindFunction(x *Profile, fid uint64) (*Function, bool) {
return nil, false
}

func Locations(x *Profile) map[uint64]*Location {
m := make(map[uint64]*Location, len(x.Location))
func Locations(x *Profile) []*Location {
m := make([]*Location, len(x.Location)+1)
for _, l := range x.Location {
m[l.Id] = l
}
return m
}

func Functions(x *Profile) map[uint64]*Function {
m := make(map[uint64]*Function, len(x.Function))
func Functions(x *Profile) []*Function {
m := make([]*Function, len(x.Function)+1)
for _, f := range x.Function {
m[f.Id] = f
}
Expand Down

0 comments on commit 14eaf48

Please sign in to comment.