Skip to content

Commit 52780d4

Browse files
authored
refactor: use the built-in max/min to simplify the code (ethereum#3110)
1 parent a81e6fd commit 52780d4

File tree

4 files changed

+5
-17
lines changed

4 files changed

+5
-17
lines changed

common/fdlimit/fdlimit_darwin.go

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,17 +24,14 @@ const hardlimit = 10240
2424
// Raise tries to maximize the file descriptor allowance of this process
2525
// to the maximum hard-limit allowed by the OS.
2626
// Returns the size it was set to (may differ from the desired 'max')
27-
func Raise(max uint64) (uint64, error) {
27+
func Raise(maxVal uint64) (uint64, error) {
2828
// Get the current limit
2929
var limit syscall.Rlimit
3030
if err := syscall.Getrlimit(syscall.RLIMIT_NOFILE, &limit); err != nil {
3131
return 0, err
3232
}
3333
// Try to update the limit to the max allowance
34-
limit.Cur = limit.Max
35-
if limit.Cur > max {
36-
limit.Cur = max
37-
}
34+
limit.Cur = min(limit.Max, maxVal)
3835
if err := syscall.Setrlimit(syscall.RLIMIT_NOFILE, &limit); err != nil {
3936
return 0, err
4037
}

p2p/dnsdisc/tree.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -202,10 +202,7 @@ func (t *Tree) build(entries []entry) entry {
202202
}
203203
var subtrees []entry
204204
for len(entries) > 0 {
205-
n := maxChildren
206-
if len(entries) < n {
207-
n = len(entries)
208-
}
205+
n := min(len(entries), maxChildren)
209206
sub := t.build(entries[:n])
210207
entries = entries[n:]
211208
subtrees = append(subtrees, sub)

p2p/msgrate/msgrate.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -378,10 +378,7 @@ func (t *Trackers) TargetTimeout() time.Duration {
378378
// targetTimeout is the internal lockless version of TargetTimeout to be used
379379
// during QoS tuning.
380380
func (t *Trackers) targetTimeout() time.Duration {
381-
timeout := time.Duration(ttlScaling * float64(t.roundtrip) / t.confidence)
382-
if timeout > t.OverrideTTLLimit {
383-
timeout = t.OverrideTTLLimit
384-
}
381+
timeout := min(time.Duration(ttlScaling*float64(t.roundtrip)/t.confidence), t.OverrideTTLLimit)
385382
return timeout
386383
}
387384

rlp/decode.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -314,10 +314,7 @@ func decodeSliceElems(s *Stream, val reflect.Value, elemdec decoder) error {
314314
for ; ; i++ {
315315
// grow slice if necessary
316316
if i >= val.Cap() {
317-
newcap := val.Cap() + val.Cap()/2
318-
if newcap < 4 {
319-
newcap = 4
320-
}
317+
newcap := max(val.Cap()+val.Cap()/2, 4)
321318
newv := reflect.MakeSlice(val.Type(), val.Len(), newcap)
322319
reflect.Copy(newv, val)
323320
val.Set(newv)

0 commit comments

Comments
 (0)