remove unnecessary slice boxing

This commit is contained in:
David 2021-12-20 18:42:36 -05:00
parent 44c8a7659d
commit 562fc231a4

View File

@ -95,9 +95,9 @@ func parsePayload(stream []uint8) (int64, int) {
return parseNumber(num, chars), pos
}
func parsePackets(stream []uint8) (int, []packet) {
func parsePackets(stream []uint8) (int, packet) {
if len(stream) <= 7 {
return 0, []packet{}
return 0, packet{}
}
parsedPacket := packet{}
parsedPacket.version = getPacketVersion(stream[0:3])
@ -105,29 +105,28 @@ func parsePackets(stream []uint8) (int, []packet) {
if parsedPacket.typeID == 4 {
encoded, consumed := parsePayload(stream[6:])
parsedPacket.encodedNumber = encoded
return consumed + 6, []packet{parsedPacket} // 6 is packet header length
return consumed + 6, parsedPacket // 6 is packet header length
} else {
parsed := 0
size, subpacketlength := getSubpacketLength(stream[6], stream[7:])
for {
consumed, subs := parsePackets(stream[7+size+parsed:])
parsedPacket.subPackets = append(parsedPacket.subPackets, subs...)
parsedPacket.subPackets = append(parsedPacket.subPackets, subs)
parsed += consumed
if size == 11 && int64(len(parsedPacket.subPackets)) == subpacketlength {
return parsed + 7 + size, []packet{parsedPacket}
return parsed + 7 + size, parsedPacket
} else if size == 15 && int64(parsed) > subpacketlength || consumed == 0 {
return parsed + 7 + size, []packet{parsedPacket}
return parsed + 7 + size, parsedPacket
}
}
}
}
func countVersions(packets []packet) int {
i := 0
for _, p := range packets {
i += int(p.version)
i += countVersions(p.subPackets)
func countVersions(pac packet) int {
i := int(pac.version)
for _, p := range pac.subPackets {
i += countVersions(p)
}
return i
}
@ -142,6 +141,7 @@ func partOne() {
stream = append(stream, hexMap[hex]...)
}
_, parsedPacket := parsePackets(stream)
fmt.Println(parsedPacket)
fmt.Println(countVersions(parsedPacket))
}