Skip to content

Commit

Permalink
feat: re-generate protobufs with gogofaster
Browse files Browse the repository at this point in the history
This removes the pointer indirection from all values and saves us an
allocation. We never actually try to distinguish between a field being
missing and the "zero" value anyways.
  • Loading branch information
Stebalien committed May 11, 2021
1 parent 0094708 commit 24638f6
Show file tree
Hide file tree
Showing 17 changed files with 603 additions and 1,245 deletions.
4 changes: 2 additions & 2 deletions comm.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ func (p *PubSub) getHelloPacket() *RPC {

for t := range subscriptions {
as := &pb.RPC_SubOpts{
Topicid: proto.String(t),
Subscribe: proto.Bool(true),
Topicid: t,
Subscribe: true,
}
rpc.Subscriptions = append(rpc.Subscriptions, as)
}
Expand Down
2 changes: 1 addition & 1 deletion compat_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ func TestMultitopicMessageCompatibility(t *testing.T) {
From: []byte("A"),
Data: []byte("blah"),
Seqno: []byte("123"),
Topic: &topic1,
Topic: topic1,
Signature: []byte("a-signature"),
Key: []byte("a-key"),
}
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ github.com/btcsuite/winsvc v1.0.0/go.mod h1:jsenWakMcC0zFBFurPLEAyrnc/teJEM1O46f
github.com/buger/jsonparser v0.0.0-20181115193947-bf1c66bbce23/go.mod h1:bbYlZJ7hK1yFx9hf58LP0zeX7UjIGs20ufpu3evjr+s=
github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU=
github.com/cespare/xxhash v1.1.0/go.mod h1:XrSqR1VqqWfGrhpAt58auRo0WTKS1nRRg3ghfAqPWnc=
github.com/cheekybits/genny v1.0.0 h1:uGGa4nei+j20rOSeDeP5Of12XVm7TGUd4dJA9RDitfE=
github.com/cheekybits/genny v1.0.0/go.mod h1:+tQajlRqAUrPI7DOSpB0XAqZYtQakVtB7wXkRAgjxjQ=
github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDkc90ppPyw=
github.com/cncf/udpa/go v0.0.0-20191209042840-269d4d468f6f/go.mod h1:M8M6+tZqaGXZJjfX53e64911xZQV5JYwmTeXPW+k8Sc=
Expand Down Expand Up @@ -600,6 +601,7 @@ gopkg.in/fsnotify.v1 v1.4.7/go.mod h1:Tz8NjZHkW78fSQdbUxIjBTcgA1z1m8ZHf0WmKUhAMy
gopkg.in/inf.v0 v0.9.1/go.mod h1:cWUDdTG/fYaXco+Dcufb5Vnc6Gp2YChqWtbxRZE0mXw=
gopkg.in/src-d/go-cli.v0 v0.0.0-20181105080154-d492247bbc0d/go.mod h1:z+K8VcOYVYcSwSjGebuDL6176A1XskgbtNl64NSg+n8=
gopkg.in/src-d/go-log.v1 v1.0.1/go.mod h1:GN34hKP0g305ysm2/hctJ0Y8nWP3zxXXJ8GFabTyABE=
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7 h1:uRGJdciOHaEIrze2W8Q3AKkepLTh2hOroT7a+7czfdQ=
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7/go.mod h1:dt/ZhP58zS4L8KSrWDmTeBkI65Dw0HsyUHuEVlX15mw=
gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
Expand Down
19 changes: 7 additions & 12 deletions gossipsub.go
Original file line number Diff line number Diff line change
Expand Up @@ -210,8 +210,8 @@ func NewGossipSub(ctx context.Context, h host.Host, opts ...Option) (*PubSub, er
outbound: make(map[peer.ID]bool),
connect: make(chan connectInfo, params.MaxPendingConnections),
mcache: NewMessageCache(params.HistoryGossip, params.HistoryLength),
protos: GossipSubDefaultProtocols,
feature: GossipSubDefaultFeatures,
protos: GossipSubDefaultProtocols,
feature: GossipSubDefaultFeatures,
tagTracer: newTagTracer(h.ConnManager()),
params: params,
}
Expand Down Expand Up @@ -1078,7 +1078,7 @@ func (gs *GossipSubRouter) Leave(topic string) {
}

func (gs *GossipSubRouter) sendGraft(p peer.ID, topic string) {
graft := []*pb.ControlGraft{&pb.ControlGraft{TopicID: &topic}}
graft := []*pb.ControlGraft{&pb.ControlGraft{TopicID: topic}}
out := rpcWithControl(nil, nil, nil, graft, nil)
gs.sendRPC(p, out)
}
Expand Down Expand Up @@ -1619,12 +1619,7 @@ func (gs *GossipSubRouter) sendGraftPrune(tograft, toprune map[peer.ID][]string,
for p, topics := range tograft {
graft := make([]*pb.ControlGraft, 0, len(topics))
for _, topic := range topics {
// copy topic string here since
// the reference to the string
// topic here changes with every
// iteration of the slice.
copiedID := topic
graft = append(graft, &pb.ControlGraft{TopicID: &copiedID})
graft = append(graft, &pb.ControlGraft{TopicID: topic})
}

var prune []*pb.ControlPrune
Expand Down Expand Up @@ -1707,7 +1702,7 @@ func (gs *GossipSubRouter) emitGossip(topic string, exclude map[peer.ID]struct{}
shuffleStrings(mids)
copy(peerMids, mids)
}
gs.enqueueGossip(p, &pb.ControlIHave{TopicID: &topic, MessageIDs: peerMids})
gs.enqueueGossip(p, &pb.ControlIHave{TopicID: topic, MessageIDs: peerMids})
}
}

Expand Down Expand Up @@ -1803,7 +1798,7 @@ func (gs *GossipSubRouter) piggybackControl(p peer.ID, out *RPC, ctl *pb.Control
func (gs *GossipSubRouter) makePrune(p peer.ID, topic string, doPX bool) *pb.ControlPrune {
if !gs.feature(GossipSubFeaturePX, gs.peers[p]) {
// GossipSub v1.0 -- no peer exchange, the peer won't be able to parse it anyway
return &pb.ControlPrune{TopicID: &topic}
return &pb.ControlPrune{TopicID: topic}
}

backoff := uint64(gs.params.PruneBackoff / time.Second)
Expand Down Expand Up @@ -1835,7 +1830,7 @@ func (gs *GossipSubRouter) makePrune(p peer.ID, topic string, doPX bool) *pb.Con
}
}

return &pb.ControlPrune{TopicID: &topic, Peers: px, Backoff: &backoff}
return &pb.ControlPrune{TopicID: topic, Peers: px, Backoff: backoff}
}

