Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
B
bio-rd
Manage
Activity
Members
Labels
Plan
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Environments
Terraform modules
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
danet
bio-rd
Commits
25c18b08
Commit
25c18b08
authored
6 years ago
by
Julian Kornberger
Browse files
Options
Downloads
Patches
Plain Diff
Use strings.Builder instead of string concatenation
Reduces allocs
parent
e560f178
Branches
Branches containing commit
Tags
Tags containing commit
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
route/bgp_path.go
+55
-38
55 additions, 38 deletions
route/bgp_path.go
route/bgp_path_test.go
+9
-3
9 additions, 3 deletions
route/bgp_path_test.go
with
64 additions
and
41 deletions
route/bgp_path.go
+
55
−
38
View file @
25c18b08
...
...
@@ -236,6 +236,8 @@ func (b *BGPPath) better(c *BGPPath) bool {
// Print all known information about a route in logfile friendly format
func
(
b
*
BGPPath
)
String
()
string
{
buf
:=
&
strings
.
Builder
{}
origin
:=
""
switch
b
.
Origin
{
case
0
:
...
...
@@ -251,30 +253,32 @@ func (b *BGPPath) String() string {
bgpType
=
"external"
}
ret
:=
fmt
.
S
printf
(
"Local Pref: %d, "
,
b
.
LocalPref
)
ret
+=
fmt
.
S
printf
(
"Origin: %s, "
,
origin
)
ret
+=
fmt
.
S
printf
(
"AS Path: %v, "
,
b
.
ASPath
)
ret
+=
fmt
.
S
printf
(
"BGP type: %s, "
,
bgpType
)
ret
+=
fmt
.
S
printf
(
"NEXT HOP: %s, "
,
b
.
NextHop
)
ret
+=
fmt
.
S
printf
(
"MED: %d, "
,
b
.
MED
)
ret
+=
fmt
.
S
printf
(
"Path ID: %d, "
,
b
.
PathIdentifier
)
ret
+=
fmt
.
S
printf
(
"Source: %s, "
,
b
.
Source
)
ret
+=
fmt
.
S
printf
(
"Communities: %v, "
,
b
.
Communities
)
ret
+=
fmt
.
S
printf
(
"LargeCommunities: %v
,
"
,
b
.
LargeCommunities
)
fmt
.
F
printf
(
buf
,
"Local Pref: %d, "
,
b
.
LocalPref
)
fmt
.
F
printf
(
buf
,
"Origin: %s, "
,
origin
)
fmt
.
F
printf
(
buf
,
"AS Path: %v, "
,
b
.
ASPath
)
fmt
.
F
printf
(
buf
,
"BGP type: %s, "
,
bgpType
)
fmt
.
F
printf
(
buf
,
"NEXT HOP: %s, "
,
b
.
NextHop
)
fmt
.
F
printf
(
buf
,
"MED: %d, "
,
b
.
MED
)
fmt
.
F
printf
(
buf
,
"Path ID: %d, "
,
b
.
PathIdentifier
)
fmt
.
F
printf
(
buf
,
"Source: %s, "
,
b
.
Source
)
fmt
.
F
printf
(
buf
,
"Communities: %v, "
,
b
.
Communities
)
fmt
.
F
printf
(
buf
,
"LargeCommunities: %v"
,
b
.
LargeCommunities
)
if
b
.
OriginatorID
!=
0
{
oid
:=
convert
.
Uint32Byte
(
b
.
OriginatorID
)
ret
+=
fmt
.
S
printf
(
"
OriginatorID: %d.%d.%d.%d
,
"
,
oid
[
0
],
oid
[
1
],
oid
[
2
],
oid
[
3
])
fmt
.
F
printf
(
buf
,
",
OriginatorID: %d.%d.%d.%d"
,
oid
[
0
],
oid
[
1
],
oid
[
2
],
oid
[
3
])
}
if
b
.
ClusterList
!=
nil
{
ret
+=
fmt
.
S
printf
(
"
ClusterList %s"
,
b
.
ClusterListString
())
fmt
.
F
printf
(
buf
,
",
ClusterList %s"
,
b
.
ClusterListString
())
}
return
ret
return
buf
.
String
()
}
// Print all known information about a route in human readable form
func
(
b
*
BGPPath
)
Print
()
string
{
buf
:=
&
strings
.
Builder
{}
origin
:=
""
switch
b
.
Origin
{
case
0
:
...
...
@@ -290,26 +294,26 @@ func (b *BGPPath) Print() string {
bgpType
=
"external"
}
ret
:=
fmt
.
S
printf
(
"
\t\t
Local Pref: %d
\n
"
,
b
.
LocalPref
)
ret
+=
fmt
.
S
printf
(
"
\t\t
Origin: %s
\n
"
,
origin
)
ret
+=
fmt
.
S
printf
(
"
\t\t
AS Path: %v
\n
"
,
b
.
ASPath
)
ret
+=
fmt
.
S
printf
(
"
\t\t
BGP type: %s
\n
"
,
bgpType
)
ret
+=
fmt
.
S
printf
(
"
\t\t
NEXT HOP: %s
\n
"
,
b
.
NextHop
)
ret
+=
fmt
.
S
printf
(
"
\t\t
MED: %d
\n
"
,
b
.
MED
)
ret
+=
fmt
.
S
printf
(
"
\t\t
Path ID: %d
\n
"
,
b
.
PathIdentifier
)
ret
+=
fmt
.
S
printf
(
"
\t\t
Source: %s
\n
"
,
b
.
Source
)
ret
+=
fmt
.
S
printf
(
"
\t\t
Communities: %v
\n
"
,
b
.
Communities
)
ret
+=
fmt
.
S
printf
(
"
\t\t
LargeCommunities: %v
\n
"
,
b
.
LargeCommunities
)
fmt
.
F
printf
(
buf
,
"
\t\t
Local Pref: %d
\n
"
,
b
.
LocalPref
)
fmt
.
F
printf
(
buf
,
"
\t\t
Origin: %s
\n
"
,
origin
)
fmt
.
F
printf
(
buf
,
"
\t\t
AS Path: %v
\n
"
,
b
.
ASPath
)
fmt
.
F
printf
(
buf
,
"
\t\t
BGP type: %s
\n
"
,
bgpType
)
fmt
.
F
printf
(
buf
,
"
\t\t
NEXT HOP: %s
\n
"
,
b
.
NextHop
)
fmt
.
F
printf
(
buf
,
"
\t\t
MED: %d
\n
"
,
b
.
MED
)
fmt
.
F
printf
(
buf
,
"
\t\t
Path ID: %d
\n
"
,
b
.
PathIdentifier
)
fmt
.
F
printf
(
buf
,
"
\t\t
Source: %s
\n
"
,
b
.
Source
)
fmt
.
F
printf
(
buf
,
"
\t\t
Communities: %v
\n
"
,
b
.
Communities
)
fmt
.
F
printf
(
buf
,
"
\t\t
LargeCommunities: %v
\n
"
,
b
.
LargeCommunities
)
if
b
.
OriginatorID
!=
0
{
oid
:=
convert
.
Uint32Byte
(
b
.
OriginatorID
)
ret
+=
fmt
.
S
printf
(
"
\t\t
OriginatorID: %d.%d.%d.%d
\n
"
,
oid
[
0
],
oid
[
1
],
oid
[
2
],
oid
[
3
])
fmt
.
F
printf
(
buf
,
"
\t\t
OriginatorID: %d.%d.%d.%d
\n
"
,
oid
[
0
],
oid
[
1
],
oid
[
2
],
oid
[
3
])
}
if
b
.
ClusterList
!=
nil
{
ret
+=
fmt
.
S
printf
(
"
\t\t
ClusterList %s
\n
"
,
b
.
ClusterListString
())
fmt
.
F
printf
(
buf
,
"
\t\t
ClusterList %s
\n
"
,
b
.
ClusterListString
())
}
return
ret
return
buf
.
String
()
}
// Prepend the given BGPPath with the given ASN given times
...
...
@@ -400,31 +404,44 @@ func (b *BGPPath) ComputeHash() string {
// CommunitiesString returns the formated communities
func
(
b
*
BGPPath
)
CommunitiesString
()
string
{
str
:=
""
for
_
,
com
:=
range
b
.
Communities
{
str
+=
types
.
CommunityStringForUint32
(
com
)
+
" "
str
:=
&
strings
.
Builder
{}
for
i
,
com
:=
range
b
.
Communities
{
if
i
>
0
{
str
.
WriteByte
(
' '
)
}
str
.
WriteString
(
types
.
CommunityStringForUint32
(
com
))
}
return
str
ings
.
TrimRight
(
str
,
" "
)
return
str
.
String
(
)
}
// ClusterListString returns the formated ClusterList
func
(
b
*
BGPPath
)
ClusterListString
()
string
{
str
:=
""
for
_
,
cid
:=
range
b
.
ClusterList
{
str
:=
&
strings
.
Builder
{}
for
i
,
cid
:=
range
b
.
ClusterList
{
if
i
>
0
{
str
.
WriteByte
(
' '
)
}
octes
:=
convert
.
Uint32Byte
(
cid
)
str
+=
fmt
.
Sprintf
(
"%d.%d.%d.%d "
,
octes
[
0
],
octes
[
1
],
octes
[
2
],
octes
[
3
])
fmt
.
Fprintf
(
str
,
"%d.%d.%d.%d"
,
octes
[
0
],
octes
[
1
],
octes
[
2
],
octes
[
3
])
}
return
str
ings
.
TrimRight
(
str
,
" "
)
return
str
.
String
(
)
}
// LargeCommunitiesString returns the formated communities
func
(
b
*
BGPPath
)
LargeCommunitiesString
()
string
{
str
:=
""
for
_
,
com
:=
range
b
.
LargeCommunities
{
str
+=
com
.
String
()
+
" "
str
:=
&
strings
.
Builder
{}
for
i
,
com
:=
range
b
.
LargeCommunities
{
if
i
>
0
{
str
.
WriteByte
(
' '
)
}
str
.
WriteString
(
com
.
String
())
}
return
str
ings
.
TrimRight
(
str
,
" "
)
return
str
.
String
(
)
}
This diff is collapsed.
Click to expand it.
route/bgp_path_test.go
+
9
−
3
View file @
25c18b08
...
...
@@ -321,18 +321,24 @@ func TestBGPPathString(t *testing.T) {
expectedString
string
}{
{
input
:
BGPPath
{},
expectedString
:
"Local Pref: 0, Origin: Incomplete, AS Path: , BGP type: internal, NEXT HOP: 0:0:0:0:0:0:0:0, MED: 0, Path ID: 0, Source: 0:0:0:0:0:0:0:0, Communities: [], LargeCommunities: [], "
,
input
:
BGPPath
{
EBGP
:
true
,
OriginatorID
:
23
,
ClusterList
:
[]
uint32
{
10
,
20
},
},
expectedString
:
"Local Pref: 0, Origin: Incomplete, AS Path: , BGP type: external, NEXT HOP: 0:0:0:0:0:0:0:0, MED: 0, Path ID: 0, Source: 0:0:0:0:0:0:0:0, Communities: [], LargeCommunities: [], OriginatorID: 0.0.0.23, ClusterList 0.0.0.10 0.0.0.20"
,
expectedPrint
:
` Local Pref: 0
Origin: Incomplete
AS Path:
BGP type:
in
ternal
BGP type:
ex
ternal
NEXT HOP: 0:0:0:0:0:0:0:0
MED: 0
Path ID: 0
Source: 0:0:0:0:0:0:0:0
Communities: []
LargeCommunities: []
OriginatorID: 0.0.0.23
ClusterList 0.0.0.10 0.0.0.20
`
,
},
}
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment