Skip to content

Conversation

gjcairo
Copy link
Collaborator

@gjcairo gjcairo commented Feb 11, 2025

PeerAddress/init currently uses String and splits them, which is not very optimal. This PR uses UTF8View instead to avoid allocations/make things faster

@gjcairo gjcairo added the 🔨 semver/patch No public API change. label Feb 11, 2025
@gjcairo gjcairo requested a review from glbrntt February 11, 2025 17:22
Copy link
Collaborator

@glbrntt glbrntt left a comment

Choose a reason for hiding this comment

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

Nice change! Left a few nits but we also need to be a little more careful parsing the port.

Comment on lines 205 to 208
guard elementValue >= 0, elementValue <= 9 else {
// non-digit character
return nil
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

At the moment if utf8Element is e.g. 47 then we'll underflow and crash when creating elementValue.

We should validate the UInt8 value and then do the conversion. It also means that the subtraction in the conversion can be unchecked.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I thought maybe using a range instead was clearer so I've done, but I'm actually unsure if the range allocates or if it's all nicely optimised by the compiler.

@gjcairo gjcairo requested a review from glbrntt February 13, 2025 13:50
Copy link
Collaborator

@glbrntt glbrntt left a comment

Choose a reason for hiding this comment

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

One nit, but otherwise looks great!

value &+= Int(utf8Char)
}

guard value <= 65535 else {
Copy link
Collaborator

Choose a reason for hiding this comment

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

nit: let the compiler do the maths

Suggested change
guard value <= 65535 else {
guard value <= Int(UInt16.max) else {

@glbrntt glbrntt linked an issue Feb 13, 2025 that may be closed by this pull request
@gjcairo gjcairo enabled auto-merge (squash) February 13, 2025 17:04
@gjcairo gjcairo requested a review from glbrntt February 14, 2025 08:51
// non-digit character
return nil
}
value &+= Int(utf8Char - UInt8(ascii: "0"))
Copy link
Collaborator

Choose a reason for hiding this comment

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

we can do unchecked here because we checked the range of values above

Suggested change
value &+= Int(utf8Char - UInt8(ascii: "0"))
value &+= Int(utf8Char &- UInt8(ascii: "0"))

@gjcairo gjcairo merged commit f41ab27 into grpc:main Feb 14, 2025
21 checks passed
@gjcairo gjcairo deleted the peeraddress-utf8view branch February 14, 2025 11:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🔨 semver/patch No public API change.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Avoid unnecessary allocations in PeerAddress/init
2 participants