@@ -147,35 +147,18 @@ func (tab *Table) ReadRandomNodes(buf []*enode.Node) (n int) {
147
147
tab .mutex .Lock ()
148
148
defer tab .mutex .Unlock ()
149
149
150
- // Find all non-empty buckets and get a fresh slice of their entries.
151
- var buckets [][]* node
150
+ var nodes []* enode.Node
152
151
for _ , b := range & tab .buckets {
153
- if len ( b .entries ) > 0 {
154
- buckets = append (buckets , b . entries )
152
+ for _ , n := range b .entries {
153
+ nodes = append (nodes , unwrapNode ( n ) )
155
154
}
156
155
}
157
- if len (buckets ) == 0 {
158
- return 0
159
- }
160
- // Shuffle the buckets.
161
- for i := len (buckets ) - 1 ; i > 0 ; i -- {
162
- j := tab .rand .Intn (len (buckets ))
163
- buckets [i ], buckets [j ] = buckets [j ], buckets [i ]
164
- }
165
- // Move head of each bucket into buf, removing buckets that become empty.
166
- var i , j int
167
- for ; i < len (buf ); i , j = i + 1 , (j + 1 )% len (buckets ) {
168
- b := buckets [j ]
169
- buf [i ] = unwrapNode (b [0 ])
170
- buckets [j ] = b [1 :]
171
- if len (b ) == 1 {
172
- buckets = append (buckets [:j ], buckets [j + 1 :]... )
173
- }
174
- if len (buckets ) == 0 {
175
- break
176
- }
156
+ // Shuffle.
157
+ for i := 0 ; i < len (nodes ); i ++ {
158
+ j := tab .rand .Intn (len (nodes ))
159
+ nodes [i ], nodes [j ] = nodes [j ], nodes [i ]
177
160
}
178
- return i + 1
161
+ return copy ( buf , nodes )
179
162
}
180
163
181
164
// getNode returns the node with the given ID or nil if it isn't in the table.
0 commit comments