-
Notifications
You must be signed in to change notification settings - Fork 7
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
Integrate zstd compression into chain exchange #842
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
package encoding | ||
|
||
import ( | ||
"bytes" | ||
"fmt" | ||
|
||
"github.com/klauspost/compress/zstd" | ||
cbg "github.com/whyrusleeping/cbor-gen" | ||
) | ||
|
||
// maxDecompressedSize is the default maximum amount of memory allocated by the | ||
// zstd decoder. The limit of 1MiB is chosen based on the default maximum message | ||
// size in GossipSub. | ||
const maxDecompressedSize = 1 << 20 | ||
|
||
type CBORMarshalUnmarshaler interface { | ||
cbg.CBORMarshaler | ||
cbg.CBORUnmarshaler | ||
} | ||
|
||
type EncodeDecoder[T CBORMarshalUnmarshaler] interface { | ||
Encode(v T) ([]byte, error) | ||
Decode([]byte, T) error | ||
} | ||
|
||
type CBOR[T CBORMarshalUnmarshaler] struct{} | ||
|
||
func NewCBOR[T CBORMarshalUnmarshaler]() *CBOR[T] { | ||
return &CBOR[T]{} | ||
} | ||
|
||
func (c *CBOR[T]) Encode(m T) ([]byte, error) { | ||
var buf bytes.Buffer | ||
if err := m.MarshalCBOR(&buf); err != nil { | ||
return nil, err | ||
} | ||
return buf.Bytes(), nil | ||
} | ||
|
||
func (c *CBOR[T]) Decode(v []byte, t T) error { | ||
r := bytes.NewReader(v) | ||
return t.UnmarshalCBOR(r) | ||
} | ||
|
||
type ZSTD[T CBORMarshalUnmarshaler] struct { | ||
cborEncoding *CBOR[T] | ||
compressor *zstd.Encoder | ||
decompressor *zstd.Decoder | ||
} | ||
|
||
func NewZSTD[T CBORMarshalUnmarshaler]() (*ZSTD[T], error) { | ||
writer, err := zstd.NewWriter(nil) | ||
if err != nil { | ||
return nil, err | ||
} | ||
reader, err := zstd.NewReader(nil, zstd.WithDecoderMaxMemory(maxDecompressedSize)) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return &ZSTD[T]{ | ||
cborEncoding: &CBOR[T]{}, | ||
compressor: writer, | ||
decompressor: reader, | ||
}, nil | ||
} | ||
|
||
func (c *ZSTD[T]) Encode(m T) ([]byte, error) { | ||
cborEncoded, err := c.cborEncoding.Encode(m) | ||
if len(cborEncoded) > maxDecompressedSize { | ||
// Error out early if the encoded value is too large to be decompressed. | ||
return nil, fmt.Errorf("encoded value cannot exceed maximum size: %d > %d", len(cborEncoded), maxDecompressedSize) | ||
} | ||
if err != nil { | ||
return nil, err | ||
} | ||
compressed := c.compressor.EncodeAll(cborEncoded, make([]byte, 0, len(cborEncoded))) | ||
return compressed, nil | ||
} | ||
|
||
func (c *ZSTD[T]) Decode(v []byte, t T) error { | ||
cborEncoded, err := c.decompressor.DecodeAll(v, make([]byte, 0, len(v))) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Future change: we should use a buffer pool for these short-lived buffers (https://pkg.go.dev/sync#Pool). If we do that, we can also allocate these buffers with 1MiB capacities and use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
if err != nil { | ||
return err | ||
} | ||
return c.cborEncoding.Decode(cborEncoded, t) | ||
} |
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.
Are we going with ZSTD by default?
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.
For chain exchange yes. For GPBFT it's configurable via manifest.
Happy to make it configurable for chain exchange too if you think it's worth doing.