this repo has no description
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

pkg/math/bits: avoid an extra allocation per call

These functions implement builtins. In the process of converting their
arguments from CUE values to Go values, we already allocate bit.Ints;
hence, it's fine to reuse the first argument to store the result,
because we don't need to keep the first argument around.

If keeping the original value is needed by a caller in the future,
it's always possible to make a copy of it before the call.

│ old │ new │
│ B/op │ B/op vs base │
VetInventory 4.601Gi ± ∞ ¹ 4.593Gi ± ∞ ¹ -0.18% (p=1.000 n=1)

│ old │ new │
│ allocs/op │ allocs/op vs base │
VetInventory 48.33M ± ∞ ¹ 47.92M ± ∞ ¹ -0.85% (p=1.000 n=1)

Signed-off-by: Daniel Martí <mvdan@mvdan.cc>
Change-Id: I6cf680cfae31732985900e25abd05c778ed01f34
Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/1229640
Unity-Result: CUE porcuepine <cue.porcuepine@gmail.com>
TryBot-Result: CUEcueckoo <cueckoo@cuelang.org>
Reviewed-by: Matthew Sackman <matthew@cue.works>

+16 -30
+16 -30
pkg/math/bits/manual.go
··· 25 25 "math/bits" 26 26 ) 27 27 28 - // Lsh returns x shifted left by n bits. 28 + // Lsh sets and returns x shifted left by n bits. 29 29 func Lsh(x *big.Int, n uint) *big.Int { 30 - var z big.Int 31 - z.Lsh(x, n) 32 - return &z 30 + return x.Lsh(x, n) 33 31 } 34 32 35 - // Rsh returns x shifted right by n bits. 33 + // Rsh sets and returns x shifted right by n bits. 36 34 func Rsh(x *big.Int, n uint) *big.Int { 37 - var z big.Int 38 - z.Rsh(x, n) 39 - return &z 35 + return x.Rsh(x, n) 40 36 } 41 37 42 38 // At returns the value of the i'th bit of x. ··· 47 43 return x.Bit(int(i)), nil 48 44 } 49 45 50 - // SetBit returns x with x's i'th bit set to b (0 or 1). That is, if b is 1 51 - // SetBit returns x with its i'th bit set; if b is 0 SetBit returns x with 52 - // its i'th bit cleared. 46 + // Set sets and returns x with x's i'th bit set to b (0 or 1). 47 + // That is, if b is 1 Set returns x with its i'th bit set; 48 + // if b is 0 Set returns x with its i'th bit cleared. 53 49 func Set(x *big.Int, i int, bit uint) *big.Int { 54 - var z big.Int 55 - z.SetBit(x, i, bit) 56 - return &z 50 + return x.SetBit(x, i, bit) 57 51 } 58 52 59 - // And returns the bitwise and of a and b. 53 + // And sets and returns a to the bitwise "and" of a and b. 60 54 func And(a, b *big.Int) *big.Int { 61 - var z big.Int 62 - z.And(a, b) 63 - return &z 55 + return a.And(a, b) 64 56 } 65 57 66 - // Or returns the bitwise or of a and b (a | b in Go). 58 + // Or sets and returns a to the bitwise "or" of a and b (a | b in Go). 67 59 func Or(a, b *big.Int) *big.Int { 68 - var z big.Int 69 - z.Or(a, b) 70 - return &z 60 + return a.Or(a, b) 71 61 } 72 62 73 - // Xor returns the bitwise xor of a and b (a ^ b in Go). 63 + // Xor sets and returns a to the bitwise xor of a and b (a ^ b in Go). 74 64 func Xor(a, b *big.Int) *big.Int { 75 - var z big.Int 76 - z.Xor(a, b) 77 - return &z 65 + return a.Xor(a, b) 78 66 } 79 67 80 - // Clear returns the bitwise and not of a and b (a &^ b in Go). 68 + // Clear sets and returns a to the bitwise "and not" of a and b (a &^ b in Go). 81 69 func Clear(a, b *big.Int) *big.Int { 82 - var z big.Int 83 - z.AndNot(a, b) 84 - return &z 70 + return a.AndNot(a, b) 85 71 } 86 72 87 73 // OnesCount returns the number of one bits ("population count") in x.