Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
What's new
10
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Open sidebar
p2p
go-p2p-kbucket
Commits
ebfcbe18
Commit
ebfcbe18
authored
Apr 09, 2019
by
Matt Joiner
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Remove a lot of allocations, and remove some ambiguous naming
parent
697cb691
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
52 additions
and
45 deletions
+52
-45
sorting.go
sorting.go
+38
-31
table.go
table.go
+14
-14
No files found.
sorting.go
View file @
ebfcbe18
...
...
@@ -13,48 +13,55 @@ type peerDistance struct {
distance
ID
}
// peerSorterArr implements sort.Interface to sort peers by xor distance
type
peerSorterArr
[]
*
peerDistance
// peerDistanceSorter implements sort.Interface to sort peers by xor distance
type
peerDistanceSorter
struct
{
peers
[]
peerDistance
target
ID
}
func
(
p
peerSorter
Arr
)
Len
()
int
{
return
len
(
p
)
}
func
(
p
peerSorter
Arr
)
Swap
(
a
,
b
int
)
{
p
[
a
],
p
[
b
]
=
p
[
b
],
p
[
a
]
}
func
(
p
peerSorter
Arr
)
Less
(
a
,
b
int
)
bool
{
return
p
[
a
]
.
distance
.
less
(
p
[
b
]
.
distance
)
func
(
p
ds
peer
Distance
Sorter
)
Len
()
int
{
return
len
(
p
ds
.
peers
)
}
func
(
p
ds
peer
Distance
Sorter
)
Swap
(
a
,
b
int
)
{
p
ds
.
peers
[
a
],
pds
.
peers
[
b
]
=
pds
.
peers
[
b
],
pds
.
peers
[
a
]
}
func
(
p
ds
peer
Distance
Sorter
)
Less
(
a
,
b
int
)
bool
{
return
p
ds
.
peers
[
a
]
.
distance
.
less
(
p
ds
.
peers
[
b
]
.
distance
)
}
//
// Append the peer.ID to the sorter's slice. It may no longer be sorted.
func
(
pds
peerDistanceSorter
)
appendPeer
(
p
peer
.
ID
)
peerDistanceSorter
{
pds
.
peers
=
append
(
pds
.
peers
,
peerDistance
{
p
:
p
,
distance
:
xor
(
pds
.
target
,
ConvertPeerID
(
p
)),
})
return
pds
}
func
copyPeersFromList
(
target
ID
,
peerArr
peerSorterArr
,
peerList
*
list
.
List
)
peerSorterArr
{
if
cap
(
peerArr
)
<
len
(
peerArr
)
+
peer
L
ist
.
Len
()
{
newArr
:=
make
(
peerSorterArr
,
len
(
peerArr
),
len
(
peerArr
)
+
peerList
.
Len
()
)
copy
(
newArr
,
peerArr
)
p
eerArr
=
newArr
// Append the peer.ID values in the list to the sorter's slice. It may no longer be sorted.
func
(
pds
peerDistanceSorter
)
appendPeersFromList
(
l
*
list
.
List
)
peer
D
ist
anceSorter
{
startLen
:=
pds
.
Len
()
for
e
:=
l
.
Front
();
e
!=
nil
;
e
=
e
.
Next
()
{
p
ds
=
pds
.
appendPeer
(
e
.
Value
.
(
peer
.
ID
))
}
for
e
:=
peerList
.
Front
();
e
!=
nil
;
e
=
e
.
Next
()
{
p
:=
e
.
Value
.
(
peer
.
ID
)
pID
:=
ConvertPeerID
(
p
)
pd
:=
peerDistance
{
p
:
p
,
distance
:
xor
(
target
,
pID
),
}
peerArr
=
append
(
peerArr
,
&
pd
)
if
pds
.
Len
()
!=
startLen
+
l
.
Len
()
{
panic
(
"len did not increase"
)
}
return
p
eerArr
return
p
ds
}
func
(
pds
peerDistanceSorter
)
sort
()
{
sort
.
Sort
(
pds
)
}
// Sort the given peers by their ascending distance from the target. A new slice is returned.
func
SortClosestPeers
(
peers
[]
peer
.
ID
,
target
ID
)
[]
peer
.
ID
{
psarr
:=
make
(
peerSorterArr
,
0
,
len
(
peers
))
sorter
:=
peerDistanceSorter
{
peers
:
make
([]
peerDistance
,
0
,
len
(
peers
)),
target
:
target
,
}
for
_
,
p
:=
range
peers
{
pID
:=
ConvertPeerID
(
p
)
pd
:=
&
peerDistance
{
p
:
p
,
distance
:
xor
(
target
,
pID
),
}
psarr
=
append
(
psarr
,
pd
)
sorter
=
sorter
.
appendPeer
(
p
)
}
sort
.
Sort
(
psarr
)
out
:=
make
([]
peer
.
ID
,
0
,
len
(
psarr
))
for
_
,
p
:=
range
psarr
{
sort
er
.
sort
(
)
out
:=
make
([]
peer
.
ID
,
0
,
sorter
.
Len
(
))
for
_
,
p
:=
range
sorter
.
peers
{
out
=
append
(
out
,
p
.
p
)
}
return
out
...
...
table.go
View file @
ebfcbe18
...
...
@@ -4,7 +4,6 @@ package kbucket
import
(
"errors"
"fmt"
"sort"
"sync"
"time"
...
...
@@ -169,6 +168,7 @@ func (rt *RoutingTable) NearestPeer(id ID) peer.ID {
func
(
rt
*
RoutingTable
)
NearestPeers
(
id
ID
,
count
int
)
[]
peer
.
ID
{
cpl
:=
CommonPrefixLen
(
id
,
rt
.
local
)
// It's assumed that this also protects the buckets.
rt
.
tabLock
.
RLock
()
// Get bucket at cpl index or last bucket
...
...
@@ -178,32 +178,32 @@ func (rt *RoutingTable) NearestPeers(id ID, count int) []peer.ID {
}
bucket
=
rt
.
Buckets
[
cpl
]
peerArr
:=
make
(
peerSorterArr
,
0
,
count
)
peerArr
=
copyPeersFromList
(
id
,
peerArr
,
bucket
.
list
)
if
len
(
peerArr
)
<
count
{
pds
:=
peerDistanceSorter
{
peers
:
make
([]
peerDistance
,
0
,
3
*
rt
.
bucketsize
),
target
:
id
,
}
pds
=
pds
.
appendPeersFromList
(
bucket
.
list
)
if
pds
.
Len
()
<
count
{
// In the case of an unusual split, one bucket may be short or empty.
// if this happens, search both surrounding buckets for nearby peers
if
cpl
>
0
{
plist
:=
rt
.
Buckets
[
cpl
-
1
]
.
list
peerArr
=
copyPeersFromList
(
id
,
peerArr
,
plist
)
pds
=
pds
.
appendPeersFromList
(
rt
.
Buckets
[
cpl
-
1
]
.
list
)
}
if
cpl
<
len
(
rt
.
Buckets
)
-
1
{
plist
:=
rt
.
Buckets
[
cpl
+
1
]
.
list
peerArr
=
copyPeersFromList
(
id
,
peerArr
,
plist
)
pds
=
pds
.
appendPeersFromList
(
rt
.
Buckets
[
cpl
+
1
]
.
list
)
}
}
rt
.
tabLock
.
RUnlock
()
// Sort by distance to local peer
sort
.
Sort
(
peerArr
)
pds
.
sort
(
)
if
count
<
len
(
peerArr
)
{
p
eerArr
=
peerArr
[
:
count
]
if
count
<
pds
.
Len
(
)
{
p
ds
.
peers
=
pds
.
peers
[
:
count
]
}
out
:=
make
([]
peer
.
ID
,
0
,
len
(
peerArr
))
for
_
,
p
:=
range
p
eerArr
{
out
:=
make
([]
peer
.
ID
,
0
,
pds
.
Len
(
))
for
_
,
p
:=
range
p
ds
.
peers
{
out
=
append
(
out
,
p
.
p
)
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment