Skip to content

Commit

Permalink
make lpm even faster
Browse files Browse the repository at this point in the history
  • Loading branch information
gaissmai committed Dec 20, 2024
1 parent f64c8ac commit 6756403
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 6 deletions.
10 changes: 10 additions & 0 deletions internal/bitset/bitset.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,16 @@ func (b BitSet) Compact() BitSet {
return nil
}

// FirstSet returns the first bit set along with an ok code.
func (b BitSet) FirstSet() (uint, bool) {
for x, word := range b {
if word != 0 {
return uint(x<<6 + bits.TrailingZeros64(word)), true
}
}
return 0, false
}

// NextSet returns the next bit set from the specified index,
// including possibly the current index along with an ok code.
func (b BitSet) NextSet(i uint) (uint, bool) {
Expand Down
6 changes: 3 additions & 3 deletions internal/sparse/array.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func (s *Array[T]) rank(i uint) int {
// If the value already exists, overwrite it with val and return true.
func (s *Array[T]) InsertAt(i uint, val T) (exists bool) {
// slot exists, overwrite val
if s.Test(i) {
if s.Len() != 0 && s.Test(i) {
s.Items[s.rank(i)] = val

return true
Expand All @@ -48,7 +48,7 @@ func (s *Array[T]) InsertAt(i uint, val T) (exists bool) {
// DeleteAt, delete a value at i from the sparse array.
func (s *Array[T]) DeleteAt(i uint) (T, bool) {
var zero T
if !s.Test(i) {
if s.Len() == 0 || !s.Test(i) {
return zero, false
}

Expand All @@ -68,7 +68,7 @@ func (s *Array[T]) DeleteAt(i uint) (T, bool) {
func (s *Array[T]) Get(i uint) (val T, ok bool) {
var zero T

if s.Test(i) {
if s.Len() != 0 && s.Test(i) {
return s.Items[s.rank(i)], true
}

Expand Down
4 changes: 2 additions & 2 deletions node.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ func (n *node[V]) isEmpty() bool {
// max steps in backtracking is the stride length.
func (n *node[V]) lpm(idx uint) (baseIdx uint, val V, ok bool) {
// shortcut optimization
minIdx, ok := n.prefixes.NextSet(0)
minIdx, ok := n.prefixes.FirstSet()
if !ok {
return 0, val, false
}
Expand All @@ -75,7 +75,7 @@ func (n *node[V]) lpm(idx uint) (baseIdx uint, val V, ok bool) {
// lpmTest for faster lpm tests without value returns
func (n *node[V]) lpmTest(idx uint) bool {
// shortcut optimization
minIdx, ok := n.prefixes.NextSet(0)
minIdx, ok := n.prefixes.FirstSet()
if !ok {
return false
}
Expand Down
2 changes: 1 addition & 1 deletion table.go
Original file line number Diff line number Diff line change
Expand Up @@ -581,7 +581,7 @@ func (t *Table[V]) OverlapsPrefix(pfx netip.Prefix) bool {
var ok bool
for _, octet := range octets[:lastOctetIdx] {
// test if any route overlaps prefix´ so far
if n.lpmTest(hostIndex(octet)) {
if n.prefixes.Len() != 0 && n.lpmTest(hostIndex(octet)) {
return true
}

Expand Down

0 comments on commit 6756403

Please sign in to comment.