Skip to content

refactor: use the built-in max/min to simplify the code #2401

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
30 changes: 6 additions & 24 deletions common/bitutil/bitutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,7 @@ func XORBytes(dst, a, b []byte) int {
// fastXORBytes xors in bulk. It only works on architectures that support
// unaligned read/writes.
func fastXORBytes(dst, a, b []byte) int {
n := len(a)
if len(b) < n {
n = len(b)
}
n := min(len(b), len(a))
w := n / wordSize
if w > 0 {
dw := *(*[]uintptr)(unsafe.Pointer(&dst))
Expand All @@ -49,10 +46,7 @@ func fastXORBytes(dst, a, b []byte) int {
// safeXORBytes xors one by one. It works on all architectures, independent if
// it supports unaligned read/writes or not.
func safeXORBytes(dst, a, b []byte) int {
n := len(a)
if len(b) < n {
n = len(b)
}
n := min(len(b), len(a))
for i := 0; i < n; i++ {
dst[i] = a[i] ^ b[i]
}
Expand All @@ -71,10 +65,7 @@ func ANDBytes(dst, a, b []byte) int {
// fastANDBytes ands in bulk. It only works on architectures that support
// unaligned read/writes.
func fastANDBytes(dst, a, b []byte) int {
n := len(a)
if len(b) < n {
n = len(b)
}
n := min(len(b), len(a))
w := n / wordSize
if w > 0 {
dw := *(*[]uintptr)(unsafe.Pointer(&dst))
Expand All @@ -93,10 +84,7 @@ func fastANDBytes(dst, a, b []byte) int {
// safeANDBytes ands one by one. It works on all architectures, independent if
// it supports unaligned read/writes or not.
func safeANDBytes(dst, a, b []byte) int {
n := len(a)
if len(b) < n {
n = len(b)
}
n := min(len(b), len(a))
for i := 0; i < n; i++ {
dst[i] = a[i] & b[i]
}
Expand All @@ -115,10 +103,7 @@ func ORBytes(dst, a, b []byte) int {
// fastORBytes ors in bulk. It only works on architectures that support
// unaligned read/writes.
func fastORBytes(dst, a, b []byte) int {
n := len(a)
if len(b) < n {
n = len(b)
}
n := min(len(b), len(a))
w := n / wordSize
if w > 0 {
dw := *(*[]uintptr)(unsafe.Pointer(&dst))
Expand All @@ -137,10 +122,7 @@ func fastORBytes(dst, a, b []byte) int {
// safeORBytes ors one by one. It works on all architectures, independent if
// it supports unaligned read/writes or not.
func safeORBytes(dst, a, b []byte) int {
n := len(a)
if len(b) < n {
n = len(b)
}
n := min(len(b), len(a))
for i := 0; i < n; i++ {
dst[i] = a[i] | b[i]
}
Expand Down
5 changes: 1 addition & 4 deletions common/fdlimit/fdlimit_darwin.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,7 @@ func Raise(max uint64) (uint64, error) {
return 0, err
}
// Try to update the limit to the max allowance
limit.Cur = limit.Max
if limit.Cur > max {
limit.Cur = max
}
limit.Cur = min(limit.Max, max)
if err := syscall.Setrlimit(syscall.RLIMIT_NOFILE, &limit); err != nil {
return 0, err
}
Expand Down
5 changes: 1 addition & 4 deletions common/hexutil/hexutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,10 +147,7 @@ func DecodeBig(input string) (*big.Int, error) {
words := make([]big.Word, len(raw)/bigWordNibbles+1)
end := len(raw)
for i := range words {
start := end - bigWordNibbles
if start < 0 {
start = 0
}
start := max(end-bigWordNibbles, 0)
for ri := start; ri < end; ri++ {
nib := decodeNibble(raw[ri])
if nib == badNibble {
Expand Down
5 changes: 1 addition & 4 deletions common/hexutil/json.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,10 +160,7 @@ func (b *Big) UnmarshalText(input []byte) error {
words := make([]big.Word, len(raw)/bigWordNibbles+1)
end := len(raw)
for i := range words {
start := end - bigWordNibbles
if start < 0 {
start = 0
}
start := max(end-bigWordNibbles, 0)
for ri := start; ri < end; ri++ {
nib := decodeNibble(raw[ri])
if nib == badNibble {
Expand Down
Loading