Skip to content

Commit

Permalink
feat: add read buffers wherever ReadByte() is called
Browse files Browse the repository at this point in the history
  • Loading branch information
dirkmc committed Sep 22, 2022
1 parent 02d658f commit 17f0235
Showing 1 changed file with 15 additions and 5 deletions.
20 changes: 15 additions & 5 deletions v2/blockstore/readonly.go
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 17f0235

Please sign in to comment.