Skip to content

Commit

Permalink
update to huffman v2 - removes tree initialization nearly completely
Browse files Browse the repository at this point in the history
  • Loading branch information
jxsl13 committed Sep 18, 2024
1 parent 0b7a04a commit 304d32c
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 9 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ module github.com/teeworlds-go/protocol

go 1.22.3

require github.com/teeworlds-go/huffman v1.0.0
require github.com/teeworlds-go/huffman/v2 v2.0.0
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
github.com/teeworlds-go/huffman v1.0.0 h1:XSNMNAJZb+njNrPACsxcDrrLDXTGjZZt35FqLAuHi4I=
github.com/teeworlds-go/huffman v1.0.0/go.mod h1:kjaXpL6C6xL7CM+tWPNYjdEgVZB2GumKhx7rCDdXArU=
github.com/teeworlds-go/huffman/v2 v2.0.0 h1:eWmQe6OZ7o6Qn75ErZ0UeM+fnT0Gz6XLx0dlO2AtJ4U=
github.com/teeworlds-go/huffman/v2 v2.0.0/go.mod h1:qrL61bcSz/NZ1mQyFjAOcbza7acJQj2ltwdjetLHsOY=
9 changes: 3 additions & 6 deletions protocol7/packet.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"fmt"
"slices"

"github.com/teeworlds-go/huffman"
"github.com/teeworlds-go/huffman/v2"
"github.com/teeworlds-go/protocol/chunk7"
"github.com/teeworlds-go/protocol/messages7"
"github.com/teeworlds-go/protocol/network7"
Expand Down Expand Up @@ -336,8 +336,7 @@ func (packet *Packet) Unpack(data []byte) (err error) {
if packet.Header.Flags.Compression {
// TODO: try avoiding repeated initialization of the huffman tree structure
// move this into the Packet struct or even further up
huff := huffman.Huffman{}
payload, err = huff.Decompress(payload)
payload, err = huffman.Decompress(payload)
if err != nil {
return err
}
Expand Down Expand Up @@ -373,10 +372,8 @@ func (packet *Packet) Pack(connection *Session) []byte {
}

if packet.Header.Flags.Compression {
// TODO: store huffman object in connection to avoid reallocating memory
huff := huffman.Huffman{}
var err error
payload, err = huff.Compress(payload)
payload, err = huffman.Compress(payload)
if err != nil {
panic(err)
}
Expand Down

0 comments on commit 304d32c

Please sign in to comment.