Skip to content

Commit 266eeab

Browse files
ucwongshekhirin
authored andcommitted
p2p: initialize maps with known size (ethereum#27229)
1 parent 500aea8 commit 266eeab

File tree

10 files changed

+13
-13
lines changed

10 files changed

+13
-13
lines changed

p2p/dial_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -584,7 +584,7 @@ func (d *dialTestDialer) Dial(ctx context.Context, n *enode.Node) (net.Conn, err
584584
// waitForDials waits for calls to Dial with the given nodes as argument.
585585
// Those calls will be held blocking until completeDials is called with the same nodes.
586586
func (d *dialTestDialer) waitForDials(nodes []*enode.Node) error {
587-
waitset := make(map[enode.ID]*enode.Node)
587+
waitset := make(map[enode.ID]*enode.Node, len(nodes))
588588
for _, n := range nodes {
589589
waitset[n.ID()] = n
590590
}

p2p/discover/table_util_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ func (t *pingRecorder) RequestENR(n *enode.Node) (*enode.Node, error) {
175175
}
176176

177177
func hasDuplicates(slice []*node) bool {
178-
seen := make(map[enode.ID]bool)
178+
seen := make(map[enode.ID]bool, len(slice))
179179
for i, e := range slice {
180180
if e == nil {
181181
panic(fmt.Sprintf("nil *Node at %d", i))

p2p/discover/v5_udp_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ func TestUDPv5_findnodeHandling(t *testing.T) {
198198
}
199199

200200
func (test *udpV5Test) expectNodes(wantReqID []byte, wantTotal uint8, wantNodes []*enode.Node) {
201-
nodeSet := make(map[enode.ID]*enr.Record)
201+
nodeSet := make(map[enode.ID]*enr.Record, len(wantNodes))
202202
for _, n := range wantNodes {
203203
nodeSet[n.ID()] = n.Record()
204204
}

p2p/dnsdisc/client_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -439,7 +439,7 @@ func testNodes(keys []*ecdsa.PrivateKey) []*enode.Node {
439439
type mapResolver map[string]string
440440

441441
func newMapResolver(maps ...map[string]string) mapResolver {
442-
mr := make(mapResolver)
442+
mr := make(mapResolver, len(maps))
443443
for _, m := range maps {
444444
mr.add(m)
445445
}

p2p/enode/iter_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ func checkNodes(t *testing.T, nodes []*Node, wantLen int) {
7575
t.Errorf("slice has %d nodes, want %d", len(nodes), wantLen)
7676
return
7777
}
78-
seen := make(map[ID]bool)
78+
seen := make(map[ID]bool, len(nodes))
7979
for i, e := range nodes {
8080
if e == nil {
8181
t.Errorf("nil node at index %d", i)
@@ -231,7 +231,7 @@ func testMixerClose(t *testing.T) {
231231
}
232232

233233
func idPrefixDistribution(nodes []*Node) map[uint32]int {
234-
d := make(map[uint32]int)
234+
d := make(map[uint32]int, len(nodes))
235235
for _, node := range nodes {
236236
id := node.ID()
237237
d[binary.BigEndian.Uint32(id[:4])]++

p2p/enode/nodedb_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -273,11 +273,11 @@ func testSeedQuery() error {
273273

274274
// Retrieve the entire batch and check for duplicates
275275
seeds := db.QuerySeeds(len(nodeDBSeedQueryNodes)*2, time.Hour)
276-
have := make(map[ID]struct{})
276+
have := make(map[ID]struct{}, len(seeds))
277277
for _, seed := range seeds {
278278
have[seed.ID()] = struct{}{}
279279
}
280-
want := make(map[ID]struct{})
280+
want := make(map[ID]struct{}, len(nodeDBSeedQueryNodes[1:]))
281281
for _, seed := range nodeDBSeedQueryNodes[1:] {
282282
want[seed.node.ID()] = struct{}{}
283283
}

p2p/msgrate/msgrate.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -329,7 +329,7 @@ func (t *Trackers) MeanCapacities() map[uint64]float64 {
329329
// meanCapacities is the internal lockless version of MeanCapacities used for
330330
// debug logging.
331331
func (t *Trackers) meanCapacities() map[uint64]float64 {
332-
capacities := make(map[uint64]float64)
332+
capacities := make(map[uint64]float64, len(t.trackers))
333333
for _, tt := range t.trackers {
334334
tt.lock.RLock()
335335
for key, val := range tt.capacity {

p2p/netutil/iptrack.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ func (it *IPTracker) PredictEndpoint() string {
7878
it.gcStatements(it.clock.Now())
7979

8080
// The current strategy is simple: find the endpoint with most statements.
81-
counts := make(map[string]int)
81+
counts := make(map[string]int, len(it.statements))
8282
maxcount, max := 0, ""
8383
for _, s := range it.statements {
8484
c := counts[s.endpoint] + 1

p2p/nodestate/nodestate.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -333,7 +333,7 @@ func NewNodeStateMachine(db ethdb.KeyValueStore, dbKey []byte, clock mclock.Cloc
333333
fields: make([]*fieldInfo, len(setup.fields)),
334334
}
335335
ns.opWait = sync.NewCond(&ns.lock)
336-
stateNameMap := make(map[string]int)
336+
stateNameMap := make(map[string]int, len(setup.flags))
337337
for index, flag := range setup.flags {
338338
if _, ok := stateNameMap[flag.name]; ok {
339339
panic("Node state flag name collision: " + flag.name)
@@ -343,7 +343,7 @@ func NewNodeStateMachine(db ethdb.KeyValueStore, dbKey []byte, clock mclock.Cloc
343343
ns.saveFlags |= bitMask(1) << uint(index)
344344
}
345345
}
346-
fieldNameMap := make(map[string]int)
346+
fieldNameMap := make(map[string]int, len(setup.fields))
347347
for index, field := range setup.fields {
348348
if _, ok := fieldNameMap[field.name]; ok {
349349
panic("Node field name collision: " + field.name)

p2p/peer.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -510,7 +510,7 @@ func (p *Peer) Info() *PeerInfo {
510510
ID: p.ID().String(),
511511
Name: p.Fullname(),
512512
Caps: caps,
513-
Protocols: make(map[string]interface{}),
513+
Protocols: make(map[string]interface{}, len(p.running)),
514514
}
515515
if p.Node().Seq() > 0 {
516516
info.ENR = p.Node().String()

0 commit comments

Comments
 (0)