-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
device: reduce RoutineHandshake allocations #116
base: master
Are you sure you want to change the base?
device: reduce RoutineHandshake allocations #116
Conversation
Reduce allocations by eliminating byte reader, hand-rolled decoding and reusing message structs. Signed-off-by: Alexander Yastrebov <[email protected]>
func (msg *MessageInitiation) unmarshal(b []byte) error { | ||
if len(b) < MessageInitiationSize { | ||
return errMessageTooShort | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe name it reset
.
Also the size of the packet is checked earlier
wireguard-go/device/receive.go
Lines 191 to 204 in 12269c2
case MessageInitiationType: | |
if len(packet) != MessageInitiationSize { | |
continue | |
} | |
case MessageResponseType: | |
if len(packet) != MessageResponseSize { | |
continue | |
} | |
case MessageCookieReplyType: | |
if len(packet) != MessageCookieReplySize { | |
continue | |
} |
so this check could be removed as well as error result check at the callsite.
@@ -286,17 +290,15 @@ func (device *Device) RoutineHandshake(id int) { | |||
|
|||
// unmarshal packet | |||
|
|||
var reply MessageCookieReply | |||
reader := bytes.NewReader(elem.packet) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here byte reader can be replaced with https://pkg.go.dev/encoding/binary#Decode added in 1.23 golang/go#60023 (comment)
Reduce allocations by eliminating byte reader, hand-rolled decoding and reusing message structs.