refactor: use the built-in max/min to simplify the code#3110
Merged
zzzckck merged 1 commit intobnb-chain:developfrom May 22, 2025
Merged
refactor: use the built-in max/min to simplify the code#3110zzzckck merged 1 commit intobnb-chain:developfrom
zzzckck merged 1 commit intobnb-chain:developfrom
Conversation
refactor: use the built-in max/min to simplify the code Signed-off-by: carrychair <linghuchong404@gmail.com>
Contributor
There was a problem hiding this comment.
Pull Request Overview
This PR refactors several modules to use centralized max/min calls, simplifying manual bound calculations and reducing boilerplate.
- Consolidate capacity-growth logic in
rlp/decode.gowithmax - Simplify timeout and subtree sizing in
p2p/msgrateandp2p/dnsdiscusingmin - Update
common/fdlimitto useminfor RLIMIT and rename parameter for clarity
Reviewed Changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
| rlp/decode.go | Replace slice-capacity growth branching with newcap := max(...) |
| p2p/msgrate/msgrate.go | Simplify TTL timeout cap logic to min(...) |
| p2p/dnsdisc/tree.go | Refactor subtree chunk size to n := min(...) |
| common/fdlimit/fdlimit_darwin.go | Use min for limit.Cur and rename max parameter to maxVal |
Comments suppressed due to low confidence (3)
rlp/decode.go:316
- Add unit tests to cover both branches of the slice-growth logic (when capacity is below and above the 4-element threshold) to ensure the new
max-based approach behaves identically to the previous implementation.
// grow slice if necessary
rlp/decode.go:317
- Ensure that the
maxfunction is defined or imported for integer arguments; otherwise this will cause an undefined identifier compile error. If you rely on a custom helper, add the appropriate import or definition.
newcap := max(val.Cap()+val.Cap()/2, 4)
p2p/msgrate/msgrate.go:381
- Verify that your
minhelper supportstime.Durationoperands. If it only handles numeric types (e.g.,int), you may need to overload it or use an explicit conditional to avoid a type mismatch.
timeout := min(time.Duration(ttlScaling*float64(t.roundtrip)/t.confidence), t.OverrideTTLLimit)
zzzckck
approved these changes
May 22, 2025
MatusKysel
approved these changes
May 22, 2025
This was referenced May 28, 2025
Merged
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
use the built-in max/min to simplify the code
Rationale
tell us why we need these changes...
Example
add an example CLI or API response...
Changes
Notable changes: