-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement GPBFT message compression using zstd
Add the ability to compress GPBFT messages controllable via manifest. Implement benchmarks to compare vanilla CBOR and ZSTD encoding. Basic local run: ``` BenchmarkCborEncoding-12 47173 25491 ns/op 135409 B/op 87 allocs/op BenchmarkCborDecoding-12 64550 18078 ns/op 61728 B/op 209 allocs/op BenchmarkZstdEncoding-12 29061 41489 ns/op 193455 B/op 88 allocs/op BenchmarkZstdDecoding-12 66172 17924 ns/op 176517 B/op 211 allocs/op ``` Fixes #786
- Loading branch information
Showing
6 changed files
with
296 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
package f3 | ||
|
||
import ( | ||
"bytes" | ||
|
||
"github.com/filecoin-project/go-f3/gpbft" | ||
"github.com/klauspost/compress/zstd" | ||
) | ||
|
||
var ( | ||
_ gMessageEncoding = (*cborGMessageEncoding)(nil) | ||
_ gMessageEncoding = (*zstdGMessageEncoding)(nil) | ||
) | ||
|
||
type gMessageEncoding interface { | ||
Encode(*gpbft.GMessage) ([]byte, error) | ||
Decode([]byte) (*gpbft.GMessage, error) | ||
} | ||
|
||
type cborGMessageEncoding struct{} | ||
|
||
func (c *cborGMessageEncoding) Encode(m *gpbft.GMessage) ([]byte, error) { | ||
var buf bytes.Buffer | ||
if err := m.MarshalCBOR(&buf); err != nil { | ||
return nil, err | ||
} | ||
return buf.Bytes(), nil | ||
} | ||
|
||
func (c *cborGMessageEncoding) Decode(v []byte) (*gpbft.GMessage, error) { | ||
r := bytes.NewReader(v) | ||
var msg gpbft.GMessage | ||
if err := msg.UnmarshalCBOR(r); err != nil { | ||
return nil, err | ||
} | ||
return &msg, nil | ||
} | ||
|
||
type zstdGMessageEncoding struct { | ||
cborEncoding cborGMessageEncoding | ||
compressor *zstd.Encoder | ||
decompressor *zstd.Decoder | ||
} | ||
|
||
func newZstdGMessageEncoding() (*zstdGMessageEncoding, error) { | ||
writer, err := zstd.NewWriter(nil) | ||
if err != nil { | ||
return nil, err | ||
} | ||
reader, err := zstd.NewReader(nil) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return &zstdGMessageEncoding{ | ||
compressor: writer, | ||
decompressor: reader, | ||
}, nil | ||
} | ||
|
||
func (c *zstdGMessageEncoding) Encode(m *gpbft.GMessage) ([]byte, error) { | ||
cborEncoded, err := c.cborEncoding.Encode(m) | ||
if err != nil { | ||
return nil, err | ||
} | ||
compressed := c.compressor.EncodeAll(cborEncoded, make([]byte, 0, len(cborEncoded))) | ||
return compressed, err | ||
} | ||
|
||
func (c *zstdGMessageEncoding) Decode(v []byte) (*gpbft.GMessage, error) { | ||
cborEncoded, err := c.decompressor.DecodeAll(v, make([]byte, 0, len(v))) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return c.cborEncoding.Decode(cborEncoded) | ||
} |
Oops, something went wrong.