bitswap: better allocation patters in message
License: MIT Signed-off-by: Steven Allen <steven@stebalien.com>
Steven Allen committed
Dec 5, 2017 at 09:56 UTC
a589470329ccaa5b59fa70b98deb0cc338bf4b51
1 file changed
+12
-4
exchange/bitswap/message/message.go
+12
-4
@@ -120,7 +120,7 @@ func (m *impl) Empty() bool {
120
}
121
122
func (m *impl) Wantlist() []Entry {
123
- var out []Entry
123
+ out := make([]Entry, 0, len(m.wantlist))
124
for _, e := range m.wantlist {
125
out = append(out, e)
126
}
@@ -182,6 +182,7 @@ func FromPBReader(pbr ggio.Reader) (BitSwapMessage, error) {
182
func (m *impl) ToProtoV0() *pb.Message {
183
pbm := new(pb.Message)
184
pbm.Wantlist = new(pb.Message_Wantlist)
185
+ pbm.Wantlist.Entries = make([]*pb.Message_Wantlist_Entry, 0, len(m.wantlist))
186
for _, e := range m.wantlist {
187
pbm.Wantlist.Entries = append(pbm.Wantlist.Entries, &pb.Message_Wantlist_Entry{
188
Block: proto.String(e.Cid.KeyString()),
@@ -190,7 +191,10 @@ func (m *impl) ToProtoV0() *pb.Message {
191
})
192
}
193
pbm.Wantlist.Full = proto.Bool(m.full)
193
- for _, b := range m.Blocks() {
194
+
195
+ blocks := m.Blocks()
196
+ pbm.Blocks = make([][]byte, 0, len(blocks))
197
+ for _, b := range blocks {
198
pbm.Blocks = append(pbm.Blocks, b.RawData())
199
}
200
return pbm
@@ -199,6 +203,7 @@ func (m *impl) ToProtoV0() *pb.Message {
203
func (m *impl) ToProtoV1() *pb.Message {
204
pbm := new(pb.Message)
205
pbm.Wantlist = new(pb.Message_Wantlist)
206
+ pbm.Wantlist.Entries = make([]*pb.Message_Wantlist_Entry, 0, len(m.wantlist))
207
for _, e := range m.wantlist {
208
pbm.Wantlist.Entries = append(pbm.Wantlist.Entries, &pb.Message_Wantlist_Entry{
209
Block: proto.String(e.Cid.KeyString()),
@@ -207,7 +212,10 @@ func (m *impl) ToProtoV1() *pb.Message {
212
})
213
}
214
pbm.Wantlist.Full = proto.Bool(m.full)
210
- for _, b := range m.Blocks() {
215
+
216
+ blocks := m.Blocks()
217
+ pbm.Payload = make([]*pb.Message_Block, 0, len(blocks))
218
+ for _, b := range blocks {
219
blk := &pb.Message_Block{
220
Data: b.RawData(),
221
Prefix: b.Cid().Prefix().Bytes(),
@@ -230,7 +238,7 @@ func (m *impl) ToNetV1(w io.Writer) error {
238
}
239
240
func (m *impl) Loggable() map[string]interface{} {
233
- var blocks []string
241
+ blocks := make([]string, 0, len(m.blocks))
242
for _, v := range m.blocks {
243
blocks = append(blocks, v.Cid().String())
244
}