github.com/uber-go/tally/v4@v4.1.17/internal/identity/accumulator.go (about)

     1  // Copyright (c) 2021 Uber Technologies, Inc.
     2  //
     3  // Permission is hereby granted, free of charge, to any person obtaining a copy
     4  // of this software and associated documentation files (the "Software"), to deal
     5  // in the Software without restriction, including without limitation the rights
     6  // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
     7  // copies of the Software, and to permit persons to whom the Software is
     8  // furnished to do so, subject to the following conditions:
     9  //
    10  // The above copyright notice and this permission notice shall be included in
    11  // all copies or substantial portions of the Software.
    12  //
    13  // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    14  // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    15  // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    16  // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    17  // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    18  // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
    19  // THE SOFTWARE.
    20  
    21  package identity
    22  
    23  import (
    24  	"math"
    25  	"time"
    26  
    27  	"github.com/twmb/murmur3"
    28  )
    29  
    30  const (
    31  	_hashSeed uint64 = 23
    32  	_hashFold uint64 = 31
    33  )
    34  
    35  // Accumulator is a commutative folding accumulator.
    36  type Accumulator uint64
    37  
    38  // NewAccumulator creates a new Accumulator with a default seed value.
    39  //
    40  //	 n.b. Here and elsewhere, we use nosplit to avoid stack size checks, which
    41  //		are unnecessary as memory width is bounded to each instance of `a` (a
    42  //		uint64) and, potentially, a single stack-local loop temporary while
    43  //		iterating.
    44  func NewAccumulator() Accumulator {
    45  	return Accumulator(_hashSeed)
    46  }
    47  
    48  // NewAccumulatorWithSeed creates a new Accumulator with the provided seed value.
    49  func NewAccumulatorWithSeed(seed uint64) Accumulator {
    50  	return Accumulator(seed)
    51  }
    52  
    53  // AddString hashes str and folds it into the accumulator.
    54  func (a Accumulator) AddString(str string) Accumulator {
    55  	return a + Accumulator(murmur3.StringSum64(str)*_hashFold)
    56  }
    57  
    58  // AddUint64 folds u64 into the accumulator.
    59  func (a Accumulator) AddUint64(u64 uint64) Accumulator {
    60  	return a + Accumulator(u64*_hashFold)
    61  }
    62  
    63  // Value returns the accumulated value.
    64  func (a Accumulator) Value() uint64 {
    65  	return uint64(a)
    66  }
    67  
    68  // Durations returns the accumulated identity of durs.
    69  func Durations(durs []time.Duration) uint64 {
    70  	if len(durs) == 0 {
    71  		return 0
    72  	}
    73  
    74  	acc := NewAccumulator()
    75  
    76  	// n.b. Wrapping due to overflow is okay here, since those values cannot be
    77  	//      represented by int64.
    78  	for _, d := range durs {
    79  		acc = acc.AddUint64(uint64(d))
    80  	}
    81  
    82  	return acc.Value()
    83  }
    84  
    85  // Int64s returns the accumulated identity of i64s.
    86  func Int64s(i64s []int64) uint64 {
    87  	if len(i64s) == 0 {
    88  		return 0
    89  	}
    90  
    91  	acc := NewAccumulator()
    92  
    93  	// n.b. Wrapping due to overflow is okay here, since those values cannot be
    94  	//      represented by int64.
    95  	for _, i := range i64s {
    96  		acc = acc.AddUint64(uint64(i))
    97  	}
    98  
    99  	return acc.Value()
   100  }
   101  
   102  // Float64s returns the accumulated identity of f64s.
   103  func Float64s(f64s []float64) uint64 {
   104  	if len(f64s) == 0 {
   105  		return 0
   106  	}
   107  
   108  	// n.b. Wrapping due to overflow is okay here, since those values cannot be
   109  	//      represented by int64.
   110  	acc := NewAccumulator()
   111  
   112  	for _, f := range f64s {
   113  		acc = acc.AddUint64(math.Float64bits(f))
   114  	}
   115  
   116  	return acc.Value()
   117  }
   118  
   119  // StringStringMap returns the accumulated identity of m.
   120  func StringStringMap(m map[string]string) uint64 {
   121  	if len(m) == 0 {
   122  		return 0
   123  	}
   124  
   125  	acc := NewAccumulator()
   126  	for k, v := range m {
   127  		acc = acc.AddString(k + "=" + v)
   128  	}
   129  
   130  	return acc.Value()
   131  }