瀏覽代碼

Merge pull request #755 from Zariel/reduce-hash-allocs

token: dont allocate maxint every call
Chris Bannister 9 年之前
父節點
當前提交
94f1315e46
共有 1 個文件被更改,包括 4 次插入5 次删除
  1. 4 5
      token.go

+ 4 - 5
token.go

@@ -89,16 +89,15 @@ func (r randomPartitioner) Name() string {
 	return "RandomPartitioner"
 }
 
-func (p randomPartitioner) Hash(partitionKey []byte) token {
-	// 2 ** 128
-	maxInt := new(big.Int)
-	maxInt.SetString("340282366920938463463374607431768211456", 10)
+// 2 ** 128
+var maxHashInt, _ = new(big.Int).SetString("340282366920938463463374607431768211456", 10)
 
+func (p randomPartitioner) Hash(partitionKey []byte) token {
 	sum := md5.Sum(partitionKey)
 	val := new(big.Int)
 	val.SetBytes(sum[:])
 	if sum[0] > 127 {
-		val.Sub(val, maxInt)
+		val.Sub(val, maxHashInt)
 		val.Abs(val)
 	}