Skip to content
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

[5/N] Chunk encoding optimization: Add support of new encoding at Node #636

Merged
merged 6 commits into from
Jul 17, 2024

Conversation

jianoaix
Copy link
Contributor

@jianoaix jianoaix commented Jul 12, 2024

Why are these changes needed?

Support both gob and gnark encoding at Node.

Checks

  • I've made sure the lint is passing in this PR.
  • I've made sure the tests are passing. Note that there might be a few flaky tests, in that case, please comment that they are not relevant.
  • Testing Strategy
    • Unit tests
    • Integration tests
    • This PR is not tested :(

@@ -198,7 +206,7 @@ func (b Bundle) Serialize() ([]byte, error) {
}
result := make([]byte, size+8)
buf := result
metadata := uint64(GnarkBundleEncodingFormat) | (uint64(len(b[0].Coeffs)) << 8)
metadata := (uint64(GnarkBundleEncodingFormat) << (NumBundleHeaderBits - NumBundleEncodingFormatBits)) | uint64(len(b[0].Coeffs))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So you are putting length at lower address, what is the motivation for the change

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To make it compatible with existing chunk encoding, so that the same algo can be used to parse both old and new encoding

core/data.go Outdated
return nil, errors.New("invalid bundle data encoding format")
}
chunkLen := meta >> 8
chunkLen := meta << NumBundleEncodingFormatBits >> NumBundleEncodingFormatBits
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

would be good to add (), or comment on the precedence on the binary operations are from left to right

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

bundles[uint8(quorumID)] = make([]*encoding.Frame, len(chunks.GetChunks()))
for k, data := range chunks.GetChunks() {
chunk, err := new(encoding.Frame).Deserialize(data)
if len(bundle.GetBundle()) > 0 {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we have a get format function, it looks a bit adhoc

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is no field to indicate the format, it's checking bundle field and then fallback to the old chunk encoding

if err != nil {
return nil, err
if usingBundleBytes {
rawBundle, ok := rawBundles[quorumID]
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

rawBundle will have zero-copy and hence more efficient compared to the old encoding below (which has to allocate memory to concat chunks)

core/data.go Outdated
return nil, errors.New("invalid bundle data encoding format")
}
chunkLen := meta >> 8
chunkLen := meta << NumBundleEncodingFormatBits >> NumBundleEncodingFormatBits
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

@@ -198,7 +206,7 @@ func (b Bundle) Serialize() ([]byte, error) {
}
result := make([]byte, size+8)
buf := result
metadata := uint64(GnarkBundleEncodingFormat) | (uint64(len(b[0].Coeffs)) << 8)
metadata := (uint64(GnarkBundleEncodingFormat) << (NumBundleHeaderBits - NumBundleEncodingFormatBits)) | uint64(len(b[0].Coeffs))
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To make it compatible with existing chunk encoding, so that the same algo can be used to parse both old and new encoding

Copy link
Contributor

@ian-shim ian-shim left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice!

if err != nil {
resultChan <- err
return
}
bundles[uint8(quorumID)][k] = chunk
bundles[uint8(quorumID)] = bundleMsg
} else {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: else if format == core.GobBundleEncodingFormat
then add else block that throws unexpected encoding format error

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

if err != nil {
resultChan <- err
return
}
bundles[uint8(quorumID)][k] = chunk
bundles[uint8(quorumID)] = bundleMsg
} else {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

for k, data := range chunks.GetChunks() {
chunk, err := new(encoding.Frame).Deserialize(data)
if format == core.GnarkBundleEncodingFormat {
bundleMsg, err := new(core.Bundle).Deserialize(bundle.GetBundle())
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This deserialization will be 3.6x faster (#624)

@jianoaix jianoaix merged commit 00bdade into Layr-Labs:master Jul 17, 2024
6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants