This repository has been archived by the owner on Feb 1, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 112
aggressively free memory #143
Merged
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,17 @@ | ||
package message | ||
|
||
import ( | ||
"encoding/binary" | ||
"fmt" | ||
"io" | ||
|
||
pb "github.com/ipfs/go-bitswap/message/pb" | ||
wantlist "github.com/ipfs/go-bitswap/wantlist" | ||
blocks "github.com/ipfs/go-block-format" | ||
|
||
ggio "github.com/gogo/protobuf/io" | ||
cid "github.com/ipfs/go-cid" | ||
pool "github.com/libp2p/go-buffer-pool" | ||
msgio "github.com/libp2p/go-msgio" | ||
|
||
"github.com/libp2p/go-libp2p-core/network" | ||
) | ||
|
@@ -170,18 +172,22 @@ func (m *impl) AddBlock(b blocks.Block) { | |
|
||
// FromNet generates a new BitswapMessage from incoming data on an io.Reader. | ||
func FromNet(r io.Reader) (BitSwapMessage, error) { | ||
pbr := ggio.NewDelimitedReader(r, network.MessageSizeMax) | ||
return FromPBReader(pbr) | ||
reader := msgio.NewVarintReaderSize(r, network.MessageSizeMax) | ||
return FromMsgReader(reader) | ||
} | ||
|
||
// FromPBReader generates a new Bitswap message from a gogo-protobuf reader | ||
func FromPBReader(pbr ggio.Reader) (BitSwapMessage, error) { | ||
pb := new(pb.Message) | ||
if err := pbr.ReadMsg(pb); err != nil { | ||
func FromMsgReader(r msgio.Reader) (BitSwapMessage, error) { | ||
msg, err := r.ReadMsg() | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return newMessageFromProto(*pb) | ||
var pb pb.Message | ||
if err := pb.Unmarshal(msg); err != nil { | ||
return nil, err | ||
} | ||
r.ReleaseMsg(msg) | ||
return newMessageFromProto(pb) | ||
} | ||
|
||
func (m *impl) ToProtoV0() *pb.Message { | ||
|
@@ -228,15 +234,25 @@ func (m *impl) ToProtoV1() *pb.Message { | |
} | ||
|
||
func (m *impl) ToNetV0(w io.Writer) error { | ||
pbw := ggio.NewDelimitedWriter(w) | ||
|
||
return pbw.WriteMsg(m.ToProtoV0()) | ||
return write(w, m.ToProtoV0()) | ||
} | ||
|
||
func (m *impl) ToNetV1(w io.Writer) error { | ||
pbw := ggio.NewDelimitedWriter(w) | ||
return write(w, m.ToProtoV1()) | ||
} | ||
|
||
return pbw.WriteMsg(m.ToProtoV1()) | ||
func write(w io.Writer, m *pb.Message) error { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I wonder if we should put this in a library somewhere? I'm going to need it in the DHT. |
||
size := m.Size() | ||
buf := pool.Get(size + binary.MaxVarintLen64) | ||
defer pool.Put(buf) | ||
n := binary.PutUvarint(buf, uint64(size)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. what did whitespace ever do to you? |
||
if written, err := m.MarshalTo(buf[n:]); err != nil { | ||
return err | ||
} else { | ||
n += written | ||
} | ||
_, err := w.Write(buf[:n]) | ||
return err | ||
} | ||
|
||
func (m *impl) Loggable() map[string]interface{} { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is your enter key not working?