Skip to content
Open
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
21 changes: 13 additions & 8 deletions zapcore/sampler.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,34 +26,39 @@ import (
)

const (
_numLevels = _maxLevel - _minLevel + 1
_countersPerLevel = 4096
_counterBuckets = 4096
)

type counter struct {
resetAt atomic.Int64
counter atomic.Uint64
}

type counters [_numLevels][_countersPerLevel]counter
type counters [_counterBuckets]counter

func newCounters() *counters {
return &counters{}
}

func (cs *counters) get(lvl Level, key string) *counter {
i := lvl - _minLevel
j := fnv32a(key) % _countersPerLevel
return &cs[i][j]
j := fnv32a(lvl, key) % _counterBuckets
return &cs[j]
}

// fnv32a, adapted from "hash/fnv", but without a []byte(string) alloc
func fnv32a(s string) uint32 {
// fnv32a returns a hash of its arguments. It is adapted from "hash/fnv", but
// without a []byte(string) alloc.
func fnv32a(lvl Level, s string) uint32 {
const (
offset32 = 2166136261
prime32 = 16777619
)
hash := uint32(offset32)

// Hash lvl
hash ^= uint32(lvl)
hash *= prime32

// Hash s
for i := 0; i < len(s); i++ {
hash ^= uint32(s[i])
hash *= prime32
Expand Down
Loading