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-cidranger
Commits
8a304357
Commit
8a304357
authored
Aug 21, 2020
by
Steven Allen
Committed by
Adin Schmahmann
Aug 25, 2020
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
reduce allocations
parent
4f594431
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
27 additions
and
12 deletions
+27
-12
cidranger.go
cidranger.go
+1
-1
net/ip.go
net/ip.go
+4
-1
trie.go
trie.go
+19
-7
trie_test.go
trie_test.go
+3
-3
No files found.
cidranger.go
View file @
8a304357
...
@@ -95,5 +95,5 @@ type Ranger interface {
...
@@ -95,5 +95,5 @@ type Ranger interface {
// NewPCTrieRanger returns a versionedRanger that supports both IPv4 and IPv6
// NewPCTrieRanger returns a versionedRanger that supports both IPv4 and IPv6
// using the path compressed trie implemention.
// using the path compressed trie implemention.
func
NewPCTrieRanger
()
Ranger
{
func
NewPCTrieRanger
()
Ranger
{
return
newVersionedRanger
(
new
PrefixTree
)
return
newVersionedRanger
(
new
Ranger
)
}
}
net/ip.go
View file @
8a304357
...
@@ -236,7 +236,10 @@ func (n Network) LeastCommonBitPosition(n1 Network) (uint, error) {
...
@@ -236,7 +236,10 @@ func (n Network) LeastCommonBitPosition(n1 Network) (uint, error) {
// Equal is the equality test for 2 networks.
// Equal is the equality test for 2 networks.
func
(
n
Network
)
Equal
(
n1
Network
)
bool
{
func
(
n
Network
)
Equal
(
n1
Network
)
bool
{
return
n
.
String
()
==
n1
.
String
()
nones
,
nbits
:=
n
.
IPNet
.
Mask
.
Size
()
n1ones
,
n1bits
:=
n1
.
IPNet
.
Mask
.
Size
()
return
nones
==
n1ones
&&
nbits
==
n1bits
&&
n
.
IPNet
.
IP
.
Equal
(
n1
.
IPNet
.
IP
)
}
}
func
(
n
Network
)
String
()
string
{
func
(
n
Network
)
String
()
string
{
...
...
trie.go
View file @
8a304357
...
@@ -35,7 +35,7 @@ import (
...
@@ -35,7 +35,7 @@ import (
// TODO: Implement level-compressed component of the LPC trie.
// TODO: Implement level-compressed component of the LPC trie.
type
prefixTrie
struct
{
type
prefixTrie
struct
{
parent
*
prefixTrie
parent
*
prefixTrie
children
[]
*
prefixTrie
children
[
2
]
*
prefixTrie
numBitsSkipped
uint
numBitsSkipped
uint
numBitsHandled
uint
numBitsHandled
uint
...
@@ -46,17 +46,29 @@ type prefixTrie struct {
...
@@ -46,17 +46,29 @@ type prefixTrie struct {
size
int
// This is only maintained in the root trie.
size
int
// This is only maintained in the root trie.
}
}
var
ip4ZeroCIDR
,
ip6ZeroCIDR
net
.
IPNet
func
init
()
{
_
,
v4
,
_
:=
net
.
ParseCIDR
(
"0.0.0.0/0"
)
_
,
v6
,
_
:=
net
.
ParseCIDR
(
"0::0/0"
)
ip4ZeroCIDR
=
*
v4
ip6ZeroCIDR
=
*
v6
}
func
newRanger
(
version
rnet
.
IPVersion
)
Ranger
{
return
newPrefixTree
(
version
)
}
// newPrefixTree creates a new prefixTrie.
// newPrefixTree creates a new prefixTrie.
func
newPrefixTree
(
version
rnet
.
IPVersion
)
Ranger
{
func
newPrefixTree
(
version
rnet
.
IPVersion
)
*
prefixTrie
{
_
,
rootNet
,
_
:=
net
.
ParseCIDR
(
"0.0.0.0/0"
)
rootNet
:=
ip4ZeroCIDR
if
version
==
rnet
.
IPv6
{
if
version
==
rnet
.
IPv6
{
_
,
rootNet
,
_
=
net
.
ParseCIDR
(
"0::0/0"
)
rootNet
=
ip6ZeroCIDR
}
}
return
&
prefixTrie
{
return
&
prefixTrie
{
children
:
make
([]
*
prefixTrie
,
2
,
2
),
numBitsSkipped
:
0
,
numBitsSkipped
:
0
,
numBitsHandled
:
1
,
numBitsHandled
:
1
,
network
:
rnet
.
NewNetwork
(
*
rootNet
),
network
:
rnet
.
NewNetwork
(
rootNet
),
}
}
}
}
...
@@ -65,7 +77,7 @@ func newPathprefixTrie(network rnet.Network, numBitsSkipped uint) *prefixTrie {
...
@@ -65,7 +77,7 @@ func newPathprefixTrie(network rnet.Network, numBitsSkipped uint) *prefixTrie {
if
len
(
network
.
Number
)
==
rnet
.
IPv6Uint32Count
{
if
len
(
network
.
Number
)
==
rnet
.
IPv6Uint32Count
{
version
=
rnet
.
IPv6
version
=
rnet
.
IPv6
}
}
path
:=
newPrefixTree
(
version
)
.
(
*
prefixTrie
)
path
:=
newPrefixTree
(
version
)
path
.
numBitsSkipped
=
numBitsSkipped
path
.
numBitsSkipped
=
numBitsSkipped
path
.
network
=
network
.
Masked
(
int
(
numBitsSkipped
))
path
.
network
=
network
.
Masked
(
int
(
numBitsSkipped
))
return
path
return
path
...
...
trie_test.go
View file @
8a304357
...
@@ -73,7 +73,7 @@ func TestPrefixTrieInsert(t *testing.T) {
...
@@ -73,7 +73,7 @@ func TestPrefixTrieInsert(t *testing.T) {
}
}
for
_
,
tc
:=
range
cases
{
for
_
,
tc
:=
range
cases
{
t
.
Run
(
tc
.
name
,
func
(
t
*
testing
.
T
)
{
t
.
Run
(
tc
.
name
,
func
(
t
*
testing
.
T
)
{
trie
:=
newPrefixTree
(
tc
.
version
)
.
(
*
prefixTrie
)
trie
:=
newPrefixTree
(
tc
.
version
)
for
_
,
insert
:=
range
tc
.
inserts
{
for
_
,
insert
:=
range
tc
.
inserts
{
_
,
network
,
_
:=
net
.
ParseCIDR
(
insert
)
_
,
network
,
_
:=
net
.
ParseCIDR
(
insert
)
err
:=
trie
.
Insert
(
NewBasicRangerEntry
(
*
network
))
err
:=
trie
.
Insert
(
NewBasicRangerEntry
(
*
network
))
...
@@ -104,7 +104,7 @@ func TestPrefixTrieInsert(t *testing.T) {
...
@@ -104,7 +104,7 @@ func TestPrefixTrieInsert(t *testing.T) {
func
TestPrefixTrieString
(
t
*
testing
.
T
)
{
func
TestPrefixTrieString
(
t
*
testing
.
T
)
{
inserts
:=
[]
string
{
"192.168.0.1/24"
,
"192.168.1.1/24"
,
"192.168.1.1/30"
}
inserts
:=
[]
string
{
"192.168.0.1/24"
,
"192.168.1.1/24"
,
"192.168.1.1/30"
}
trie
:=
newPrefixTree
(
rnet
.
IPv4
)
.
(
*
prefixTrie
)
trie
:=
newPrefixTree
(
rnet
.
IPv4
)
for
_
,
insert
:=
range
inserts
{
for
_
,
insert
:=
range
inserts
{
_
,
network
,
_
:=
net
.
ParseCIDR
(
insert
)
_
,
network
,
_
:=
net
.
ParseCIDR
(
insert
)
trie
.
Insert
(
NewBasicRangerEntry
(
*
network
))
trie
.
Insert
(
NewBasicRangerEntry
(
*
network
))
...
@@ -195,7 +195,7 @@ func TestPrefixTrieRemove(t *testing.T) {
...
@@ -195,7 +195,7 @@ func TestPrefixTrieRemove(t *testing.T) {
for
_
,
tc
:=
range
cases
{
for
_
,
tc
:=
range
cases
{
t
.
Run
(
tc
.
name
,
func
(
t
*
testing
.
T
)
{
t
.
Run
(
tc
.
name
,
func
(
t
*
testing
.
T
)
{
trie
:=
newPrefixTree
(
tc
.
version
)
.
(
*
prefixTrie
)
trie
:=
newPrefixTree
(
tc
.
version
)
for
_
,
insert
:=
range
tc
.
inserts
{
for
_
,
insert
:=
range
tc
.
inserts
{
_
,
network
,
_
:=
net
.
ParseCIDR
(
insert
)
_
,
network
,
_
:=
net
.
ParseCIDR
(
insert
)
err
:=
trie
.
Insert
(
NewBasicRangerEntry
(
*
network
))
err
:=
trie
.
Insert
(
NewBasicRangerEntry
(
*
network
))
...
...
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