Skip to content

Commit

Permalink
update for performance
Browse files Browse the repository at this point in the history
  • Loading branch information
zhangdongdong2 committed Sep 24, 2020
1 parent 7ff5a0e commit dfe59a7
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 14 deletions.
3 changes: 2 additions & 1 deletion net/ip.go
Expand Up @@ -4,6 +4,7 @@ Package net provides utility functions for working with IPs (net.IP).
package net

import (
"bytes"
"encoding/binary"
"fmt"
"math"
Expand Down Expand Up @@ -236,7 +237,7 @@ func (n Network) LeastCommonBitPosition(n1 Network) (uint, error) {

// Equal is the equality test for 2 networks.
func (n Network) Equal(n1 Network) bool {
return n.String() == n1.String()
return bytes.Equal(n.IPNet.IP, n1.IPNet.IP) && bytes.Equal(n.IPNet.Mask, n1.IPNet.Mask)
}

func (n Network) String() string {
Expand Down
32 changes: 19 additions & 13 deletions trie.go
Expand Up @@ -135,11 +135,12 @@ func (p *prefixTrie) Len() int {
func (p *prefixTrie) String() string {
children := []string{}
padding := strings.Repeat("| ", p.level()+1)
for bits, child := range p.children {
if child == nil {
lens := len(p.children)
for bits := 0; bits < lens; bits++ {
if p.children[bits] == nil {
continue
}
childStr := fmt.Sprintf("\n%s%d--> %s", padding, bits, child.String())
childStr := fmt.Sprintf("\n%s%d--> %s", padding, bits, p.children[bits].String())
children = append(children, childStr)
}
return fmt.Sprintf("%s (target_pos:%d:has_entry:%t)%s", p.network,
Expand Down Expand Up @@ -314,9 +315,10 @@ func (p *prefixTrie) compressPathIfPossible() error {

// Find lone child.
var loneChild *prefixTrie
for _, child := range p.children {
if child != nil {
loneChild = child
lens := len(p.children)
for bits := 0; bits < lens; bits++ {
if p.children[bits] != nil {
loneChild = p.children[bits]
break
}
}
Expand All @@ -338,8 +340,9 @@ func (p *prefixTrie) compressPathIfPossible() error {

func (p *prefixTrie) childrenCount() int {
count := 0
for _, child := range p.children {
if child != nil {
lens := len(p.children)
for bits := 0; bits < lens; bits++ {
if p.children[bits] != nil {
count++
}
}
Expand Down Expand Up @@ -379,14 +382,17 @@ func (p *prefixTrie) walkDepth() <-chan RangerEntry {
entries <- p.entry
}
childEntriesList := []<-chan RangerEntry{}
for _, trie := range p.children {
if trie == nil {
lens := len(p.children)
for bits := 0; bits < lens; bits++ {
if p.children[bits] == nil {
continue
}
childEntriesList = append(childEntriesList, trie.walkDepth())
childEntriesList = append(childEntriesList, p.children[bits].walkDepth())
}
for _, childEntries := range childEntriesList {
for entry := range childEntries {

lensx := len(childEntriesList)
for i := 0; i < lensx; i++ {
for entry := range childEntriesList[i] {
entries <- entry
}
}
Expand Down

0 comments on commit dfe59a7

Please sign in to comment.