Skip to content

Commit

Permalink
reduce heap allocations #6
Browse files Browse the repository at this point in the history
Summary: Cache stats snapshot

Reviewed By: abulimov, deathowl

Differential Revision: D58414699

fbshipit-source-id: f49ecd3da01b7b70e838adcec4e4e25064b6e496
  • Loading branch information
leoleovich authored and facebook-github-bot committed Jun 11, 2024
1 parent fa7eed6 commit 6ab7f9f
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 9 deletions.
2 changes: 1 addition & 1 deletion ptp/sptp/client/sptp.go
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,7 @@ func (p *SPTP) reprioritize(bestAddr string) {
func (p *SPTP) processResults(results map[string]*RunResult) {
defer func() {
for addr, res := range results {
s := runResultToStats(addr, res, p.priorities[addr], addr == p.bestGM)
s := runResultToGMStats(addr, res, p.priorities[addr], addr == p.bestGM)
p.stats.SetGMStats(s)
}
}()
Expand Down
12 changes: 7 additions & 5 deletions ptp/sptp/client/stats.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ type Stats struct {
clientStats
sysStats
gmStats gmstats.Stats
snapshot gmstats.Stats
procStartTime time.Time
memstats runtime.MemStats
}
Expand Down Expand Up @@ -80,6 +81,7 @@ type sysStats struct {
func NewStats() *Stats {
return &Stats{
gmStats: gmstats.Stats{},
snapshot: gmstats.Stats{},
procStartTime: time.Now(),
}
}
Expand Down Expand Up @@ -157,11 +159,11 @@ func (s *Stats) GetCounters() map[string]int64 {

// GetGMStats returns an all gm stats
func (s *Stats) GetGMStats() gmstats.Stats {
ret := make(gmstats.Stats, len(s.gmStats))
s.Lock()
copy(ret, s.gmStats)
s.Unlock()
return ret
defer s.Unlock()
s.snapshot = make(gmstats.Stats, len(s.gmStats))
copy(s.snapshot, s.gmStats)
return s.snapshot
}

// SetGMStats sets GM stats for particular gm
Expand Down Expand Up @@ -206,7 +208,7 @@ func (s *Stats) CollectSysStats() error {
return nil
}

func runResultToStats(address string, r *RunResult, p3 int, selected bool) *gmstats.Stat {
func runResultToGMStats(address string, r *RunResult, p3 int, selected bool) *gmstats.Stat {
s := &gmstats.Stat{
GMAddress: address,
Priority3: uint8(p3),
Expand Down
6 changes: 3 additions & 3 deletions ptp/sptp/client/stats_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func TestRunResultToStatsError(t *testing.T) {
Server: "192.168.0.10",
Error: fmt.Errorf("ooops"),
}
got := runResultToStats("192.168.0.10", r, 1, false)
got := runResultToGMStats("192.168.0.10", r, 1, false)
want := &gmstats.Stat{
GMAddress: "192.168.0.10",
Priority3: 1,
Expand Down Expand Up @@ -101,12 +101,12 @@ func TestRunResultToStats(t *testing.T) {
}

t.Run("not selected", func(t *testing.T) {
got := runResultToStats("192.168.0.10", r, 3, false)
got := runResultToGMStats("192.168.0.10", r, 3, false)
require.Equal(t, want, got)
})
want.Selected = true
t.Run("selected", func(t *testing.T) {
got := runResultToStats("192.168.0.10", r, 3, true)
got := runResultToGMStats("192.168.0.10", r, 3, true)
require.Equal(t, want, got)
})
}
Expand Down

0 comments on commit 6ab7f9f

Please sign in to comment.