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
69c28bb5
Unverified
Commit
69c28bb5
authored
Dec 06, 2017
by
Jakub Sztandera
Committed by
GitHub
Dec 06, 2017
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #17 from libp2p/feat/optimize-allocations
reduce the number of slice reallocations
parents
f37743c9
262aab6b
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
15 additions
and
6 deletions
+15
-6
sorting.go
sorting.go
+7
-2
table.go
table.go
+8
-4
No files found.
sorting.go
View file @
69c28bb5
...
...
@@ -25,6 +25,11 @@ func (p peerSorterArr) Less(a, b int) bool {
//
func
copyPeersFromList
(
target
ID
,
peerArr
peerSorterArr
,
peerList
*
list
.
List
)
peerSorterArr
{
if
cap
(
peerArr
)
<
len
(
peerArr
)
+
peerList
.
Len
()
{
newArr
:=
make
(
peerSorterArr
,
0
,
len
(
peerArr
)
+
peerList
.
Len
())
copy
(
newArr
,
peerArr
)
peerArr
=
newArr
}
for
e
:=
peerList
.
Front
();
e
!=
nil
;
e
=
e
.
Next
()
{
p
:=
e
.
Value
.
(
peer
.
ID
)
pID
:=
ConvertPeerID
(
p
)
...
...
@@ -38,7 +43,7 @@ func copyPeersFromList(target ID, peerArr peerSorterArr, peerList *list.List) pe
}
func
SortClosestPeers
(
peers
[]
peer
.
ID
,
target
ID
)
[]
peer
.
ID
{
var
psarr
peerSorterArr
psarr
:=
make
(
peerSorterArr
,
0
,
len
(
peers
))
for
_
,
p
:=
range
peers
{
pID
:=
ConvertPeerID
(
p
)
pd
:=
&
peerDistance
{
...
...
@@ -48,7 +53,7 @@ func SortClosestPeers(peers []peer.ID, target ID) []peer.ID {
psarr
=
append
(
psarr
,
pd
)
}
sort
.
Sort
(
psarr
)
var
out
[]
peer
.
ID
out
:=
make
([]
peer
.
ID
,
0
,
len
(
psarr
))
for
_
,
p
:=
range
psarr
{
out
=
append
(
out
,
p
.
p
)
}
...
...
table.go
View file @
69c28bb5
...
...
@@ -165,7 +165,7 @@ func (rt *RoutingTable) NearestPeers(id ID, count int) []peer.ID {
}
bucket
=
rt
.
Buckets
[
cpl
]
var
peerArr
peerSorterArr
peerArr
:=
make
(
peerSorterArr
,
0
,
count
)
peerArr
=
copyPeersFromList
(
id
,
peerArr
,
bucket
.
list
)
if
len
(
peerArr
)
<
count
{
// In the case of an unusual split, one bucket may be short or empty.
...
...
@@ -185,9 +185,13 @@ func (rt *RoutingTable) NearestPeers(id ID, count int) []peer.ID {
// Sort by distance to local peer
sort
.
Sort
(
peerArr
)
var
out
[]
peer
.
ID
for
i
:=
0
;
i
<
count
&&
i
<
peerArr
.
Len
();
i
++
{
out
=
append
(
out
,
peerArr
[
i
]
.
p
)
if
count
<
len
(
peerArr
)
{
peerArr
=
peerArr
[
:
count
]
}
out
:=
make
([]
peer
.
ID
,
0
,
len
(
peerArr
))
for
_
,
p
:=
range
peerArr
{
out
=
append
(
out
,
p
.
p
)
}
return
out
...
...
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