p2p, swarm, trie: avoid copying slices in loops (#17265)

This commit is contained in:
Oleg Kovalov 2018-08-07 12:56:40 +02:00 committed by Péter Szilágyi
parent de9b0660ac
commit cf05ef9106
6 changed files with 15 additions and 15 deletions

View File

@ -160,7 +160,7 @@ func (tab *Table) ReadRandomNodes(buf []*Node) (n int) {
// Find all non-empty buckets and get a fresh slice of their entries. // Find all non-empty buckets and get a fresh slice of their entries.
var buckets [][]*Node var buckets [][]*Node
for _, b := range tab.buckets { for _, b := range &tab.buckets {
if len(b.entries) > 0 { if len(b.entries) > 0 {
buckets = append(buckets, b.entries[:]) buckets = append(buckets, b.entries[:])
} }
@ -508,7 +508,7 @@ func (tab *Table) copyLiveNodes() {
defer tab.mutex.Unlock() defer tab.mutex.Unlock()
now := time.Now() now := time.Now()
for _, b := range tab.buckets { for _, b := range &tab.buckets {
for _, n := range b.entries { for _, n := range b.entries {
if now.Sub(n.addedAt) >= seedMinTableTime { if now.Sub(n.addedAt) >= seedMinTableTime {
tab.db.updateNode(n) tab.db.updateNode(n)
@ -524,7 +524,7 @@ func (tab *Table) closest(target common.Hash, nresults int) *nodesByDistance {
// obviously correct. I believe that tree-based buckets would make // obviously correct. I believe that tree-based buckets would make
// this easier to implement efficiently. // this easier to implement efficiently.
close := &nodesByDistance{target: target} close := &nodesByDistance{target: target}
for _, b := range tab.buckets { for _, b := range &tab.buckets {
for _, n := range b.entries { for _, n := range b.entries {
close.push(n, nresults) close.push(n, nresults)
} }
@ -533,7 +533,7 @@ func (tab *Table) closest(target common.Hash, nresults int) *nodesByDistance {
} }
func (tab *Table) len() (n int) { func (tab *Table) len() (n int) {
for _, b := range tab.buckets { for _, b := range &tab.buckets {
n += len(b.entries) n += len(b.entries)
} }
return n return n

View File

@ -355,7 +355,7 @@ func (tn *preminedTestnet) mine(target NodeID) {
fmt.Printf(" target: %#v,\n", tn.target) fmt.Printf(" target: %#v,\n", tn.target)
fmt.Printf(" targetSha: %#v,\n", tn.targetSha) fmt.Printf(" targetSha: %#v,\n", tn.targetSha)
fmt.Printf(" dists: [%d][]NodeID{\n", len(tn.dists)) fmt.Printf(" dists: [%d][]NodeID{\n", len(tn.dists))
for ld, ns := range tn.dists { for ld, ns := range &tn.dists {
if len(ns) == 0 { if len(ns) == 0 {
continue continue
} }

View File

@ -81,7 +81,7 @@ func (tab *Table) chooseBucketRefreshTarget() common.Hash {
if printTable { if printTable {
fmt.Println() fmt.Println()
} }
for i, b := range tab.buckets { for i, b := range &tab.buckets {
entries += len(b.entries) entries += len(b.entries)
if printTable { if printTable {
for _, e := range b.entries { for _, e := range b.entries {
@ -93,7 +93,7 @@ func (tab *Table) chooseBucketRefreshTarget() common.Hash {
prefix := binary.BigEndian.Uint64(tab.self.sha[0:8]) prefix := binary.BigEndian.Uint64(tab.self.sha[0:8])
dist := ^uint64(0) dist := ^uint64(0)
entry := int(randUint(uint32(entries + 1))) entry := int(randUint(uint32(entries + 1)))
for _, b := range tab.buckets { for _, b := range &tab.buckets {
if entry < len(b.entries) { if entry < len(b.entries) {
n := b.entries[entry] n := b.entries[entry]
dist = binary.BigEndian.Uint64(n.sha[0:8]) ^ prefix dist = binary.BigEndian.Uint64(n.sha[0:8]) ^ prefix
@ -121,7 +121,7 @@ func (tab *Table) readRandomNodes(buf []*Node) (n int) {
// TODO: tree-based buckets would help here // TODO: tree-based buckets would help here
// Find all non-empty buckets and get a fresh slice of their entries. // Find all non-empty buckets and get a fresh slice of their entries.
var buckets [][]*Node var buckets [][]*Node
for _, b := range tab.buckets { for _, b := range &tab.buckets {
if len(b.entries) > 0 { if len(b.entries) > 0 {
buckets = append(buckets, b.entries[:]) buckets = append(buckets, b.entries[:])
} }
@ -175,7 +175,7 @@ func (tab *Table) closest(target common.Hash, nresults int) *nodesByDistance {
// obviously correct. I believe that tree-based buckets would make // obviously correct. I believe that tree-based buckets would make
// this easier to implement efficiently. // this easier to implement efficiently.
close := &nodesByDistance{target: target} close := &nodesByDistance{target: target}
for _, b := range tab.buckets { for _, b := range &tab.buckets {
for _, n := range b.entries { for _, n := range b.entries {
close.push(n, nresults) close.push(n, nresults)
} }

View File

@ -159,7 +159,7 @@ func (m *ManifestWalker) Walk(walkFn WalkFn) error {
} }
func (m *ManifestWalker) walk(trie *manifestTrie, prefix string, walkFn WalkFn) error { func (m *ManifestWalker) walk(trie *manifestTrie, prefix string, walkFn WalkFn) error {
for _, entry := range trie.entries { for _, entry := range &trie.entries {
if entry == nil { if entry == nil {
continue continue
} }
@ -308,7 +308,7 @@ func (mt *manifestTrie) addEntry(entry *manifestTrieEntry, quitC chan bool) {
} }
func (mt *manifestTrie) getCountLast() (cnt int, entry *manifestTrieEntry) { func (mt *manifestTrie) getCountLast() (cnt int, entry *manifestTrieEntry) {
for _, e := range mt.entries { for _, e := range &mt.entries {
if e != nil { if e != nil {
cnt++ cnt++
entry = e entry = e
@ -362,7 +362,7 @@ func (mt *manifestTrie) recalcAndStore() error {
buffer.WriteString(`{"entries":[`) buffer.WriteString(`{"entries":[`)
list := &Manifest{} list := &Manifest{}
for _, entry := range mt.entries { for _, entry := range &mt.entries {
if entry != nil { if entry != nil {
if entry.Hash == "" { // TODO: paralellize if entry.Hash == "" { // TODO: paralellize
err := entry.subtrie.recalcAndStore() err := entry.subtrie.recalcAndStore()

View File

@ -55,7 +55,7 @@ var nilValueNode = valueNode(nil)
func (n *fullNode) EncodeRLP(w io.Writer) error { func (n *fullNode) EncodeRLP(w io.Writer) error {
var nodes [17]node var nodes [17]node
for i, child := range n.Children { for i, child := range &n.Children {
if child != nil { if child != nil {
nodes[i] = child nodes[i] = child
} else { } else {
@ -98,7 +98,7 @@ func (n valueNode) String() string { return n.fstring("") }
func (n *fullNode) fstring(ind string) string { func (n *fullNode) fstring(ind string) string {
resp := fmt.Sprintf("[\n%s ", ind) resp := fmt.Sprintf("[\n%s ", ind)
for i, node := range n.Children { for i, node := range &n.Children {
if node == nil { if node == nil {
resp += fmt.Sprintf("%s: <nil> ", indices[i]) resp += fmt.Sprintf("%s: <nil> ", indices[i])
} else { } else {

View File

@ -356,7 +356,7 @@ func (t *Trie) delete(n node, prefix, key []byte) (bool, node, error) {
// value that is left in n or -2 if n contains at least two // value that is left in n or -2 if n contains at least two
// values. // values.
pos := -1 pos := -1
for i, cld := range n.Children { for i, cld := range &n.Children {
if cld != nil { if cld != nil {
if pos == -1 { if pos == -1 {
pos = i pos = i