Skip to content

Commit

Permalink
producer: increase performance of sampling rate lookup (#264)
Browse files Browse the repository at this point in the history
Formatting a string with version and domain when looking up sampling
rate hurts performance. Instead, use a dedicated structure for key. It
also reduces the pressure on the GC.
  • Loading branch information
vincentbernat authored Dec 29, 2023
1 parent e4a14c2 commit 1a90055
Showing 1 changed file with 15 additions and 11 deletions.
26 changes: 15 additions & 11 deletions producer/proto/producer_nf.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,19 @@ type SamplingRateSystem interface {
AddSamplingRate(version uint16, obsDomainId uint32, samplingRate uint32)
}

type basicSamplingRateKey struct {
version uint16
obsDomainId uint32
}

type basicSamplingRateSystem struct {
//sampling map[uint16]map[uint32]uint32
sampling map[string]uint32
sampling map[basicSamplingRateKey]uint32
samplinglock *sync.RWMutex
}

func CreateSamplingSystem() SamplingRateSystem {
ts := &basicSamplingRateSystem{
//sampling: make(map[uint16]map[uint32]uint32),
sampling: make(map[string]uint32),
sampling: make(map[basicSamplingRateKey]uint32),
samplinglock: &sync.RWMutex{},
}
return ts
Expand All @@ -36,18 +39,19 @@ func CreateSamplingSystem() SamplingRateSystem {
func (s *basicSamplingRateSystem) AddSamplingRate(version uint16, obsDomainId uint32, samplingRate uint32) {
s.samplinglock.Lock()
defer s.samplinglock.Unlock()
/*_, exists := s.sampling[version]
if exists != true {
s.sampling[version] = make(map[uint32]uint32)
}
s.sampling[version][obsDomainId] = samplingRate*/
s.sampling[fmt.Sprintf("%d-%d", version, obsDomainId)] = samplingRate
s.sampling[basicSamplingRateKey{
version: version,
obsDomainId: obsDomainId,
}] = samplingRate
}

func (s *basicSamplingRateSystem) GetSamplingRate(version uint16, obsDomainId uint32) (uint32, error) {
s.samplinglock.RLock()
defer s.samplinglock.RUnlock()
if samplingRate, ok := s.sampling[fmt.Sprintf("%d-%d", version, obsDomainId)]; ok {
if samplingRate, ok := s.sampling[basicSamplingRateKey{
version: version,
obsDomainId: obsDomainId,
}]; ok {
return samplingRate, nil
}

Expand Down

0 comments on commit 1a90055

Please sign in to comment.