Skip to content

Commit

Permalink
fb overlaps (#7)
Browse files Browse the repository at this point in the history
implements Table.Overlap
  • Loading branch information
gaissmai authored Feb 10, 2024
1 parent 00d6482 commit 151a7cc
Show file tree
Hide file tree
Showing 8 changed files with 1,144 additions and 499 deletions.
10 changes: 4 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,18 +48,16 @@ the backtracking algorithm is as fast as possible.
func (t *Table[V]) Lookup(ip netip.Addr) (lpm netip.Prefix, val V, ok bool)
func (t *Table[V]) LookupShortest(ip netip.Addr) (spm netip.Prefix, val V, ok bool)

func (t *Table[V]) Overlaps(o *Table[V]) bool
func (t *Table[V]) OverlapsPrefix(pfx netip.Prefix) bool

func (t *Table[V]) String() string
func (t *Table[V]) Fprint(w io.Writer) error
func (t *Table[V]) DumpList(is4 bool) []DumpListNode[V]
func (t *Table[V]) MarshalJSON() ([]byte, error)
func (t *Table[V]) MarshalText() ([]byte, error)
```

# TODO
func (t *Table[V]) MarshalJSON() ([]byte, error)

- [ ] implement Overlaps ...
func (t *Table[V]) DumpList(is4 bool) []DumpListNode[V]
```

# CONTRIBUTION

Expand Down
68 changes: 34 additions & 34 deletions fulltable_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,10 @@ func TestFullNew(t *testing.T) {
runtime.ReadMemStats(&endMem)
rawBytes := endMem.TotalAlloc - startMem.TotalAlloc

rtBart := bart.Table[any]{}
rt := bart.Table[any]{}
runtime.ReadMemStats(&startMem)
for _, route := range nRoutes {
rtBart.Insert(route.CIDR, nil)
rt.Insert(route.CIDR, nil)
}
runtime.ReadMemStats(&endMem)
bartBytes := endMem.TotalAlloc - startMem.TotalAlloc
Expand All @@ -64,10 +64,10 @@ func TestFullNewV4(t *testing.T) {
runtime.ReadMemStats(&endMem)
rawBytes := endMem.TotalAlloc - startMem.TotalAlloc

rtBart := bart.Table[any]{}
rt := bart.Table[any]{}
runtime.ReadMemStats(&startMem)
for _, route := range nRoutes {
rtBart.Insert(route.CIDR, nil)
rt.Insert(route.CIDR, nil)
}
runtime.ReadMemStats(&endMem)
bartBytes := endMem.TotalAlloc - startMem.TotalAlloc
Expand All @@ -87,10 +87,10 @@ func TestFullNewV6(t *testing.T) {
runtime.ReadMemStats(&endMem)
rawBytes := endMem.TotalAlloc - startMem.TotalAlloc

rtBart := bart.Table[any]{}
rt := bart.Table[any]{}
runtime.ReadMemStats(&startMem)
for _, route := range nRoutes {
rtBart.Insert(route.CIDR, nil)
rt.Insert(route.CIDR, nil)
}
runtime.ReadMemStats(&endMem)
bartBytes := endMem.TotalAlloc - startMem.TotalAlloc
Expand All @@ -108,18 +108,18 @@ var (
)

func BenchmarkFullMatchV4(b *testing.B) {
var rtBart bart.Table[int]
var rt bart.Table[int]

for i, route := range routes {
rtBart.Insert(route.CIDR, i)
rt.Insert(route.CIDR, i)
}

var ip netip.Addr

// find a random match
for {
ip = randomIP4()
_, ok := rtBart.Get(ip)
_, ok := rt.Get(ip)
if ok {
break
}
Expand All @@ -128,46 +128,46 @@ func BenchmarkFullMatchV4(b *testing.B) {
b.Run("Get", func(b *testing.B) {
b.ResetTimer()
for k := 0; k < b.N; k++ {
intSink, okSink = rtBart.Get(ip)
intSink, okSink = rt.Get(ip)
}
})

b.Run("Lookup", func(b *testing.B) {
b.ResetTimer()
for k := 0; k < b.N; k++ {
_, intSink, okSink = rtBart.Lookup(ip)
_, intSink, okSink = rt.Lookup(ip)
}
})

b.Run("LookupSCP", func(b *testing.B) {
b.ResetTimer()
for k := 0; k < b.N; k++ {
_, _, okSink = rtBart.LookupShortest(ip)
_, _, okSink = rt.LookupShortest(ip)
}
})

pfx := randomPrefix4()
b.Run("OverlapsBart", func(b *testing.B) {
b.Run("Overlaps", func(b *testing.B) {
b.ResetTimer()
for k := 0; k < b.N; k++ {
okSink = rtBart.OverlapsPrefix(pfx)
okSink = rt.OverlapsPrefix(pfx)
}
})
}

func BenchmarkFullMatchV6(b *testing.B) {
var rtBart bart.Table[int]
var rt bart.Table[int]

for i, route := range routes {
rtBart.Insert(route.CIDR, i)
rt.Insert(route.CIDR, i)
}

var ip netip.Addr

// find a random match
for {
ip = randomIP6()
_, ok := rtBart.Get(ip)
_, ok := rt.Get(ip)
if ok {
break
}
Expand All @@ -176,45 +176,45 @@ func BenchmarkFullMatchV6(b *testing.B) {
b.Run("Get", func(b *testing.B) {
b.ResetTimer()
for k := 0; k < b.N; k++ {
intSink, okSink = rtBart.Get(ip)
intSink, okSink = rt.Get(ip)
}
})

b.Run("Lookup", func(b *testing.B) {
b.ResetTimer()
for k := 0; k < b.N; k++ {
_, intSink, okSink = rtBart.Lookup(ip)
_, intSink, okSink = rt.Lookup(ip)
}
})

b.Run("LookupSCP", func(b *testing.B) {
b.ResetTimer()
for k := 0; k < b.N; k++ {
_, _, okSink = rtBart.LookupShortest(ip)
_, _, okSink = rt.LookupShortest(ip)
}
})

pfx := randomPrefix6()
b.Run("OverlapsBart", func(b *testing.B) {
b.Run("Overlaps", func(b *testing.B) {
b.ResetTimer()
for k := 0; k < b.N; k++ {
okSink = rtBart.OverlapsPrefix(pfx)
okSink = rt.OverlapsPrefix(pfx)
}
})
}

func BenchmarkFullMissV4(b *testing.B) {
var rtBart bart.Table[int]
var rt bart.Table[int]

for i, route := range routes {
rtBart.Insert(route.CIDR, i)
rt.Insert(route.CIDR, i)
}

var ip netip.Addr

for {
ip = randomIP4()
_, ok := rtBart.Get(ip)
_, ok := rt.Get(ip)
if !ok {
break
}
Expand All @@ -223,37 +223,37 @@ func BenchmarkFullMissV4(b *testing.B) {
b.Run("Get", func(b *testing.B) {
b.ResetTimer()
for k := 0; k < b.N; k++ {
intSink, okSink = rtBart.Get(ip)
intSink, okSink = rt.Get(ip)
}
})

b.Run("Lookup", func(b *testing.B) {
b.ResetTimer()
for k := 0; k < b.N; k++ {
_, intSink, okSink = rtBart.Lookup(ip)
_, intSink, okSink = rt.Lookup(ip)
}
})

b.Run("LookupSCP", func(b *testing.B) {
b.ResetTimer()
for k := 0; k < b.N; k++ {
_, _, okSink = rtBart.LookupShortest(ip)
_, _, okSink = rt.LookupShortest(ip)
}
})
}

func BenchmarkFullMissV6(b *testing.B) {
var rtBart bart.Table[int]
var rt bart.Table[int]

for i, route := range routes {
rtBart.Insert(route.CIDR, i)
rt.Insert(route.CIDR, i)
}

var ip netip.Addr

for {
ip = randomIP6()
_, ok := rtBart.Get(ip)
_, ok := rt.Get(ip)
if !ok {
break
}
Expand All @@ -262,21 +262,21 @@ func BenchmarkFullMissV6(b *testing.B) {
b.Run("Get", func(b *testing.B) {
b.ResetTimer()
for k := 0; k < b.N; k++ {
intSink, okSink = rtBart.Get(ip)
intSink, okSink = rt.Get(ip)
}
})

b.Run("Lookup", func(b *testing.B) {
b.ResetTimer()
for k := 0; k < b.N; k++ {
_, intSink, okSink = rtBart.Lookup(ip)
_, intSink, okSink = rt.Lookup(ip)
}
})

b.Run("LookupSCP", func(b *testing.B) {
b.ResetTimer()
for k := 0; k < b.N; k++ {
_, _, okSink = rtBart.LookupShortest(ip)
_, _, okSink = rt.LookupShortest(ip)
}
})
}
Expand Down
Loading

0 comments on commit 151a7cc

Please sign in to comment.