Skip to content

Commit

Permalink
simplify code
Browse files Browse the repository at this point in the history
  • Loading branch information
gaissmai committed Jul 24, 2024
1 parent 1f053cc commit c8a11d8
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 36 deletions.
28 changes: 28 additions & 0 deletions allot_tbl.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// Copyright (c) 2024 Karl Gaissmaier
// SPDX-License-Identifier: MIT

package bart

import "github.com/bits-and-blooms/bitset"

// allotedPrefixRoutes, overwrite the buffer with the precalculated words in allotment table.
func allotedPrefixRoutes(idx uint, buf []uint64) {
if idx < 256 {
// overwrite the backing array of bitset with precalculated bitset
copy(buf[:], allotLookupTbl[idx][:])
return
}
// upper half in allot tbl, just 1 bit is set, fast calculation at runtime
bitset.From(buf[:]).Set(idx)
}

// allotedHostRoutes, overwrite the buffer with the precalculated words in allotment table.
func allotedHostRoutes(idx uint, buf []uint64) {
if idx < 256 {
// overwrite the backing array of bitset with precalculated bitset
copy(buf[:], allotLookupTbl[idx][4:])
return
}
// upper half in allot tbl, just 1 bit is set, fast calculation at runtime
bitset.From(buf[:]).Set(idx - 256)
}
17 changes: 4 additions & 13 deletions allot_tbl_big_endian.go
Original file line number Diff line number Diff line change
@@ -1,21 +1,12 @@
// Copyright (c) 2024 Karl Gaissmaier
// SPDX-License-Identifier: MIT

//go:build big_endian

package bart

// allotLookupTbl, as precalculated bitsets,
// map the baseIndex to bitset with precomputed complete binary tree.
//
// // 1 <= idx <= 511
// func allotRec(aTbl *bitset.BitSet, idx uint) {
// aTbl = aTbl.Set(idx)
// if idx >= 256 {
// return
// }
// allotRec(aTbl, idx<<1)
// allotRec(aTbl, idx<<1+1)
// }
//
// Used for bitset intersections instead of range loops in overlaps tests.
// see comment in little endian file
var allotLookupTbl = [512][8]uint64{
/* idx == 0 */ {0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0},
/* idx == 1 */ {0xfeffffffffffffff, 0xffffffffffffffff, 0xffffffffffffffff, 0xffffffffffffffff, 0xffffffffffffffff, 0xffffffffffffffff, 0xffffffffffffffff, 0xffffffffffffffff},
Expand Down
3 changes: 3 additions & 0 deletions allot_tbl_little_endian.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
// Copyright (c) 2024 Karl Gaissmaier
// SPDX-License-Identifier: MIT

//go:build !big_endian

package bart
Expand Down
32 changes: 9 additions & 23 deletions node.go
Original file line number Diff line number Diff line change
Expand Up @@ -419,22 +419,15 @@ func (n *node[V]) overlapsPrefix(octet byte, pfxLen int) bool {
}

// 2. Test if prefix overlaps any route in this node
// use bitsets intersection instead of range loops
// use bitset intersection with alloted stride table instead of range loops

// buffer for bitset backing array, make sure we don't allocate
idxBuf := [8]uint64{}
idxRoutes := bitset.From(idxBuf[:])
if idx < 256 {
// overwrite the backing array of bitset with precalculated bitset
copy(idxBuf[:], allotLookupTbl[idx][:])
idxRoutes = bitset.From(idxBuf[:])
} else {
// upper half in allot tbl, just 1 bit is set, fast calculation at runtime
idxRoutes.Set(idx)
}
pfxBuf := [8]uint64{}
allotedPrefixRoutes(idx, pfxBuf[:])
prefixRoutesBitset := bitset.From(pfxBuf[:])

// use bitsets intersection instead of range loops
if idxRoutes.IntersectionCardinality(n.prefixesBitset) != 0 {
// use bitset intersection instead of range loops
if prefixRoutesBitset.IntersectionCardinality(n.prefixesBitset) != 0 {
return true
}

Expand All @@ -445,18 +438,11 @@ func (n *node[V]) overlapsPrefix(octet byte, pfxLen int) bool {

// buffer for bitset backing array, make sure we don't allocate
hostBuf := [4]uint64{}
hostRoutes := bitset.From(hostBuf[:])
if idx < 256 {
// overwrite the backing array of bitset with precalculated bitset
copy(hostBuf[:], allotLookupTbl[idx][4:])
hostRoutes = bitset.From(hostBuf[:])
} else {
// upper half in allot tbl, just 1 bit is set, fast calculation at runtime
hostRoutes.Set(idx - 256)
}
allotedHostRoutes(idx, hostBuf[:])
hostRoutesBitset := bitset.From(hostBuf[:])

// use bitsets intersection instead of range loops
return hostRoutes.IntersectionCardinality(n.childrenBitset) != 0
return hostRoutesBitset.IntersectionCardinality(n.childrenBitset) != 0
}

// eachSubnet calls yield() for any covered CIDR by parent prefix in natural CIDR sort order..
Expand Down

0 comments on commit c8a11d8

Please sign in to comment.