Skip to content

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

Merged
zzzckck merged 1 commit intobnb-chain:developfrom
carrychair:develop
May 22, 2025
Merged

refactor: use the built-in max/min to simplify the code#3110
zzzckck merged 1 commit intobnb-chain:developfrom
carrychair:develop

Conversation

@carrychair
Copy link
Copy Markdown
Contributor

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:

  • add each change in a bullet point here
  • ...

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

Signed-off-by: carrychair <linghuchong404@gmail.com>
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.go with max
  • Simplify timeout and subtree sizing in p2p/msgrate and p2p/dnsdisc using min
  • Update common/fdlimit to use min for 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 max function 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 min helper supports time.Duration operands. 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 zzzckck merged commit 52780d4 into bnb-chain:develop May 22, 2025
7 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants