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

Simplify the bytes decoding of chunks #607

Merged
merged 4 commits into from
Jun 18, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 10 additions & 21 deletions node/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"context"
"encoding/binary"
"errors"
"io"
"time"

"github.com/Layr-Labs/eigenda/api/grpc/node"
Expand Down Expand Up @@ -380,32 +379,22 @@ func EncodeChunks(chunks [][]byte) ([]byte, error) {
//
// decodeChunks((len(chunks[0]), chunks[0], len(chunks[1]), chunks[1], ...)) = chunks
func decodeChunks(data []byte) ([][]byte, error) {
buf := bytes.NewReader(data)
chunks := make([][]byte, 0)

for {
var length uint64
err := binary.Read(buf, binary.LittleEndian, &length)
if errors.Is(err, io.EOF) {
break
}
if err != nil {
return nil, err
buf := data
for len(buf) > 0 {
if len(buf) < 8 {
return nil, errors.New("invalid data to decode")
}
chunkSize := binary.LittleEndian.Uint64(buf)
buf = buf[8:]

chunk := make([]byte, length)
_, err = buf.Read(chunk)
if errors.Is(err, io.EOF) {
break
}
if err != nil {
return nil, err
if len(buf) < int(chunkSize) {
return nil, errors.New("invalid data to decode")
}
chunk := buf[:chunkSize]
Copy link
Contributor Author

Choose a reason for hiding this comment

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

It should actually also be much more efficient, as this doesn't need to copy chunks (no memory allocation).

buf = buf[chunkSize:]

chunks = append(chunks, chunk)
if buf.Len() < 8 {
break
}
}

return chunks, nil
Expand Down
Loading