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

Add read buffers wherever ReadByte() is called repeatedly #334

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
20 changes: 15 additions & 5 deletions v2/blockstore/readonly.go
@@ -1,6 +1,7 @@
package blockstore

import (
"bufio"
"bytes"
"context"
"errors"
Expand Down Expand Up @@ -208,7 +209,10 @@ func (b *ReadOnly) readBlock(idx int64) (cid.Cid, []byte, error) {
if err != nil {
return cid.Cid{}, nil, err
}
return util.ReadNode(r, b.opts.ZeroLengthSectionAsEOF, b.opts.MaxAllowedSectionSize)
// ReadNode calls LdRead and CidFromBytes, which in turn repeatedly call
// ReadByte(), so add a buffer with at least the size of a uvarint + cid
rdr := bufio.NewReaderSize(r, 256)
return util.ReadNode(rdr, b.opts.ZeroLengthSectionAsEOF, b.opts.MaxAllowedSectionSize)
}

// DeleteBlock is unsupported and always errors.
Expand Down Expand Up @@ -251,12 +255,15 @@ func (b *ReadOnly) Has(ctx context.Context, key cid.Cid) (bool, error) {
fnErr = err
return false
}
_, err = varint.ReadUvarint(uar)
// ReadUvarint and CidFromReader repeatedly call ReadByte(), so buffer
// reads with a buffer that's at least as large as a uvarint + cid
r := bufio.NewReaderSize(uar, 256)
_, err = varint.ReadUvarint(r)
if err != nil {
fnErr = err
return false
}
_, readCid, err := cid.CidFromReader(uar)
_, readCid, err := cid.CidFromReader(r)
if err != nil {
fnErr = err
return false
Expand Down Expand Up @@ -364,12 +371,15 @@ func (b *ReadOnly) GetSize(ctx context.Context, key cid.Cid) (int, error) {
fnErr = err
return false
}
sectionLen, err := varint.ReadUvarint(rdr)
// ReadUvarint and CidFromReader repeatedly call ReadByte(), so buffer
// reads with a buffer that's at least as large as a uvarint + cid
r := bufio.NewReaderSize(rdr, 256)
sectionLen, err := varint.ReadUvarint(r)
if err != nil {
fnErr = err
return false
}
cidLen, readCid, err := cid.CidFromReader(rdr)
cidLen, readCid, err := cid.CidFromReader(r)
if err != nil {
fnErr = err
return false
Expand Down