Skip to content

Instantly share code, notes, and snippets.

@DennisPing
Created September 21, 2022 06:38
Show Gist options
  • Save DennisPing/e24019d2e2d5c357fabc7034e5e7102b to your computer and use it in GitHub Desktop.
Save DennisPing/e24019d2e2d5c357fabc7034e5e7102b to your computer and use it in GitHub Desktop.
Compiler magic?
func AppendDataNaive(packet1 []byte, packet2 []byte) uint16 {
// Do some initial work done with packet1 and packet2...
// Don't set any capacity, let Go auto-resize
data := make([]byte, 0)
data = append(data, packet1...)
data = append(data, packet2...)
// Do some calculations with 'data'
return calculatedValue
}
func AppendDataSmart(packet1 []byte, packet2 []byte) uint16 {
// Do some initial work done with packet1 and packet2...
// Make a slice with len = 0 but underlying capacity of len1 + len2
data := make([]byte, 0, len(packet1)+len(packet2))
data = append(data, packet1...)
data = append(data, packet2...)
// Do some calculations with 'data'
return calculatedValue
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment