Skip to content

Commit

Permalink
feat(memory): improve memory usage
Browse files Browse the repository at this point in the history
reduce copies of temporary buffers in message write and unverified block store
  • Loading branch information
hannahhoward committed Oct 27, 2020
1 parent ea899f5 commit 55ed84a
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 5 deletions.
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ require (
github.com/ipfs/go-blockservice v0.1.3
github.com/ipfs/go-cid v0.0.6
github.com/ipfs/go-datastore v0.4.4
github.com/ipfs/go-ds-badger v0.2.1
github.com/ipfs/go-ipfs-blockstore v0.1.4
github.com/ipfs/go-ipfs-blocksutil v0.0.1
github.com/ipfs/go-ipfs-chunker v0.0.5
Expand All @@ -29,6 +30,7 @@ require (
github.com/ipld/go-ipld-prime-proto v0.0.0-20200922192210-9a2bfd4440a6
github.com/jbenet/go-random v0.0.0-20190219211222-123a90aedc0c
github.com/jbenet/goprocess v0.1.4 // indirect
github.com/libp2p/go-buffer-pool v0.0.2
github.com/libp2p/go-libp2p v0.6.0
github.com/libp2p/go-libp2p-core v0.5.0
github.com/libp2p/go-libp2p-netutil v0.1.0
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
cloud.google.com/go v0.26.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw=
github.com/AndreasBriese/bbloom v0.0.0-20180913140656-343706a395b7/go.mod h1:bOvUY6CB00SOBii9/FifXqc0awNKxLFCL/+pkDPuyl8=
github.com/AndreasBriese/bbloom v0.0.0-20190306092124-e2d15f34fcf9 h1:HD8gA2tkByhMAwYaFAX9w2l7vxvBQ5NMoxDrkhqhtn4=
github.com/AndreasBriese/bbloom v0.0.0-20190306092124-e2d15f34fcf9/go.mod h1:bOvUY6CB00SOBii9/FifXqc0awNKxLFCL/+pkDPuyl8=
github.com/BurntSushi/toml v0.3.1 h1:WXkYYl6Yr3qBf1K79EBnL4mak0OimBfB0XUf9Vl28OQ=
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
Expand Down Expand Up @@ -168,6 +169,7 @@ github.com/ipfs/go-detect-race v0.0.1 h1:qX/xay2W3E4Q1U7d9lNs1sU9nvguX0a7319XbyQ
github.com/ipfs/go-detect-race v0.0.1/go.mod h1:8BNT7shDZPo99Q74BpGMK+4D8Mn4j46UU0LZ723meps=
github.com/ipfs/go-ds-badger v0.0.2/go.mod h1:Y3QpeSFWQf6MopLTiZD+VT6IC1yZqaGmjvRcKeSGij8=
github.com/ipfs/go-ds-badger v0.0.5/go.mod h1:g5AuuCGmr7efyzQhLL8MzwqcauPojGPUaHzfGTzuE3s=
github.com/ipfs/go-ds-badger v0.2.1 h1:RsC9DDlwFhFdfT+s2PeC8joxbSp2YMufK8w/RBOxKtk=
github.com/ipfs/go-ds-badger v0.2.1/go.mod h1:Tx7l3aTph3FMFrRS838dcSJh+jjA7cX9DrGVwx/NOwE=
github.com/ipfs/go-ds-leveldb v0.0.1/go.mod h1:feO8V3kubwsEF22n0YRQCffeb79OOYIykR4L04tMOYc=
github.com/ipfs/go-ds-leveldb v0.4.1/go.mod h1:jpbku/YqBSsBc1qgME8BkWS4AxzF2cEu1Ii2r79Hh9s=
Expand Down
14 changes: 11 additions & 3 deletions message/message.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
package message

import (
"encoding/binary"
"errors"
"fmt"
"io"

ggio "github.com/gogo/protobuf/io"
blocks "github.com/ipfs/go-block-format"
cid "github.com/ipfs/go-cid"
"github.com/ipld/go-ipld-prime"
pool "github.com/libp2p/go-buffer-pool"
"github.com/libp2p/go-libp2p-core/network"
"github.com/libp2p/go-msgio"
"google.golang.org/protobuf/proto"
Expand Down Expand Up @@ -346,12 +347,19 @@ func (gsm *graphSyncMessage) ToProto() (*pb.Message, error) {
}

func (gsm *graphSyncMessage) ToNet(w io.Writer) error {
pbw := ggio.NewDelimitedWriter(w)
msg, err := gsm.ToProto()
size := proto.Size(msg)
buf := pool.Get(size + binary.MaxVarintLen64)
defer pool.Put(buf)

n := binary.PutUvarint(buf, uint64(size))

out, err := proto.MarshalOptions{}.MarshalAppend(buf[:n], msg)
if err != nil {
return err
}
return pbw.WriteMsg(msg)
_, err = w.Write(out)
return err
}

func (gsm *graphSyncMessage) Loggable() map[string]interface{} {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ import (
ipld "github.com/ipld/go-ipld-prime"
)

type settableWriter interface {
SetBytes([]byte) error
}

// UnverifiedBlockStore holds an in memory cache of receied blocks from the network
// that have not been verified to be part of a traversal
type UnverifiedBlockStore struct {
Expand Down Expand Up @@ -55,7 +59,11 @@ func (ubs *UnverifiedBlockStore) VerifyBlock(lnk ipld.Link) ([]byte, error) {
if err != nil {
return nil, err
}
_, err = buffer.Write(data)
if settable, ok := buffer.(settableWriter); ok {
err = settable.SetBytes(data)
} else {
_, err = buffer.Write(data)
}
if err != nil {
return nil, err
}
Expand Down
21 changes: 20 additions & 1 deletion storeutil/storeutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func LoaderForBlockstore(bs bstore.Blockstore) ipld.Loader {
// from an IPFS blockstore
func StorerForBlockstore(bs bstore.Blockstore) ipld.Storer {
return func(lnkCtx ipld.LinkContext) (io.Writer, ipld.StoreCommitter, error) {
var buffer bytes.Buffer
var buffer settableBuffer
committer := func(lnk ipld.Link) error {
asCidLink, ok := lnk.(cidlink.Link)
if !ok {
Expand All @@ -46,3 +46,22 @@ func StorerForBlockstore(bs bstore.Blockstore) ipld.Storer {
return &buffer, committer, nil
}
}

type settableBuffer struct {
bytes.Buffer
didSetData bool
data []byte
}

func (sb *settableBuffer) SetBytes(data []byte) error {
sb.didSetData = true
sb.data = data
return nil
}

func (sb *settableBuffer) Bytes() []byte {
if sb.didSetData {
return sb.data
}
return sb.Buffer.Bytes()
}

0 comments on commit 55ed84a

Please sign in to comment.