func (gs *GossipSubRouter) getPeers(topic string, count int, filter func(peer.ID) bool) []peer.ID {
Expand Down
4 changes: 2 additions & 2 deletions gossipsub_spam_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,7 @@ func TestGossipsubAttackGRAFTNonExistentTopic(t *testing.T) {
// Graft to the peer on a non-existent topic
nonExistentTopic := "non-existent"
writeMsg(&pb.RPC{
Control: &pb.ControlMessage{Graft: []*pb.ControlGraft{&pb.ControlGraft{TopicID: &nonExistentTopic}}},
Control: &pb.ControlMessage{Graft: []*pb.ControlGraft{&pb.ControlGraft{TopicID: nonExistentTopic}}},
})

go func() {
Expand Down Expand Up @@ -663,7 +663,7 @@ func TestGossipsubAttackInvalidMessageSpam(t *testing.T) {
for i := 0; i < 100; i++ {
msg := &pb.Message{
Data: []byte("some data" + strconv.Itoa(i)),
Topic: &mytopic,
Topic: mytopic,
From: []byte(attacker.ID()),
Seqno: []byte{byte(i + 1)},
}
Expand Down
16 changes: 8 additions & 8 deletions gossipsub_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1579,8 +1579,8 @@ func TestGossipsubPiggybackControl(t *testing.T) {

rpc := &RPC{RPC: pb.RPC{}}
gs.piggybackControl(blah, rpc, &pb.ControlMessage{
Graft: []*pb.ControlGraft{&pb.ControlGraft{TopicID: &test1}, &pb.ControlGraft{TopicID: &test2}, &pb.ControlGraft{TopicID: &test3}},
Prune: []*pb.ControlPrune{&pb.ControlPrune{TopicID: &test1}, &pb.ControlPrune{TopicID: &test2}, &pb.ControlPrune{TopicID: &test3}},
Graft: []*pb.ControlGraft{&pb.ControlGraft{TopicID: test1}, &pb.ControlGraft{TopicID: test2}, &pb.ControlGraft{TopicID: test3}},
Prune: []*pb.ControlPrune{&pb.ControlPrune{TopicID: test1}, &pb.ControlPrune{TopicID: test2}, &pb.ControlPrune{TopicID: test3}},
})
res <- rpc
}
Expand Down Expand Up @@ -1796,7 +1796,7 @@ func (sq *sybilSquatter) handleStream(s network.Stream) {
w := protoio.NewDelimitedWriter(os)
truth := true
topic := "test"
err = w.WriteMsg(&pb.RPC{Subscriptions: []*pb.RPC_SubOpts{&pb.RPC_SubOpts{Subscribe: &truth, Topicid: &topic}}})
err = w.WriteMsg(&pb.RPC{Subscriptions: []*pb.RPC_SubOpts{&pb.RPC_SubOpts{Subscribe: truth, Topicid: topic}}})
if err != nil {
panic(err)
}
Expand Down Expand Up @@ -2028,7 +2028,7 @@ func (iwe *iwantEverything) handleStream(s network.Stream) {
w := protoio.NewDelimitedWriter(os)
truth := true
topic := "test"
err = w.WriteMsg(&pb.RPC{Subscriptions: []*pb.RPC_SubOpts{&pb.RPC_SubOpts{Subscribe: &truth, Topicid: &topic}}})
err = w.WriteMsg(&pb.RPC{Subscriptions: []*pb.RPC_SubOpts{&pb.RPC_SubOpts{Subscribe: truth, Topicid: topic}}})

var rpc pb.RPC
for {
Expand Down Expand Up @@ -2127,8 +2127,8 @@ func TestFragmentRPCFunction(t *testing.T) {
truth := true
rpc.Subscriptions = []*pb.RPC_SubOpts{
{
Subscribe: &truth,
Topicid: &topic,
Subscribe: truth,
Topicid: topic,
},
}
rpc.Publish = make([]*pb.Message, nMessages)
Expand Down Expand Up @@ -2162,8 +2162,8 @@ func TestFragmentRPCFunction(t *testing.T) {
// the control messages should be in a separate RPC at the end
// reuse RPC from prev test, but add a control message
rpc.Control = &pb.ControlMessage{
Graft: []*pb.ControlGraft{{TopicID: &topic}},
Prune: []*pb.ControlPrune{{TopicID: &topic}},
Graft: []*pb.ControlGraft{{TopicID: topic}},
Prune: []*pb.ControlPrune{{TopicID: topic}},
Ihave: []*pb.ControlIHave{{MessageIDs: []string{"foo"}}},
Iwant: []*pb.ControlIWant{{MessageIDs: []string{"bar"}}},
}
Expand Down
2 changes: 1 addition & 1 deletion mcache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ func makeTestMessage(n int) *pb.Message {
topic := "test"
return &pb.Message{
Data: data,
Topic: &topic,
Topic: topic,
From: []byte("test"),
Seqno: seqno,
}
Expand Down
2 changes: 1 addition & 1 deletion pb/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ GO = $(PB:.proto=.pb.go)
all: $(GO)

%.pb.go: %.proto
protoc --proto_path=$(GOPATH)/src:. --gogofast_out=. $<
protoc --proto_path=$(GOPATH)/src:. --gogofaster_out=. $<

clean:
rm -f *.pb.go
Expand Down
Loading

0 comments on commit 24638f6

Please sign in to comment.